美文网首页
Android -容器- FrameLayout

Android -容器- FrameLayout

作者: 奋飞的蜗牛ing | 来源:发表于2019-04-25 18:33 被阅读0次

    onMeasure 流程

    1. 计算出所有的子View中最大的宽,高。因为FrameLayout中的每个View 在不同的层级,所有测量需要计算上,左右上下Margin,加上 FrameLayout自身的上下左右Padding,组成FrameLayout实际需要设置的宽高。

    2. 如果FragmeLayout本身宽,高不是MeasureSpec.EXACTLY,需要重新计算 设置了Match_Parent 的子View的大小。

    第一次measure:

    //确定FrameLayout本身宽高是否为EXACTLY,该变量会导致某些子view再次绘制
    final boolean measureMatchParentChildren =
        MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY ||
            MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY;
          mMatchParentChildren.clear();
    
    int maxHeight = 0;
    int maxWidth = 0;
    int childState = 0;
    
    for (int i = 0; i < count; i++) {
      final View child = getChildAt(i);
      if (mMeasureAllChildren || child.getVisibility() != GONE) {
        //调用child.measure()
        measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
        maxWidth = Math.max(maxWidth,
            child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin);
        maxHeight = Math.max(maxHeight,
            child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin);
        childState = combineMeasuredStates(childState, child.getMeasuredState());
        //将高宽属性为MATCH_PARENT的子view进行保存
        if (measureMatchParentChildren) {
          if (lp.width == LayoutParams.MATCH_PARENT ||
              lp.height == LayoutParams.MATCH_PARENT) {
            mMatchParentChildren.add(child);
          }
        }
      }
    }
    

    第二次measure:【在FrameLayout高或宽为wrap_content时,只对高或宽属性为match_parent的子view进行再次measure】

    count = mMatchParentChildren.size();
    //注意只有当count>1时,才会执行
    if (count > 1) {
      for (int i = 0; i < count; i++) {
        final View child = mMatchParentChildren.get(i);
        final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
    
        final int childWidthMeasureSpec;
        if (lp.width == LayoutParams.MATCH_PARENT) {
          final int width = Math.max(0, getMeasuredWidth()
              - getPaddingLeftWithForeground() - getPaddingRightWithForeground()
              - lp.leftMargin - lp.rightMargin);
          childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
              width, MeasureSpec.EXACTLY);
        } else {
          childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec,
              getPaddingLeftWithForeground() + getPaddingRightWithForeground() +
                  lp.leftMargin + lp.rightMargin,
              lp.width);
        }
    
        final int childHeightMeasureSpec;
        if (lp.height == LayoutParams.MATCH_PARENT) {
          final int height = Math.max(0, getMeasuredHeight()
              - getPaddingTopWithForeground() - getPaddingBottomWithForeground()
              - lp.topMargin - lp.bottomMargin);
          childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
              height, MeasureSpec.EXACTLY);
        } else {
          childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec,
              getPaddingTopWithForeground() + getPaddingBottomWithForeground() +
                  lp.topMargin + lp.bottomMargin,
              lp.height);
        }
    
        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
      }
    }
    

    例子:

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/ll_my_root"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">
    
      <Button
        android:id="@+id/tv_111"
        android:layout_width="50dp"
        android:layout_height="match_parent"
        android:background="@android:color/holo_orange_dark" />
    
      <Button
        android:id="@+id/tv_222"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@android:color/holo_blue_dark" />
    
      <Button
        android:id="@+id/tv_333"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:layout_marginTop="300dp"
        android:background="@android:color/holo_blue_dark" />
    
    </FrameLayout>
    

    Button_111的高度match_parent,会考虑Button_333的marginTop=300dp;Button_222的宽度match_parent,为Button_333的宽度200dp。

    onLayout 流程

    计算每个子View的layout_gravity,默认的layout_gravity 为Gravity.TOP | Gravity.LEFT,在根据Gravity计算时,通过水平方向的layout_gravity【CENTER_HORIZONTAL、RIGHT、LEFT】计算出childLeft,然后根据垂直方向layout_gravity 【CENTER_VERTICAL、TOP、BOTTOM】计算出childTop。
    最后调用child.layout(childLeft, childTop, childLeft + width, childTop + height);

    相关文章

      网友评论

          本文标题:Android -容器- FrameLayout

          本文链接:https://www.haomeiwen.com/subject/rzazgqtx.html