繁体   English   中英

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

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

背景:

我有一个包含6个项目的布局,布局为网格(2x3)。 所有项目的宽度均为屏幕的一半(每边减去一点边距)。 每个项目都是一个RelativeLayout ,其中包含ImageView (背景)和TextView (标签)。 标签设置为包裹文本,并且允许其几乎扩大到其顶部的图像宽度。 之后,它将分为两行。

问题:

只要文本适合一行,一切看起来就不错(请参见图片中的顶部元素)。 背景很好地包裹了文字。 但是,当文本显示在两行上时,背景会变得太宽(请参见图片底部)。 即使第一行中的文本不占用太多空间,它也会填充允许的最大宽度。 有没有一种方法可以使背景与仅使用一行时的背景换行一样?

图片:

screen_capture

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

发生这种问题的可能性之一是textSize

这取决于您如何设置以及用于设置TextView textSize的度量单位。

dp,sp,pt and in可能会导致产生此类问题。

因此,请尝试使用mmpx

根据我的测试px将为您带来最佳结果。

编辑:

将以下属性添加到布局文件中的TextView中。

android:gravity="right|start"

代替 :

android:gravity="right"

谢谢。

暂无
暂无

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

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