Tuesday, January 3, 2012

Apply different layout on different orientation, portrait and landscape.

By save another layout in /res/layout-land/ folder, we can apply different layout on different orientation. When the device is hold in portrait, the layout /res/layout/main.xml will be applied. When the device is hold in landscape, /res/layout-land/main.xml will be applied.

/res/layout/main.xml
/res/layout-land/main.xml

Change on the last exercise "App (for Android 3.0) with dual Fragment", modify /res/layout/main.xml and add /res/layout-land/main.xml.

/res/layout/main.xml:
<?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" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="/res/layout/main.xml"/>
<fragment
android:name="com.exercise.AndroidDualFragment.Fragment1"
android:id="@+id/fragment1"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0px" />
<fragment
android:name="com.exercise.AndroidDualFragment.Fragment2"
android:id="@+id/fragment2"
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="0px" />

</LinearLayout>


/res/layout-land/main.xml:
<?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" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="/res/layout-land/main.xml" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<fragment
android:name="com.exercise.AndroidDualFragment.Fragment1"
android:id="@+id/fragment1"
android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent" />
<fragment
android:name="com.exercise.AndroidDualFragment.Fragment2"
android:id="@+id/fragment2"
android:layout_weight="2"
android:layout_width="0px"
android:layout_height="match_parent" />
</LinearLayout>


</LinearLayout>

No comments: