2011-07-05 51 views
9

现在我有两个textViews,一个在相对布局的左边对齐,一个在右边对齐。左边的文本比右边的文本长得多。在某些情况下,左边的文字是两行。发生这种情况时,此文本与右侧对齐的文本重叠。如何防止视图在相对布局中重叠?

反正有防止这种情况吗?或者有什么方法可以说出左侧的文本是否是两行,将文本放在第二行的右侧?

我在与名称和时间的麻烦到这里:

<RelativeLayout android:layout_width="wrap_content" android:id="@+id/relativeLayout" android:layout_height="wrap_content"> 

    <TextView android:text="TextView" android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10sp" android:textStyle="bold" android:textSize="16sp"></TextView> 
    <TextView android:layout_width="wrap_content" android:id="@+id/address" android:text="address" android:layout_height="wrap_content" android:layout_below="@+id/name" android:layout_alignLeft="@+id/name" android:layout_marginLeft="30sp"></TextView> 
    <TextView android:layout_width="wrap_content" android:layout_toRightOf="@+id/address" android:text="" android:layout_height="wrap_content" android:layout_alignTop="@+id/address" android:layout_alignBottom="@+id/address" android:id="@+id/crossStreet"></TextView> 
    <TextView android:layout_width="wrap_content" android:id="@+id/time" android:text="Time" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="10sp"></TextView> 

</RelativeLayout> 

回答

16

你能代替包含水平的LinearLayout内这两个TextViews(其本身仍然是你的RelativeLayout的孩子)。然后,为该LinearLayout中的两个TextView分配适当的权重属性。

+0

太棒了。有效。我还将时间对准线性布局的右侧,以供将来参考。 – mergesort

+0

我应该玩重量吗?我有.35和.65来使它工作,但.4和.6产生了有趣的结果 – mergesort

0

请检查以下代码是否对您有帮助。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
    <LinearLayout android:layout_width="fill_parent" 
     android:weightSum="1.0" android:id="@+id/relativeLayout" 
     android:layout_height="wrap_content"> 
     <TextView android:text="TextView1 hhhhh hhhhhhhh hhhhhhhh hhhhhhh hhhhhh" 
      android:id="@+id/name" android:layout_weight="0.5" 
      android:layout_width="0dip" android:layout_height="wrap_content" 
      android:layout_marginLeft="10sp" android:textStyle="bold" 
      android:textSize="16sp"></TextView> 
     <TextView android:layout_width="0dip" android:layout_weight="0.5" 
      android:id="@+id/time" android:text="Textview4" 
      android:layout_height="wrap_content" android:layout_alignParentRight="true" 
      android:layout_marginRight="10sp"></TextView> 
    </LinearLayout> 
</LinearLayout> 
19

要使用RelativeLayout得到相同的结果,请使用android:layout_toLeftOf="@+id/right_text"。它会将此视图的右边缘定位到给定锚点视图ID的左侧。

请按照下面的例子:上述布局的

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingLeft="@dimen/margin_material_small" 
    android:paddingRight="@dimen/margin_material_small"> 
    <TextView 
     android:id="@+id/long_text" 
     style="@style/Base.TextAppearance.AppCompat.Title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_toLeftOf="@+id/right_text" 
     android:singleLine="true" 
     tools:text="Some text field that will definitely overlap with another one" /> 
    <TextView 
     android:id="@+id/right_text" 
     style="@style/Base.TextAppearance.AppCompat.Display1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:singleLine="true" 
     tools:text="10.34" /> 
    <TextView 
     android:id="@+id/subtext" 
     style="@style/Base.TextAppearance.AppCompat.Medium" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/long_text" 
     android:layout_alignParentLeft="true" 
     android:layout_toLeftOf="@+id/right_text" 
     android:singleLine="true" 
     tools:text="Some other text slightly smaller"/> 
</RelativeLayout> 


结果:

Result of attached example

+7

这应该是被接受的答案 –

+1

这当然比目前接受的答案更可取。不需要在相对布局内嵌套另一个容器(线性布局)。您希望尽可能减少嵌套容器。 – PentaPenguin

0
<RelativeLayout 
     android:id="@+id/relativeParent" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <TextView 
      android:id="@+id/titleLeft" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:layout_toLeftOf="@+id/imageRight" 
      android:layout_alignParentStart="true" /> 

     <ImageView 
      android:id="@+id/imageRight" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentEnd="true" 
      android:src="@drawable/image" /> 
</RelativeLayout> 



android:layout_toLeftOf="@+id/imageRight" 
android:layout_alignParentStart="true" 

的伎俩无需增加额外的LinearLayout

0

Best Fix:

android:layout_toStartOf="@id/item_on_the_right"