简体   繁体   English

将 TextView 宽度设置为 wrap_content 的 LinearLayout 裁剪子项

[英]LinearLayout clipping children with TextView width set to wrap_content

I have a LinearLayout which contains a TextView and an ImageView.我有一个包含 TextView 和 ImageView 的 LinearLayout。 The TextView width is set to wrap_content , but the issue is when the width reaches the parent width. TextView 宽度设置为wrap_content ,但问题是当宽度达到父宽度时。 The text content will correctly wrap to 2 or more lines, but the TextView and ImageView will be clipped on the left and right sides.文本内容将正确换行为 2 行或更多行,但 TextView 和 ImageView 将在左侧和右侧被剪裁。 The most similar question I could find was this one , which is from 2013 and has no solution.我能找到的最相似的问题是this one ,它来自 2013 年,没有解决方案。

Specifically, I'm experiencing the issue on this view , but I've created the following simpler view to exemplify the problem:具体来说,我在这个视图上遇到了这个问题,但我创建了以下更简单的视图来举例说明这个问题:

<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>

Here is the problem:这是问题所在: 儿童被剪裁的 LinearLayout Here is the layout normally:这是通常的布局: 没有子元素被剪裁的 LinearLayout

When the layout overlaps another view, setting android:clipChildren="false" on the LinearLayout doesn't even prevent the clipping.当布局与另一个视图重叠时,在 LinearLayout 上设置android:clipChildren="false"甚至不会阻止剪辑。

And here is one final image to prove it's happening on a real device and not just the layout viewer:这是一张最终图像,以证明它发生在真实设备上,而不仅仅是布局查看器: 真实设备上的剪辑视图

I'm basically out of ideas.我基本上没有想法了。 Any thoughts?有什么想法吗? Is this an issue with the Android layout system?这是Android布局系统的问题吗? Thanks for the help and consideration!感谢您的帮助和考虑!

You can achieve the desired result using android:layout_weight您可以使用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>

Edit: A little quote from the docs about how layout_weight works编辑:文档中关于 layout_weight 如何工作的一点引述

LinearLayout also supports assigning a weight to individual children with the android:layout_weight attribute. LinearLayout 还支持使用 android:layout_weight 属性为单个子项分配权重。 This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen.这个属性根据它应该在屏幕上占据多少空间为一个视图分配一个“重要性”值。 A larger weight value allows it to expand to fill any remaining space in the parent view.较大的权重值允许它扩展以填充父视图中的任何剩余空间。 Child views can specify a weight value, and then any remaining space in the view group is assigned to children in the proportion of their declared weight.子视图可以指定一个权重值,然后视图组中的任何剩余空间按照其声明的权重的比例分配给子视图。 Default weight is zero.默认权重为零。

For example, if there are three text fields and two of them declare a weight of 1, while the other is given no weight, the third text field without weight will not grow and will only occupy the area required by its content.例如,如果有三个文本字段,其中两个声明权重为1,而另一个没有权重,则没有权重的第三个文本字段不会增长,只会占用其内容所需的区域。 The other two will expand equally to fill the space remaining after all three fields are measured.在测量完所有三个字段后,另外两个将相等地扩展以填充剩余的空间。 If the third field is then given a weight of 2 (instead of 0), then it is now declared more important than both the others, so it gets half the total remaining space, while the first two share the rest equally.如果第三个字段的权重为 2(而不是 0),那么它现在被声明为比其他两个更重要,因此它获得总剩余空间的一半,而前两个平均分配其余空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM