android - subview 的 onMeasure 和 onLayout 不会在其父 View 的 requestLayout 之后调用

标签 android android-custom-view onmeasure

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

自定义 View 组

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");
    }
}

自定义 View

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
        ));
    }
}

主 Activity

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 开始时,onMeasureonLayoutonDraw 将在标记为 CustomVieGroup1 的 View 组上调用,并且CustomVieGroup2。每个 View 组在其 subview 上调用这些方法。没关系。我明白了。

当我在标记为 CustomVieGroup1onMeasureonLayout 的 View 组上调用 requestLayout 时onDraw 在 CustomVieGroup1 上调用。该小组将这些方法用于他们的 child 。我明白了。

当我在标记为 CustomVieGroup2onMeasureonLayout 的 View 组上调用 requestLayout 时onDraw 在 CustomVieGroup2 上调用。该小组将这些方法用于他们的 child 。我也明白。

但是,当我在 ID 为 activity_main 的 View 上调用 requestLayout 时,不会调用 onMeasureonLayout CustomVieGroup1CustomVieGroup2。为什么?我预计 onMeasureonLayoutonDraw 将在 CustomVieGroup1CustomVieGroup2 上被调用>(即 id 为 activity_main 的 View 的 cgildren)

根据documentation of requestLayout :

This will schedule a layout pass of the view tree.

最佳答案

requestLayout() 将导致 LinearLayout (@id/activity_main) 的 onLayout() 方法执行。但是,无法保证 LinearLayout 会重新布局其子项;这真的取决于它的实现。例如,您的 LinearLayout 宽度/高度设置为 match_parent。当 LinearLayout 被第二次布局时,它的宽度和高度不会改变,因为它的大小是基于父元素的大小,而不是它的子元素。因为它的布局不是基于它的子元素,所以 LinearLayout 不需要重新布局它的子元素。这就是为什么几乎所有大小与其父级匹配的 ViewGroup 的布局无效都不会导致该布局的子级被布局。

您应该能够确认这是将您的 LinearLayout (@id/activity_main) 更改为“wrap_content”。通过这样做,布局的大小变得依赖于它的 child ,所以它需要重新布局它的 child 。这将导致您的自定义 View 布局方法被调用。希望这有帮助,

关于android - subview 的 onMeasure 和 onLayout 不会在其父 View 的 requestLayout 之后调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40450321/

相关文章:

android - 为平板电脑和手机创建自定义布局

java - 如何在绘制之前获取调整大小的自定义 View 的宽度和高度

android - ScrollView 内的 FlowLayout

android - Google Developer Console 不显示高级模式

android - 获取最近和正在运行的应用程序列表而不是进程

android - 自定义 EditText 未在焦点上显示键盘

android - 自定义属性 - 方向变化

Android:以编程方式在自定义 View 中设置 match_parent

android - 从 PlaceAutocomplete 中删除 "Powered by google"

android - viewModelScope 未取消