简体   繁体   English

具有多行的TextView背景未正确换行

[英]Background of TextView with multiple lines not wrapping correctly

Background: 背景:

I've got a layout that consists of 6 items layed out as a grid (2x3). 我有一个包含6个项目的布局,布局为网格(2x3)。 All items are as wide as half the screen (minus a little margin on each side). 所有项目的宽度均为屏幕的一半(每边减去一点边距)。 Each item is a RelativeLayout that contains an ImageView (the background) and a TextView (the label). 每个项目都是一个RelativeLayout ,其中包含ImageView (背景)和TextView (标签)。 The label is set to wrap the text, and it's allowed to grow almost as wide as the image it sits on top of. 标签设置为包裹文本,并且允许其几乎扩大到其顶部的图像宽度。 After that it will break into two lines. 之后,它将分为两行。

Problem: 问题:

Everything looks good as long as the text fits on a single line (see top element in picture). 只要文本适合一行,一切看起来就不错(请参见图片中的顶部元素)。 The background wraps the text nicely. 背景很好地包裹了文字。 However, when the text is displayed on two lines the background gets too wide (see bottom item in picture). 但是,当文本显示在两行上时,背景会变得太宽(请参见图片底部)。 It fills up the maximum allowed width even though the text on the first line doesn't take up that much space. 即使第一行中的文本不占用太多空间,它也会填充允许的最大宽度。 Is there a way to make the background wrap the text in the same way as it does when only one line is used? 有没有一种方法可以使背景与仅使用一行时的背景换行一样?

Picture: 图片:

screen_capture

Layout XML: 布局XML:

    <ImageView
        android:id="@+id/item_image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="centerCrop" />

    <!-- some stuff I had to remove... -->

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:paddingBottom="11.33333dp">

        <!-- some other stuff I had to remove... -->

        <!-- the gray background color is set to the TextView below programmatically together with the text itself. -->
        <TextView
            android:id="@+id/item_text_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5.33333dp"
            android:paddingLeft="7.33333dp"
            android:paddingRight="20dp"
            android:paddingTop="1dp"
            android:paddingBottom="5.33333dp"
            android:layout_alignParentRight="true"
            android:gravity="right" />
    </RelativeLayout>
</RelativeLayout>

One of the possibility of such problem is textSize . 发生这种问题的可能性之一是textSize

It depends on how you are setting and which measuring unit you are using to set textSize of your TextView . 这取决于您如何设置以及用于设置TextView textSize的度量单位。

dp,sp,pt and in may cause to create such problems. dp,sp,pt and in可能会导致产生此类问题。

So try to go with mm or px specifically. 因此,请尝试使用mmpx

And as per my testing px will give you the best result. 根据我的测试px将为您带来最佳结果。

EDITED : 编辑:

Add following attribute to your TextView in layout file. 将以下属性添加到布局文件中的TextView中。

android:gravity="right|start"

Instead of : 代替 :

android:gravity="right"

Thanks. 谢谢。

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

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