RelativeLayout和其他in-onMeasure中的差异



所以,基本上我有一个自定义视图:

public class CustomView extends PercentFrameLayout

正如你所看到的,我从PercentFrameLayout继承了它(可能是这个问题的原因,但我不确定)

在这个视图中,我已经膨胀了layout.xml文件:

<merge xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:layout_gravity="center">
<View
    style="@style/SomeStyle"
    app:layout_widthPercent="98%"
    app:layout_heightPercent="98%"/>
....
</merge>

此外,在这个自定义视图中,我覆盖了onMeasure方法,将其大小乘以2(仅用于测试):

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(MeasureSpec.getSize(widthMeasureSpec) * 2|MeasureSpec.EXACTLY, MeasureSpec.getSize(heightMeasureSpec) * 2|MeasureSpec.EXACTLY);
    }

当前此自定义视图位于RelativeLayout:中

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center">
        <com.example.CustomView
              android:id="@+id/progress_new_collagen"
              android:layout_width="120dp"
              android:layout_height="120dp"
              android:layout_centerHorizontal="true"/>
</RelativeLayout>

问题:目前,它的大小没有乘以2,而是乘以,比如宽度为4,高度为3,我不知道为什么。

奇怪的事情让我想问一个问题:如果不是RelativeLayout作为此视图的父级,而是任何其他ViewGroup类,如FrameLayoutLinearLayout等,代码正在工作。如果我只是删除onMeasure,它也会起作用。在onMeasure的情况下,这个ViewGroups有什么不同吗?

RelativeLayout每次渲染时都会调用onMeasure()两次。我认为这就是为什么你会得到奇怪的结果。如果第二个onMeasure()使用第一次调用的结果,那么您的维度将不会加倍,而是四倍。

最新更新