在開發較複雜的介面時,特別是平板電腦的頁面,布局往往比手機來得複雜,這時就會需要用到嵌套布局。而為了達到某些效果,我們常需要讓局部頁面能夠動態變化,最典型的例子就是在同一個頁面裡使用多個 ViewPager。當這些 ViewPager 所在的頁面本身也是動態產生的時候,就必須實作多層 LinearLayout 的嵌套。舉例來說,一個標籤頁的頭部是靜態的、內容部分會動態變化,而每個內容區塊又各自需要再動態變化,實作時就得透過多次 addView 來完成。
以下是一個簡化的案例(此範例只示範多層嵌套的加入方式;若要實現動態切換的效果,只要透過條件控制每次加入的內容即可)。
PadTestActivity.java
package com.devin; import Android.app.Activity; import android.os.Bundle; import android.view.LayoutInflater; import android.widget.LinearLayout; public class PadTestActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LayoutInflater inflater=getLayoutInflater(); //Add first page LinearLayout myFirst = (LinearLayout) inflater.inflate( R.layout.first, null).findViewById(R.id.myFirst); LinearLayout layoutMain = (LinearLayout)findViewById(R.id.LayoutMain); layoutMain.removeAllViews(); layoutMain.addView(myFirst); //Show the page first //Add second page LinearLayout mySecond = (LinearLayout) inflater.inflate( R.layout.second, null).findViewById(R.id.mySecond); LinearLayout layoutFirst = (LinearLayout) findViewById(R.id.LayoutFirst); layoutFirst.addView(mySecond); //Add third page LinearLayout myThird = (LinearLayout) inflater.inflate( R.layout.third, null).findViewById(R.id.myThird); LinearLayout layoutSecond = (LinearLayout) findViewById(R.id.LayoutSecond); layoutSecond.addView(myThird); } }
以下是各層的布局檔。
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Main Page" /> <LinearLayout android:id="@+id/LayoutMain" android:layout_width="fill_parent" android:layout_height="fill_parent" > </LinearLayout> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Main Page" /> </LinearLayout>
first.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:id="@+id/myFirst" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="First Page" /> <LinearLayout android:id="@+id/LayoutFirst" android:layout_width="fill_parent" android:layout_height="fill_parent" > </LinearLayout> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="First Page" /> </LinearLayout>
second.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:id="@+id/mySecond" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Second Page" /> <LinearLayout android:id="@+id/LayoutSecond" android:layout_width="fill_parent" android:layout_height="fill_parent" > </LinearLayout> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Second Page" /> </LinearLayout>
third.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:id="@+id/myThird" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Third Page" /> <LinearLayout android:id="@+id/LayoutThird" android:layout_width="fill_parent" android:layout_height="fill_parent" > </LinearLayout> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Third Page" /> </LinearLayout>
更多 Android 相關資訊見Android 專題頁面 https://www.linuxidc.com/topicnews.aspx?tid=11