[英]Parent LinearLayout Not Limiting Size of Inner TextView Set To WRAP_CONTENT
[英]LinearLayout clipping children with TextView width set to wrap_content
我有一个包含 TextView 和 ImageView 的 LinearLayout。 TextView 宽度设置为wrap_content
,但问题是当宽度达到父宽度时。 文本内容将正确换行为 2 行或更多行,但 TextView 和 ImageView 将在左侧和右侧被剪裁。 我能找到的最相似的问题是this one ,它来自 2013 年,没有解决方案。
具体来说,我在这个视图上遇到了这个问题,但我创建了以下更简单的视图来举例说明这个问题:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_top_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/card_top_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="One Two Three Four Five"
android:textSize="40sp"/>
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="@drawable/ic_volume_up_black_48dp"/>
</LinearLayout>
当布局与另一个视图重叠时,在 LinearLayout 上设置android:clipChildren="false"
甚至不会阻止剪辑。
这是一张最终图像,以证明它发生在真实设备上,而不仅仅是布局查看器:
我基本上没有想法了。 有什么想法吗? 这是Android布局系统的问题吗? 感谢您的帮助和考虑!
您可以使用android:layout_weight实现所需的结果
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_top_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/card_top_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="One Two Three Four Five"
android:textSize="40sp"/>
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="@drawable/ic_volume_up_black_48dp"/>
</LinearLayout>
编辑:文档中关于 layout_weight 如何工作的一点引述
LinearLayout 还支持使用 android:layout_weight 属性为单个子项分配权重。 这个属性根据它应该在屏幕上占据多少空间为一个视图分配一个“重要性”值。 较大的权重值允许它扩展以填充父视图中的任何剩余空间。 子视图可以指定一个权重值,然后视图组中的任何剩余空间按照其声明的权重的比例分配给子视图。 默认权重为零。
例如,如果有三个文本字段,其中两个声明权重为1,而另一个没有权重,则没有权重的第三个文本字段不会增长,只会占用其内容所需的区域。 在测量完所有三个字段后,另外两个将相等地扩展以填充剩余的空间。 如果第三个字段的权重为 2(而不是 0),那么它现在被声明为比其他两个更重要,因此它获得总剩余空间的一半,而前两个平均分配其余空间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.