Android 相对误差和其他测量误差的差异

Android 相对误差和其他测量误差的差异,android,layout,Android,Layout,因此,基本上我有一个自定义视图: public class CustomView extends PercentFrameLayout 如您所见,我从PercentFrameLayout继承了它(可能是这个问题的原因,但我不确定) 在这个视图中,我已经扩展了layout.xml文件: <merge xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.

因此,基本上我有一个自定义视图:

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>
当前此自定义视图位于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
,而是任何其他
视图组
类,如
框架布局
线性布局
等等-代码正在工作。如果我只是在测量时删除
,它也会起作用。在使用
onMeasure
的情况下,此视图组中是否有任何差异?

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

你说得对!谢谢你,还有,你知道什么可以防止它吗?这是一个真实的项目,在这个布局中有很多事情要做,如果我改变它,它将从相对论变成至少3个其他的=(不幸的是,没有办法阻止相对主义的双重测量过程-这只是它的工作原理。你可以尝试做的一件事是尝试在何时更改测量规格。也许你可以计算你想要的尺寸,只有在你还没有达到规格时才更改规格。是的,我已经解决了我的问题基本上,RelativeLayout只是LinearLayout和FrameLayout的组合,这就是为什么它会对它的子对象进行两次测量?每次测量一次用于创建它的布局?我认为这与内部的相对规则有关,它需要两个阶段来获得所有的测量结果。这篇演讲可能会对t他提出了整个概念:
<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>