第一个TextView重叠第二个,Android



我有两个TextViews,我需要水平并排,就像类别+类别的产品数量。代码如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="15dp"
    android:paddingBottom ="10dp"
    android:background="@color/navigation_background_sub">

   <TextView
        android:id="@+id/category_name_second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:textColor="@color/ebuy_color_navigation_name" />
    <TextView
        android:layout_toRightOf="@+id/category_name_second"
        android:paddingLeft="7dp"
        android:id="@+id/category_number_second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:textColor="@color/ebuy_color_navigation_number" />
</RelativeLayout>

现在的问题是,一旦第一个TextView的文本太长,留给第二个的空间就太小,并且文本显示在两行或更多行中。

我没有张贴图片的名声,所以我只是想解释如下:

如何:

笔记本电脑(34)

计算机软件(1

               4                    
               )

应该如何:

笔记本电脑(34)

计算机(14)

软件

使用

layout_toRightOf= "@id/category_name_second"

它可能会帮助您

如果用水平LineraLayout替换RelativeLayout,则可用空间将均匀分布在文本视图中。您可以添加一个weight属性来控制一行长度中每个TextView的部分。

问题是水平行没有足够的空间,这就是为什么文本视图显示更多行的原因。

您必须定义每个文本视图需要多少空间。如果你的布局如此简单,你可以选择线性布局来更好地与文本视图对齐。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="10dp"
    android:paddingLeft="15dp"
    android:weightSum="1" >
    <TextView
        android:id="@+id/category_name_second"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.7"
        android:text="Computers"
        android:textSize="22sp"
        android:singleLine="true"
        android:textColor="@color/ebuy_color_navigation_name" />
    <TextView
        android:id="@+id/category_number_second"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.3"
        android:paddingLeft="7dp"
        android:text="(24)"
        android:textSize="22sp"
        android:singleLine="true"
        android:textColor="@color/ebuy_color_navigation_number" />
</LinearLayout>

您还必须添加属性singleLine,以避免文本视图显示更多行。如您所见,使用权重可以设置水平线上每个组件的比例宽度。

在TextView中,当您使用wrap_content时,android设备会根据您输入的数据(文本长度)调整其宽度和高度(如果您使用wrappe_content表示高度)。如果文本视图中的文本不固定,则不要使用wrap_content(仅当您不希望文本自动写入第二行时)。因此,如果第一个文本视图占用了一半以上的空间,则第二个文本视图必须调整其高度以显示全文。并没有永久的解决方案,因为这条文本行取决于设备的大小。若设备的屏幕更大,可以在一行中显示两个文本视图,它会显示您,除非它会增加第二个文本视图的高度。

您还可以为所有类型的屏幕大小制作不同的布局,并根据其设置文本的大小。您可以检查是否支持多屏幕大小。

最新更新