【问题标题】:LinearLayout children - all views should have the same height and at least wrap their contentLinearLayout 子级 - 所有视图都应具有相同的高度并至少包装其内容
【发布时间】:2018-01-06 14:02:05
【问题描述】:

这是我的(简化的)代码示例:

<LinearLayout
    android:id="@+id/llCustomValueContainer"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/llTitle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <!-- some content of undefined height -->

    </LinearLayout>

    <!-- following content is of undefined height as well -->
    <TextView
        android:id="@+id/tvValueTop"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical"/>

</LinearLayout>

我想要什么

  • llCustomValueContainer 应该从它的最高子级(llTitletvValueTop)推导出它的高度
  • llTitletvValueTop 应该有相同的高度(因为我希望选择器和触摸使用全高)并且至少应该包装它们的内容

问题

只要llTitle 的高度高于tvValueTop 的高度,一切正常。我可以以某种方式更改布局,以便我 llTitletvValueTop 具有相同的高度并至少包裹它们的内容(较小的视图应该被拉伸到与较大的视图具有相同的高度)?我不希望它们看起来具有相同的高度(例如,通过将较小的垂直居中),但我真的希望它们具有相同的高度,因为可以单击两个视图。

【问题讨论】:

  • 为什么不在 TextView 中使用 android:layout_weight="1"
  • 我不明白你的问题。你说两个孩子应该有相同的高度,但你也说你的父布局应该从它的最高子布局的高度派生它的高度。你能解释一下吗?

标签: android android-linearlayout


【解决方案1】:

外部LinearLayout 的高度为wrap_content,两个孩子的高度为match_parent

我知道这看起来有点奇怪,但只要外部LinearLayout 的子级没有 具有确定性大小(固定或wrap_content),结果行为将是每个孩子都是最高孩子的身高。

【讨论】:

  • 谢谢,那是我完全忘记的事情......这行得通
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-22
  • 1970-01-01
  • 2015-08-24
相关资源
最近更新 更多