Android 如何防止视图在相对布局中重叠?

Android 如何防止视图在相对布局中重叠?,android,Android,现在,我有两个TextView。一个对齐到RelativeLayout的左侧,另一个对齐到右侧。左边的TextView比右边的长得多。在某些情况下,左侧的TextView是两行。发生这种情况时,它与右侧对齐的TextView重叠 有没有办法防止这种情况?或者有没有办法说如果左边的TextView是两行,就把右边的TextView放在第二行 我在这里的名字和时间都有问题: <RelativeLayout android:layout_width="wrap_content"

现在,我有两个
TextView
。一个对齐到<代码>RelativeLayout的左侧,另一个对齐到右侧。左边的
TextView
比右边的长得多。在某些情况下,左侧的
TextView
是两行。发生这种情况时,它与右侧对齐的
TextView
重叠

有没有办法防止这种情况?或者有没有办法说如果左边的
TextView
是两行,就把右边的
TextView
放在第二行

我在这里的名字和时间都有问题:

<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
        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
        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
        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"/>

</RelativeLayout>


您是否可以将这两个文本视图包含在水平线性布局中(它本身仍然是您的RelativeLayout的子对象)。然后,为该LinearLayout中的两个文本视图分别指定适当的权重属性

请检查以下代码,以便对您更有帮助

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

要使用
RelativeLayout
获得相同的结果,请使用
android:layout\u-toLeftOf=“@+id/right\u-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>


上述布局的结果:


android:layout_-toLeftOf=“@+id/imageRight”
android:layout\u alignParentStart=“true”
在不添加额外线性布局的情况下执行了此技巧

最佳修复:

android:layout_toStartOf="@id/item_on_the_right"

令人惊叹的。成功了。我还将时间与线性布局的右侧对齐,以供将来参考。我是否应该玩重量游戏?我有0.35和0.65来让它工作,但是0.4和0.6产生了有趣的结果。这当然比目前公认的答案更可取。不需要在相对布局中嵌套另一个容器(线性布局)。您希望尽可能减少嵌套容器。
android:layout_toStartOf="@id/item_on_the_right"