Placing an item on the right in a linear layout in android

I want to place the image to the right of the view. For this I use something like

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > ...some other elements <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <ImageView android:id="@+id/imageIcon" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="right" android:src="@drawable/image_icon" > </ImageView> </LinearLayout> ... </RelativeLayout> 

The above image puts the image somewhere in the center. How to ensure the correct alignment of the image, regardless of whether we are in portrait or landscape mode?

Thanx!

+4
source share
3 answers

It seems to work. Two changes 1. use orientation = vertical, as suggested by Zortkun 2. Use wrap_content in layout_width.

 <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:id="@+id/settingsIcon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:src="@drawable/settings_icon" > </ImageView> </LinearLayout> 
+3
source

The default orientation of the LinearLayout is horizontal. Make sure the orientation of the linear layout is set to Vertical , otherwise you cannot use the material horizontally.

 <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > 
0
source

You do not need Linearlayout if you use only one view inside Linringayout.

Anyway, here,

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > //...some other elements <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="right"> <ImageView android:id="@+id/imageIcon" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="right" android:src="@drawable/image_icon" > </ImageView> </LinearLayout> </RelativeLayout> 
0
source

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


All Articles