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

无人久伴 提交于 2019-12-11 10:22:04

问题


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>

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

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


回答1:


On your second TextView, use android:layout_toLeftOf="@id/textView1" and remove android:layout_alignParentTop="true" and android:layout_alignParentRight="true"

Like this:

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

What it actually does is:

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

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.

UPDATE:

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 :

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

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

  • If you apply android:layout_alignParentStart="true" and android:layout_toStartOf="@id/view1" the priority will be given to android:layout_alignParentStart

  • If you apply android:layout_alignParentStart="true" , android:layout_centerHorizontal="true" and android:layout_toStartOf="@id/view1"

    Then the priority will be given in order

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

    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.




回答2:


  android:layout_alignParentRight="true"

Remove this from textView2



来源:https://stackoverflow.com/questions/34006068/align-to-right-side-of-relative-layout-does-not-seem-to-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!