Android中Widget的不同visibility属性值调用
2021-03-18 本文已影响0人
千夜零一
View的绘制流程
绘制流程主要关乎这三个方法:onMesure()、onLayout()、onDraw(),分别是测量、布局、绘制。
定义一个View类,重写三个方法
public class MyDiyView extends View {
public MyDiyView(Context context) {
super(context);
}
public MyDiyView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public MyDiyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public MyDiyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Log.d("MyDiyViewTest:==>","onMeasure()");
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
Log.d("MyDiyViewTest:==>","onLayout()");
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.d("MyDiyViewTest:==>","onDraw()");
}
}
结论:
android:visibility="visible" //可见
D/MyDiyViewTest:==>: onMeasure()
D/MyDiyViewTest:==>: onLayout()
D/MyDiyViewTest:==>: onDraw()
android:visibility="invisible" //占位不可见
D/MyDiyViewTest:==>: onMeasure()
D/MyDiyViewTest:==>: onLayout()
android:visibility="gone" //不占位不可见
三个方法都不调用