Android Layout Right Align
The layout is extremely inefficient and bloated. You don't need that many LinearLayout
s. In fact you don't need any 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>
You can do all that by using just one RelativeLayout
(which, btw, don't need android:orientation
parameter). So, instead of having a LinearLayout
, containing a bunch of stuff, 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, there are some XML parameters missing. I was just showing the basic parameters you had to put. You can complete the rest.
If you want to use LinearLayout, you can do alignment with layout_weight
with Space
element.
E.g. following layout places textView
and textView2
next to each other and textView3
will be right-aligned
<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 would set layout_weight
to textView2
. It's just that I like things more separated, plus to demonstrate 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 should (not must though) set layout_width
explicitly as it will be recalculated according to it's weight anyway (same way you should set height in elements of vertical LinearLayout
). For other layout performance tips see Android Layout Tricks series.