【问题标题】:RelativeLayout wrap_content not respecting non-anchor viewRelativeLayout wrap_content 不尊重非锚视图
【发布时间】:2014-04-01 01:34:14
【问题描述】:

我有一个相对布局,如下所示

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/profile_pic"
        android:layout_width="@dimen/small_size"
        android:layout_height="@dimen/small_size"
        android:layout_alignParentLeft="true"
        android:src="@drawable/some_image" />

    <ImageView
        android:id="@+id/extra_pic"
        android:layout_width="@dimen/large_size"
        android:layout_height="@dimen/large_size"
        android:layout_alignBottom="@id/profile_pic"
        andorid:layout_toRightOf="@id/profile_pic" />

</RelativeLayout>

我需要将其保留为相对布局,因为原始布局文件看起来比这更复杂。

viewGroup 的高度只包裹了profile_pic 的高度,由于extra_pic 的高度较大,它被截断了。如何将 relativeLayout 获取到 wrap_content 以便它包装布局中的最大项目?

我想要的样子:

现在是什么样子

【问题讨论】:

  • 它现在看起来怎么样?您希望它是什么样子?
  • 你应该附上一张图片来展示它现在的样子。
  • 我附上了两张图片供参考

标签: android android-layout android-xml


【解决方案1】:

看起来这是RelativeLayout 的限制。每当您使用wrap_content 来定义RelativeLayout 的大小时,肯定会有些奇怪。如果您深入研究source code,容器的高度似乎是由任何子视图的bottom 的最大值决定的。由于大视图与小视图的底部对齐,因此大视图的 bottom 与小视图的 bottom 相同,因此容器的高度不会被扩展。

没有看到你的完整布局,我不能给你一个确切的解决方案,但你可以尝试这样的事情:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <View
        android:id="@+id/spacer"
        android:layout_width="@dimen/large_size"
        android:layout_height="@dimen/large_size"
        android:visibility="INVISIBLE" />

    <ImageView
        android:id="@+id/profile_pic"
        android:layout_width="@dimen/small_size"
        android:layout_height="@dimen/small_size"
        android:layout_alignBottom="@id/spacer"
        android:layout_alignParentLeft="true"
        android:src="@drawable/some_image" />

    <ImageView
        android:id="@+id/extra_pic"
        android:layout_width="@dimen/large_size"
        android:layout_height="@dimen/large_size"
        android:layout_alignBottom="@id/profile_pic"
        android:layout_toRightOf="@id/profile_pic" />

</RelativeLayout>

不可见的垫片视图占据了较大的高度,然后其他组件与垫片对齐。这应该会导致 RelativeLayout 为您提供适当的高度。

【讨论】:

  • 感谢您的建议。但是我不能采用这个解决方案,因为它们有几个不同的项目可能会决定大小(例如,想象一个具有不同高度的 extra_pic_2,因此 extra_pic_1 会被隐藏)
  • 嗯,我最终采用了不同的布局样式来适应这个“错误”,感谢您的洞察力。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
相关资源
最近更新 更多