<?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="fill_parent"
android:orientation="vertical">
<!-- The layout_gravity example -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- notice that the textview's width is as same as its content,
In this way, we can set "layout_gravity" property
to let the textview to "left,right,top,buttom" of its parent
-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/layout_gravity_test"
android:layout_gravity="right"
android:background="#CCCCCC"
android:textColor="#000000" />
</LinearLayout>
<!-- The gravity example -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="20dp">
<!-- notice that the textview's width is match the screen,
set "gravity property" is set the content of the textview to "left,right,top,bottom"
-->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/gravity_test"
android:gravity="right"
android:background="#CCCCCC"
android:textColor="#000000" />
</LinearLayout>
<!-- The "layout_gravity" property does not work example -->
<!-- 1.When we set the LinearLayout's orientation to horizontal,
and set layout_gravity to left or right,in this way,
"layout_gravity" property will not work.
2.Also,when we set the LinearLayout's orientation to vertical,
set layout_gravity to top or bottom will not work
-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/not_work"
android:layout_gravity="right"
android:background="#CCCCCC"
android:textColor="#000000" />
</LinearLayout>
<!-- How to solve this problem and not change the LinearLayout's orientation -->
<!-- wrap the TextView with a LinearLayout -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="#CCCCCC"
android:text="@string/solve_problem"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World!</string>
<string name="app_name">AndroidLayout</string>
<string name="not_work">layout gravity does not work!</string>
<string name="gravity_test">gravity test!</string>
<string name="layout_gravity_test">layout gravity test!</string>
<string name="solve_problem">layout gravity work again</string>
</resources>