欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

自定义view[getHeight getWidth]

程序员文章站 2022-07-14 17:41:03
...

EqualSizeLinearLayout extends LinearLayout

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
  {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int childSize = (getOrientation() == HORIZONTAL) ? getMeasuredHeight()-getPaddingTop()-getPaddingBottom()
                                                     : getMeasuredWidth()-getPaddingLeft()-getPaddingRight();
    final int childCount = getChildCount();
    for(int i=0; i<childCount; ++i)
    {
      final View child = getChildAt(i);
      if(getOrientation() == HORIZONTAL)
      {
        if(child.getMeasuredHeight() < childSize)
        {
          // adjust this child height to be childSize
          child.measure(MeasureSpec.makeMeasureSpec(child.getMeasuredWidth(), MeasureSpec.EXACTLY),
                        MeasureSpec.makeMeasureSpec(childSize, MeasureSpec.EXACTLY));
        }
      }
      else
      {
        if(child.getMeasuredWidth() < childSize)
        {
          // adjust this child width to be childSize
          child.measure(MeasureSpec.makeMeasureSpec(childSize, MeasureSpec.EXACTLY),
                        MeasureSpec.makeMeasureSpec(child.getMeasuredHeight(), MeasureSpec.EXACTLY));
        }
      }
    }
  }

getHeight()和 getMeasuredHeight()区别:
前者是最终的大小,后者是测量的大小,举个列子,当布局还没完成的时候,getHeight就是0的,
或者是在onmeasure方法里,这个方法会进来两次,第一次进来的时候getheight是0的,第二次进来才有真正的高度【也不一定额,比如进行child.measure以后这个getheight也不准了。具体看日志下边的解释】
如下的布局:

<widget.SquarLinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

        <Button
            android:id="@+id/btn_system"
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:text="系统" />
        <ToggleButton
            android:id="@+id/toggleButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textOn="on"
            android:textOff="off"
            android:text="ToggleButton" />
 </widget.SquarLinearLayout>
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//      System.err.println("widthMeasureSpec==========="+widthMeasureSpec+"============heightMeasureSpec="+heightMeasureSpec);
//      System.err.println("getMode==============w="+MeasureSpec.getMode(widthMeasureSpec)+"===h="+MeasureSpec.getMode(heightMeasureSpec));
//      System.err.println("getSize========"+MeasureSpec.getSize(widthMeasureSpec)+" ========="+MeasureSpec.getSize(heightMeasureSpec));
//      
        System.err.println(getChildCount()+"===11111======"+MeasureSpec.UNSPECIFIED+"======"+MeasureSpec.EXACTLY+"======="+MeasureSpec.AT_MOST);
        for(int i=0;i<getChildCount();i++) {
            View child=getChildAt(i);
            if(child!=null) {
                if(getOrientation()==LinearLayout.HORIZONTAL) {
                    System.err.println("i=="+i+" =="+child.getHeight()+"==="+child.getMeasuredHeight());
                    if(child.getMeasuredHeight()<getHeight()) {
                        child.measure(child.getMeasuredWidth(), MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.EXACTLY));
                    }System.err.println("i=="+i+" =="+child.getHeight()+"==="+child.getMeasuredHeight());
                    
                }else {
                    if(child.getMeasuredWidth()<getWidth()) {
                        child.measure(MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY),child.getMeasuredHeight());
                    }
                }
            }
        }
    }
    
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        System.err.println("onLayout============="+changed+"========="+(b-t)+"============"+getChildAt(1).getHeight()+"=="+getChildAt(1).getMeasuredHeight());
        super.onLayout(changed, l, t, r, b);
        
    }

日志如下:

02-11 02:01:59.767: W/System.err(28512): i==0 ==0===100
02-11 02:01:59.767: W/System.err(28512): i==0 ==0===100
02-11 02:01:59.767: W/System.err(28512): i==1 ==0===48
02-11 02:01:59.767: W/System.err(28512): i==1 ==0===48
02-11 02:01:59.787: W/System.err(28512): onLayout=============true=========100============0==48
02-11 02:01:59.797: W/System.err(28512): i==0 ==100===100
02-11 02:01:59.797: W/System.err(28512): i==0 ==100===100
02-11 02:01:59.797: W/System.err(28512): i==1 ==48===48
02-11 02:01:59.797: W/System.err(28512): i==1 ==48===100
02-11 02:01:59.797: W/System.err(28512): onLayout=============false=========100============48==100

//完事点击toggleButton,布局会重置的,可以看到,经过如下方法super.onMeasure(widthMeasureSpec, heightMeasureSpec);
child的measureHeight又还原回去了。
02-11 02:04:37.637: W/System.err(28512): i==0 ==100===100
02-11 02:04:37.637: W/System.err(28512): i==0 ==100===100
02-11 02:04:37.637: W/System.err(28512): i==1 ==100===48
02-11 02:04:37.637: W/System.err(28512): i==1 ==100===100
02-11 02:04:37.637: W/System.err(28512): onLayout=============false=========100============100==100

对日志的解释,首次加载线性布局的onmeasure会进行2次的,第一次没高度,不考虑。
第二次的高度48,进行child.measure之后高度看到还是48,这是真的吗,其实不是真的,这时候getheight应该已经是100,不过就和我们在oncreate里getheight一样,这个时候获取到的高度并不准确,你可以延迟1秒才调用child.getHeigth就可以看到返回的是100,和我们实际显示的大小一致。
第二次的日志,点击togglebutton以后整个线性布局会进行重绘,因为调用了super.onMeasure(widthMeasureSpec, heightMeasureSpec); 其实这时候child的大小又恢复原始大小48了,可其实我们看到打印的日志getheight还是100,这个是不准确的。延迟获取才能看到真实的大小。
或者给这个线性布局设置个背景,让它可以走onDraw方法,在onDraw方法里获取高度也是真实的

看下源码draw方法里如下的注释

/*
         * Draw traversal performs several drawing steps which must be executed
         * in the appropriate order:
         *
         *      1. Draw the background
         *      2. If necessary, save the canvas' layers to prepare for fading
         *      3. Draw view's content
         *      4. Draw children
         *      5. If necessary, draw the fading edges and restore layers
         *      6. Draw decorations (scrollbars for instance)
         */


    /**
     * Flag indicating whether a view failed the quickReject() check in draw(). This condition
     * is used to check whether later changes to the view's transform should invalidate the
     * view to force the quickReject test to run again.
     */
    static final int PFLAG2_VIEW_QUICK_REJECTED = 0x10000000;

//16080行
        if (!drawingWithDrawingCache) {
            if (drawingWithRenderNode) {
                mPrivateFlags &= ~PFLAG_DIRTY_MASK;
                ((DisplayListCanvas) canvas).drawRenderNode(renderNode);
            } else {
                // Fast path for layouts with no backgrounds
                if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
                    mPrivateFlags &= ~PFLAG_DIRTY_MASK;
                    dispatchDraw(canvas);
                } else {
                    draw(canvas);
                }
            }
        }

线性布局,相对布局,帧布局,只有设置了背景才会执行 draw和onDraw方法。 最后执行的是dispathDraw
另外,线性布局如果设置divider也是可以的,不需要背景。