Why android: layout_gravity = "right" doesn't work here in android xml

my xml code for list list string

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:baselineAligned="false" android:orientation="horizontal" > <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="5" android:orientation="vertical" > <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" /> <TextView android:id="@+id/tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_weight="2" android:gravity="center" > <ImageView android:id="@+id/iv_image" android:layout_width="50dp" android:layout_height="40dp" android:background="@drawable/app_icon_17" android:contentDescription="@string/empty" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_weight="2" android:gravity="center" > <Button android:id="@+id/btn_delete" android:layout_width="40dp" android:layout_height="40dp" android:background="@drawable/btn_delete_new" android:focusable="false" /> </LinearLayout> </LinearLayout> 

I want the last button with id btn_delete to stay aligned right. And its display, as I wanted in the "graphic layout" when designing. But when I run it, it does not work on the emulator.

See the output:

enter image description here

So why does the delete button not align right?

+4
source share
5 answers

Use this using relative layout

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" > <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" /> <TextView android:id="@+id/tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_name" android:layout_marginLeft="10dp" /> </RelativeLayout> <RelativeLayout android:id="@+id/rl_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" > <Button android:id="@+id/btn_delete" android:layout_width="40dp" android:layout_height="40dp" android:background="@drawable/btn_delete_new" android:focusable="false" /> </RelativeLayout> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="20dp" android:layout_toLeftOf="@+id/rl_btn" > <ImageView android:id="@+id/iv_image" android:layout_width="50dp" android:layout_height="40dp" android:background="@drawable/app_icon_17" android:contentDescription="@string/empty" /> </RelativeLayout> </RelativeLayout> 
+7
source

It seems that you misunderstood the layouts, those built-in LinearLayouts not needed. Use a RelativeLayout , for example:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="10dp" /> <TextView android:id="@+id/tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_marginLeft="10dp" /> <ImageView android:id="@+id/iv_image" android:layout_width="50dp" android:layout_height="40dp" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:layout_toRightOf="@id/tv_time" android:background="@drawable/app_icon_17" android:contentDescription="@string/empty" /> <Button android:id="@+id/btn_delete" android:layout_width="40dp" android:layout_height="40dp" android:layout_centerVertical="true" android:layout_alignParentRight="true" android:background="@drawable/btn_delete_new" android:focusable="false" /> </RelativeLayout> 
+5
source

As we see, you have assigned the weight to your line layout.

If you have assigned the weight of your parent layout , you just need to set the appropriate height either using 0dp . In your case android:layout_width="0dp"

but if you have a child layout of this weighted layout, you must specify the layouts property as fill_parent ie: button android:layout_height="fill_parent"

So, if you do not want to change the parent layout and want to work with existing code, you can try to execute the code below.

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:baselineAligned="false" android:orientation="horizontal" > <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="5" android:orientation="vertical" > <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" /> <TextView android:id="@+id/tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" > <ImageView android:id="@+id/iv_image" android:layout_width="fill_parent" android:layout_height="40dp" android:background="@drawable/icon" android:contentDescription="@string/app_name" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" > <Button android:id="@+id/btn_delete" android:layout_width="fill_parent" android:layout_height="40dp" android:background="@drawable/icon" android:focusable="false" /> </LinearLayout> </LinearLayout> 
+1
source

Change it

 <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_weight="2" android:gravity="center" > <Button android:id="@+id/btn_delete" android:layout_width="40dp" android:layout_height="40dp" android:background="@drawable/btn_delete_new" android:focusable="false" /> </LinearLayout> 

to

 <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:id="@+id/btn_delete" android:layout_width="40dp" android:layout_height="40dp" android:layout_gravity="right" android:background="@drawable/btn_delete_new" android:focusable="false" /> </LinearLayout> 

You applied android:layout_gravity="right" to the layout. Instead, you should apply it to the button inside the layout. And I don’t think you need 2 weight for this layout. But you can add it if you need it. Hope this helps.

0
source
 <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="2" android:orientation="vertical" > <Button android:id="@+id/btn_delete" android:layout_width="40dp" android:layout_height="40dp" android:layout_gravity="right" android:gravity="right" android:background="@drawable/btn_delete_new" android:focusable="false" /> </LinearLayout> 
-3
source

All Articles