与相对布局的右侧对齐似乎不起作用 [英] Align to right side of relative layout does not seem to work

查看:104
本文介绍了与相对布局的右侧对齐似乎不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在父容器的右侧放置2个textview,一个紧挨着另一个.
像这样:

I am trying to have 2 textviews one next to the other to the right side of the parent container.
Something like:

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

以下内容无效,但我不明白为什么

The following does not work but I can't understand why

<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仅用于显示问题

Note: The 100dp is only just to show the problem

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

UPDATE:
After removing android:layout_alignParentTop="true" and android:layout_alignParentRight="true" I get:

推荐答案

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

On your second TextView, use android:layout_toLeftOf="@id/textView1" and remove android:layout_alignParentTop="true" and 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放置在彼此重叠的右侧.

When you used android:alignParentRight="true" in both of the textViews, It will place both the textViews to the right side overlapping each other.

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

So on adding it to only one textView it will align it to the right side only. And then adding toLeftOf will place it to the left of first textView.

更新:

我没有找到有关RelativeLayout的优先级如何的任何文档.但是,我深入研究了RelativeLayout.java文件以进行查找.有点难以理解.

I didn't find any documentation for the question how is the priority given to RelativeLayout. But, I dig into the RelativeLayout.java file to find out. It was a bit hard to understand.

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

So I tried to play with the options and what i found :

  • 如果应用android:layout_alignParentStart="true"android:layout_alignParentEnd="true",则优先级将赋予 android:layout_alignParentStart

  • If you apply android:layout_alignParentStart="true" and android:layout_alignParentEnd="true" the priority will be given to android:layout_alignParentStart

如果应用android:layout_toStartOf="@id/view2"android:layout_toEndOf="@id/view2",则优先级将赋予 android:layout_toEndOf

If you apply android:layout_toStartOf="@id/view2" and android:layout_toEndOf="@id/view2" the priority will be given to android:layout_toEndOf

如果您应用android:layout_alignParentStart="true"android:layout_toStartOf="@id/view1",则优先级将赋予 android:layout_alignParentStart

If you apply android:layout_alignParentStart="true" and android:layout_toStartOf="@id/view1" the priority will be given to 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不存在的情况下才能工作.

So, it means android:layout_centerHorizontal will work only if android:layout_alignParentStart and android:layout_toStartOf will not be there.

希望现在很清楚. 如果有人找到相关文档,我很想阅读优先事项.

Hope it is clear now. I would love to read about the priorities if someone finds the documentation of this.

这篇关于与相对布局的右侧对齐似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆