RelativeMeayout和onMeasure中其他的差异

时间:2016-04-04 14:04:01

标签: android layout

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

public class CustomView extends PercentFrameLayout

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

在这个视图的内部,我已经夸大了布局.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有什么不同吗?

1 个答案:

答案 0 :(得分:0)

RelativeLayout每次呈现时都会调用onMeasure()两次。我认为这就是你得到奇怪结果的原因。如果第二个onMeasure()使用第一次调用的结果,那么您不会翻倍,而是将尺寸翻两番。