child 视图的 onMeasure 和 onLayout 在其 parent 上的 requestLayout 之后未被调用

onMeasure and onLayout of child views are not called after requestLayout on their parent

我正在尝试了解 Android 如何测量和布置视图。我创建了一个带有自定义视图和视图组的示例应用程序。

CustomViewGroup

public class CustomViewGroup extends ViewGroup {

    private static final String LOG_TAG = "CustomViewGroup";

    public CustomViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.d(LOG_TAG, String.format(Locale.ENGLISH,
                getTag() + " " +
                        "onMeasure(widthMeasureSpec=%d, heightMeasureSpec%d)",
                widthMeasureSpec, heightMeasureSpec
        ));

        final int count = getChildCount();
        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() != View.GONE) {
                // Make or work out measurements for children here (MeasureSpec.make...)
                measureChild(child, widthMeasureSpec, heightMeasureSpec);
            }
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        Log.d(LOG_TAG, String.format(Locale.ENGLISH,
                getTag() + " " +
                        "onLayout(changed=%b, left=%d, top=%d, right=%d, bottom=%d)",
                changed, left, top, top, right, bottom
        ));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.d(LOG_TAG, getTag() + " " + "onDraw");
    }
}

自定义视图

public class CustomView extends View {

    private static final String LOG_TAG = "CustomView";

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        Log.d(LOG_TAG, String.format(Locale.ENGLISH,
                getTag() + " " +
                "onLayout(changed=%b, left=%d, top=%d, right=%d, bottom=%d)",
                changed, left, top, top, right, bottom
        ));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.d(LOG_TAG, getTag() + " " +"onDraw");
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.d(LOG_TAG, String.format(Locale.ENGLISH,
                getTag() + " " +
                "onMeasure(widthMeasureSpec=%d, heightMeasureSpec=%d)",
                widthMeasureSpec, heightMeasureSpec
        ));
    }
}

MainActivity

public class MainActivity extends AppCompatActivity {

    private View mCustomGroup1;
    private View mCustomGroup2;
    private View mContainer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.d("MainAct", "onCreate");
        mCustomGroup1 = findViewById(R.id.requestLayout);
        mCustomGroup2 = findViewById(R.id.requestLayout2);
        mContainer = findViewById(R.id.activity_main);

        findViewById(R.id.requestLayoutBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCustomGroup1.requestLayout();
            }
        });

        findViewById(R.id.requestLayoutBtn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCustomGroup2.requestLayout();
            }
        });

        findViewById(R.id.requestLayoutContBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mContainer.requestLayout();
            }
        });

    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="ru.dmitriev.squareorderedlayout.MainActivity">

    <Button
        android:id="@+id/requestLayoutBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="requestLayout" />

    <Button
        android:id="@+id/requestLayoutBtn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="requestLayout2" />

    <Button
        android:id="@+id/requestLayoutContBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="requestLayoutCont" />

    <ru.dmitriev.squareorderedlayout.CustomViewGroup
        android:id="@+id/requestLayout"
        android:layout_width="100dp"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#7f7"
        android:tag="CustomVieGroup1">

        <ru.dmitriev.squareorderedlayout.CustomView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="CustomView11" />

        <ru.dmitriev.squareorderedlayout.CustomView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="CustomView12" />
    </ru.dmitriev.squareorderedlayout.CustomViewGroup>

    <ru.dmitriev.squareorderedlayout.CustomViewGroup
        android:id="@+id/requestLayout2"
        android:layout_width="100dp"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#f77"
        android:tag="CustomVieGroup2">

        <ru.dmitriev.squareorderedlayout.CustomView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="CustomView21" />

        <ru.dmitriev.squareorderedlayout.CustomView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="CustomView22" />

    </ru.dmitriev.squareorderedlayout.CustomViewGroup>
</LinearLayout>

当 activity 启动时,在标记为 CustomVieGroup1CustomVieGroup2 的视图组上调用 onMeasureonLayoutonDraw。每个视图组在其 children 上调用这些方法。没关系。我明白了。

当我在标记为 CustomVieGroup 的视图组上调用 requestLayout1onMeasureonLayoutonDraw 被调用在 CustomVieGroup1 上。该小组在 children 上调用这些方法。我明白了。

当我在标记为 CustomVieGroup 的视图组上调用 requestLayout2onMeasureonLayoutonDraw 被调用在 CustomVieGroup2 上。该小组在 children 上调用这些方法。我也明白了。

但是当我在 ID 为 activity_main 的视图上调用 requestLayout 时,onMeasureonLayout 不会在 CustomVieGroup1 或 [=20= 上调用].为什么?我预计 onMeasureonLayoutonDraw 将在 CustomVieGroup1CustomVieGroup2 上被调用(即具有 id activity_main 的视图的 cgildren )

根据 documentation of requestLayout:

This will schedule a layout pass of the view tree.

requestLayout() 将导致 LinearLayout (@id/activity_main) 的 onLayout() 方法执行。但是,不能保证 LinearLayout 会 re-layout 其 children;这真的取决于它的实施。例如,您的 LinearLayout width/height 设置为 match_parent。当 LinearLayout 第二次变为 laid-out 时,它的宽度和高度不会改变,因为它的大小是基于它的 parent 大小,而不是它的 children。因为它的布局不是基于它的children,所以不需要LinearLayout到re-layout它的children。这就是为什么使几乎任何大小与其 parent 匹配的 ViewGroup 的布局无效不会导致该布局的 children 成为 laid-out.

您应该能够确认这是将您的 LinearLayout (@id/activity_main) 更改为 'wrap_content'。通过这样做,布局的大小变得取决于它的 children,因此它需要 re-layout 它的 children。这将导致您的自定义视图布局方法被调用。希望这有帮助,