LinearLayout: alignment to the right of the parent

Pay attention to the following XML

<?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" > <TextView android:id="@+id/open_file_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="left" android:gravity="left" android:padding="5dp" android:text="TextView" /> <Button android:id="@+id/button1" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="right" android:text="@string/open" /> </LinearLayout> 

I want to align Button in the right corner of the layout. How can i do this?

+6
source share
5 answers

Use RelativeLayout for this android:layout_alignParentLeft="true" and android:layout_alignParentRight="true"

Try the data below

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/open_file_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:padding="5dp" android:text="TextView" /> <Button android:id="@+id/button1" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="@string/open" /> </RelativeLayout> 
+5
source

Another possible way to do this is to use the layout_weight attribute. This is my preference because it fills all the free space.

 <?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" > <TextView android:id="@+id/open_file_name" android:layout_width="0dp" android:layout_weight="3" android:layout_height="match_parent" android:layout_gravity="start|left" android:gravity="start|left" android:padding="5dp" android:text="TextView" /> <Button android:id="@+id/button1" style="?android:attr/buttonStyleSmall" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:gravity="end|right" android:text="open" /> </LinearLayout> 
+8
source

Try it too.

 <?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" > <TextView android:id="@+id/open_file_name" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="3" android:layout_gravity="left" android:padding="5dp" android:text="TextView" /> <Button android:id="@+id/button1" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:text="@string/open" /> </LinearLayout> 
+2
source

You can add padding between the TextView and Button using the layout_weight attribute:

 <View android:layout_width="0dp" android:layout_heigth="wrap_content" android:layout_weight="1"/> 

Or it is better to use RelativeLayout and android:layout_alignParentRight="true" .

0
source

Just try it, it worked for me.

  <TextView android:id="@+id/open_file_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="left" android:gravity="left" android:padding="5dp" android:text="TextView" /> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:id="@+id/button1" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="@string/open" /> </RelativeLayout> </LinearLayout> 
0
source

Source: https://habr.com/ru/post/958257/


All Articles