Android自定义ViewGroup控件中两种方式获取的宽度为什么不一样

问题描述:

通过getLayoutParams().width和getMeasuredWidth()获取的宽度为什么不一样
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    View menuView=getChildAt(0);//得到menu控件
    menuView.measure(menuView.getLayoutParams().width,heightMeasureSpec);
    System.out.println("布局宽度-----------"+menuView.getLayoutParams().width);
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
     protected void onLayout(boolean changed, int l, int t, int r, int b) {
    View menuView=getChildAt(0);
    menuView.layout(-menuView.getLayoutParams().width,t,0,b);
    menuWidth = menuView.getLayoutParams().width;
    System.out.println("getMeasured宽度:::::"+menuView.getMeasuredWidth());
            }

安卓界面显示要经过三个过程,measure,layout,draw。
第一个过程viewgroup会根据自身的特点传递一个值(这个值有三种)给它包含的view,view会根据自身的情况再给viewgroup一个值。这样两个值综合起来就能大致决定view的大小。
第二个过程是根据第一步测量的大小,从根viewgroup开始(一般说默认的是fraglayout),一层一层往下分析,看应该将布局绘制到什么位置。
最后一步根据layout的结果,直接将图像显示出来就可以了。
具体的内容我也记不太清了,可以自己百度一下(measure,layout,draw)关键字,很多博客里面都有说明