I try to set the background alpha of my custom linearLayout.
I have tried:
//ctor
protected MyView(
Context context, AttributeSet attrs, int defStyle, @LayoutRes int layout) {
super(context, attrs, defStyle);
setOrientation(VERTICAL);
LayoutInflater.from(context).inflate(layout, this);
getBackground().setAlpha(128);
}
and
vto.addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
getViewTreeObserver().removeOnGlobalLayoutListener(this);
getBackground().setAlpha(128);
}
});
but the background is still null
I have tried
ViewTreeObserver vto = getViewTreeObserver();
vto.addOnPreDrawListener(new OnPreDrawListener() {
@Override
public boolean onPreDraw() {
vto.removeOnPreDrawListener(this);
getBackground().setAlpha(128);
return true;
}
});
but i get java.lang.IllegalStateException: This ViewTreeObserver is not alive, call getViewTreeObserver() again
What is the right timing to get the background and change its alpha (opacity)?
您是否尝试过在xml本身的android:alpha =“ 0.5”属性中进行线性布局?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.