对齐到相对布局的右侧似乎不起作用

Align to right side of relative layout does not seem to work

我正在尝试让 2 个文本视图一个挨着一个在父容器的右侧。
类似于:

+++++++++++++++++++++++++++++++++++++  (parent container)  
|               TextView2 TextView1 |  
+++++++++++++++++++++++++++++++++++++    

下面的不起作用,但我不明白为什么

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="100dp"
              android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:text="TEXTVIEW-1" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_toLeftOf="@id/textView1"
            android:layout_marginRight="1dp"
            android:text="TEXTVIEW-2" />

    </RelativeLayout>

</LinearLayout>

注意:100dp只是为了说明问题

更新:
删除 android:layout_alignParentTop="true" 和 android:layout_alignParentRight="true" 我得到:

在你的第二个 TextView 上,使用 android:layout_toLeftOf="@id/textView1" 并删除 android:layout_alignParentTop="true"android:layout_alignParentRight="true"

像这样:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="100dp"
              android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:text="TEXTVIEW-1"
            />
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@id/textView1"
            android:layout_marginRight="1dp"
            android:text="TEXTVIEW-2"
            />

    </RelativeLayout>  

</LinearLayout>

它实际做的是:

当您在两个 textView 中使用 android:alignParentRight="true" 时,它会将两个 textView 放置在右侧,彼此重叠。

因此,仅将它添加到一个 textView 时,它只会与右侧对齐。然后添加 toLeftOf 会将其放置在第一个 textView 的左侧。

UPDATE:

我没有找到关于如何优先考虑 RelativeLayout 的问题的任何文档。但是,我深入研究了 RelativeLayout.java 文件以找出答案。有点难懂。

所以我尝试使用选项和发现的内容:

  • 如果申请了android:layout_alignParentStart="true"android:layout_alignParentEnd="true",优先考虑android:layout_alignParentStart

  • 如果你申请了android:layout_toStartOf="@id/view2"android:layout_toEndOf="@id/view2",会优先给android:layout_toEndOf

  • 如果你申请了android:layout_alignParentStart="true"android:layout_toStartOf="@id/view1",那么优先给android:layout_alignParentStart

  • 如果您申请 android:layout_alignParentStart="true" , android:layout_centerHorizontal="true"android:layout_toStartOf="@id/view1"

    然后优先顺序

     android:layout_alignParentStart="true"   //1 High
     android:layout_toStartOf="@id/view1"     //2 Medium
     android:layout_centerHorizontal="true"   //3 Low
    

    所以,这意味着 android:layout_centerHorizontal 只有在 android:layout_alignParentStartandroid:layout_toStartOf 不存在的情况下才有效。

希望现在已经清楚了。 如果有人找到这方面的文档,我很乐意阅读有关优先级的信息。

  android:layout_alignParentRight="true"

从 textView2 中删除它