Compatible Android Layout Right Align

I have the following layout in place

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> <WebView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="13"> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content"> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> <ImageButton android:background="@null" android:id="@+id/back" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/back" android:padding="10dip" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> <ImageButton android:background="@null" android:id="@+id/forward" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/forward" android:padding="10dip" /> </LinearLayout> </LinearLayout> <RelativeLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" > <ImageButton android:background="@null" android:id="@+id/special" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/barcode" android:padding="10dip" android:layout_gravity="right"/> </RelativeLayout> </LinearLayout> </LinearLayout> 

For this question, only the bottom half of the layout bothers me. Now it contains 3 image buttons. The first 2, I want next to each other aligned to the left. Third, I want to be aligned on the right side.

As it is, the first 2 buttons are the place where I want them to be, but the third - without any problems remaining to the left. How I would do it right.

+63
android layout right-align
Nov 29 '10 at 16:05
source share
5 answers

The layout is extremely inefficient and bloated. You do not need much LinearLayout s. In fact, you don't need LinearLayout at all.

Use only one RelativeLayout . Like this.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageButton android:background="@null" android:id="@+id/back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/back" android:padding="10dip" android:layout_alignParentLeft="true"/> <ImageButton android:background="@null" android:id="@+id/forward" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/forward" android:padding="10dip" android:layout_toRightOf="@id/back"/> <ImageButton android:background="@null" android:id="@+id/special" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/barcode" android:padding="10dip" android:layout_alignParentRight="true"/> </RelativeLayout> 
+123
Nov 29 '10 at 16:16
source share
β€” -

You can do all this using only one RelativeLayout (which, btw, does not need the android:orientation parameter). So, instead of having a LinearLayout containing a bunch of things, you can do something like:

 <RelativeLayout> <ImageButton android:layout_width="wrap_content" android:id="@+id/the_first_one" android:layout_alignParentLeft="true"/> <ImageButton android:layout_width="wrap_content" android:layout_toRightOf="@+id/the_first_one"/> <ImageButton android:layout_width="wrap_content" android:layout_alignParentRight="true"/> </RelativeLayout> 

As you noticed, some XML options are missing. I just showed the main parameters that you had to set. You can finish the rest.

+18
Nov 29 '10 at 16:13
source share

This is an example for a RelativeLayout :

 RelativeLayout relativeLayout=(RelativeLayout)vi.findViewById(R.id.RelativeLayoutLeft); RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams(); params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); relativeLayout.setLayoutParams(params); 

With a different layout view (e.g. LinearLayout) you just need to change the RelativeLayout for LinearLayout.

+3
Aug 07 '13 at 16:53 on
source share

If you want to use LinearLayout , you can align with layout_weight using the Space element.

eg. next placements textView and textView2 next to each other and textView3 will be aligned right

 <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Medium Text" android:id="@+id/textView" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Medium Text" android:id="@+id/textView2" /> <Space android:layout_width="0dp" android:layout_weight="1" android:layout_height="20dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Medium Text" android:id="@+id/textView3" /> </LinearLayout> 

you can achieve the same effect without Space if you set layout_weight to textView2 . I just like to share more and also to demonstrate the Space element.

  <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Medium Text" android:id="@+id/textView2" /> 

Note that you must (optionally) set layout_width explicitly, as it will be recalculated according to that weight anyway (just like you should set the height in vertical LinearLayout elements). For other layout tips, see Android Layout Tricks .

+3
Nov 19 '13 at 11:17
source share

To support the old version of Space, you can replace View with the following. Add this view between those left by most components and before the right majority of components. This view with weight = 1 will stretch and fill the space

  <View android:layout_width="0dp" android:layout_height="20dp" android:layout_weight="1" /> 

Here is the full example code. It has 4 components. Two arrows will be on the right and on the left. Text and Spinner will be in the middle.

  <ImageButton android:id="@+id/btnGenesis" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center|center_vertical" android:layout_marginBottom="2dp" android:layout_marginLeft="0dp" android:layout_marginTop="2dp" android:background="@null" android:gravity="left" android:src="@drawable/prev" /> <View android:layout_width="0dp" android:layout_height="20dp" android:layout_weight="1" /> <TextView android:id="@+id/lblVerseHeading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:gravity="center" android:textSize="25sp" /> <Spinner android:id="@+id/spinnerVerses" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:gravity="center" android:textSize="25sp" /> <View android:layout_width="0dp" android:layout_height="20dp" android:layout_weight="1" /> <ImageButton android:id="@+id/btnExodus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center|center_vertical" android:layout_marginBottom="2dp" android:layout_marginLeft="0dp" android:layout_marginTop="2dp" android:background="@null" android:gravity="right" android:src="@drawable/next" /> </LinearLayout> 
+1
Mar 02 '16 at 0:28
source share



All Articles