【问题标题】:RelativeLayout ignoring size set by childRelativeLayout 忽略子级设置的大小
【发布时间】:2016-04-29 17:21:13
【问题描述】:

我的布局大致如下:

<RelativeLayout>
  <TextView
    layout_width="match_parent"
    layout_height="50dp"
    layout_alignParentTop="true"
    id="@+/A"/>
  <TextView
    layout_width="match_parent"
    layout_height="46dp"
    layout_alignParentBottom="true"
    id="@+/B"/>
  <CustomView 
    layout_width="wrap_content"
    layout_height="wrap_content"
    layout_below="@id/A"
    layout_above="@id/B"
    layout_centerHorizontal="true"/>
</RelativeLayout>

目的是 A 和 B 应该以固定的高度在顶部和底部,并且 CustomView 应该尽可能多地占用剩余空间,因为它可以受到其纵横比的限制。 (注意我不能使用LinearLayout,因为我省略了一些其他的孩子)

以下是特定屏幕尺寸的情况:

  • CustomView.onMeasure 使用 (AT_MOST 776, AT_MOST 395) 调用
  • CustomView 调用 setMeasuredDimension(285, 391)
  • CustomView.onMeasure 再次调用 (EXACTLY 285, EXACTLY 251)
  • CustomView 调用 setMeasuredDimension(参数似乎无关紧要)
  • CustomView.onLayout 调用尺寸为 285 x 251

我猜顶部和底部约束强制高度为251,但为什么RelativeLayout 不给CustomView 一个机会根据这个高度约束选择宽度?假设如果一个孩子可以是 285x391,那么它也可以是 285x251,这似乎是不正确的行为。是否有解决方法(除了用另一个自定义视图替换 RelativeLayout)?

我使用 minSdkVersion=14 和 targetSdkVersion=21,以防万一。

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    我写了这样的代码

    `

    <RelativeLayout
        android:layout_width="wrap_content"
        android:background="@color/material_blue_grey_800"
        android:layout_height="wrap_content">
    
        <TextView android:id="@+id/A"
            android:layout_width="match_parent"
            android:layout_alignParentTop="true"
            android:text="TOP TEXT"
            android:textColor="#ff0000"
            android:layout_height="50dp" />
    
        <TextView android:id="@+id/B"
            android:layout_width="match_parent"
            android:layout_alignParentBottom="true"
            android:text="Bottom text"
            android:textColor="#00ff00"
            android:background="#820f22"
            android:layout_height="50dp" />
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/A"
            android:layout_above="@+id/B"
            android:background="#0ff0ff"
            android:src="@drawable/abc_btn_check_material"
            android:layout_centerInParent="true"/>
    
    
    </RelativeLayout>
    

    `

    您可以检查它是否适合我,您可以尝试将自定义视图高度更改为“match_parent”并移除重力方向

    【讨论】:

    • 我尝试了所有 wrap_content/match_parent 的高度和 centerInParent/centerHorizo​​ntal/无重力组合。对于高度受限的情况,全部生成:i.imgur.com/vrE0qxa.png。请注意,水色背景表明 ImageView 不是方形的,尽管图像在 ImageView 内居中。也许我不能假设 CustomView 可以选择它的纵横比。
    【解决方案2】:

    只需将重量和高度与父级匹配到中心视图,并删除 layout_above,您可以在我的代码中找到。

    <RelativeLayout>
      <TextView
        layout_width="match_parent"
        layout_height="50dp"
        layout_alignParentTop="true"
        id="@+/A"/>
      <TextView
        layout_width="match_parent"
        layout_height="46dp"
        layout_alignParentBottom="true"
        id="@+/B"/>
      <CustomView 
        layout_width="wrap_content"
        layout_height="match_parent"
        layout_below="@id/A"
        weight=1
        layout_centerHorizontal="true"/>
    </RelativeLayout>
    

    此代码工作背后的原因是您的顶部和底部视图具有固定值,因此布局必须为其腾出空间,即使中心视图的权重为 1。 抱歉打错了。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-08
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多