1.要实现一个容器  如此做:

public class CustomFrameLayout extends FrameLayout {
		private static final String TAG = "CustomFrameLayout";
		
		public CustomFrameLayout(Context context) {
			super(context);
			this.setWillNotDraw(false);//必须
		}
		
		public void onDraw(Canvas canvas){
			super.onDraw(canvas);
			drawSomething(canvas);
		}
	}

构造函数中没有 this.setWillNotDraw(false)这句话时候 onDraw函数始终不被调用 直到添加上之后才可以成功调用onDraw函数

仔细查看onDraw函数的说明:

public void setWillNotDraw (boolean willNotDraw)
Since:  API Level 1

If this view doesn't do any drawing on its own, set this flag to allow further optimizations. By default, this flag is not set on View, but could be set on some View subclasses such as ViewGroup. Typically, if you overrideonDraw(Canvas) you should clear this flag.

Parameters
willNotDraw whether or not this View draw on its own

总之,要想再自定义的画一些东西 就要进行setWillNotDraw(false).
2.如果你有两个容器 A 和 a,a是放在A之中。如果都对这两个容器设置了 setWillNotDraw(false),那么你将会看到一个“奇迹":a中被添加进来的控件都不透明了,这时候不要怀疑你的控件图片不是透明的,是因为你对A容器设置了setWillNotDraw(false),尝试将其去掉,问题就OK了


Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐