2014-08-30 79 views
0

我有2 TextViews,我需要水平并排,如类别+产品类别nummber类别。代码如下所示:第一个TextView重叠第二个,Android

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

软件

+0

对两个TextView都使用相同的ID。使他们独一无二,它应该工作 – hoomi 2014-08-30 16:15:58

+0

他没有使用相同的ID。这甚至不可能 – eldjon 2014-08-30 16:16:30

+0

你尝试过'layout_toEndOf'而不是'layout_toRightOf'吗? – mbmc 2014-08-30 16:16:58

回答

0

使用

​​

它可以帮助你

+0

这是我一直在使用的东西 – Xhulio 2014-08-30 16:21:14

+0

你可以删除填充 – 2014-08-30 16:29:45

+0

我试过了,没有。 – Xhulio 2014-08-30 16:36:36

0

如果更换RelativeLayout与水平LineraLayout的可用空间将均匀之间进行分配您的文字视图。您可以添加一个weight属性来控制每个长度的每个TextView的部分。

+0

是的,但如果将'LinearLayout'与'weights'一起使用,如果第一个'TextView'的文本太小,恐怕两个'TextView'之间的空间太大或太小。 – Xhulio 2014-08-30 16:26:25

+0

如果使用wrap_content,则不适用 – eldjon 2014-08-30 16:49:34

0

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

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

<?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的显示更多行。如您所见,使用重量可以为水平线中的每个组件设置比例宽度。

0

在TextView中当你使用wrap_content时,android设备会根据你放入的数据(文本长度)调整它的宽度和高度(如果你使用wrap_content作为高度)。如果textview没有在textview中修复,那么请不要使用wrap_content(仅当您不希望该文本自动写入第二行时)。因此,如果第一个Textview占用了超过一半的空间,第二个textview必须调整其高度以显示完整text.There没有永久的解决方案,因为这个文本行取决于设备的大小。如果设备的屏幕比较大以显示单行中的textview,它会向您显示,除非它会增加您的案例中第二个textview的高度。

您还可以针对所有类型的屏幕尺寸进行不同的布局,并根据它设置文本的大小。您可以check支持多种屏幕尺寸。

相关问题