編輯:Android 開發實例
本文示範一個常見的介面結構:外層版面固定不動,其中某塊區域可以動態替換內容。實作效果如下:



整個畫面由三個 layout 檔組成。
<?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="horizontal" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@android:color/black" >
<Button android:id="@+id/btnSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="switch" />
<Button android:id="@+id/btnScreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="screen" />
</LinearLayout>
<LinearLayout
android:id="@+id/frameSwitch"
android:layout_width="160dp"
android:layout_height="fill_parent"
android:background="@android:color/white" >
</LinearLayout>
</LinearLayout>
關鍵是 id 為 frameSwitch 的那個空 LinearLayout——它本身就是一個容器佔位,內容稍後由程式碼填入。
<?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:background="@color/yellow"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this is linearLayout one" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this is linearLayout two" />
<Button android:id="@+id/btnSecond"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btnSecond" />
</LinearLayout>
public class ZzzAndroidActivity extends Activity {
private LinearLayout frameSwitch;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
frameSwitch = (LinearLayout) findViewById(R.id.frameSwitch);
Button btnSwitch = (Button) findViewById(R.id.btnSwitch);
btnSwitch.setOnClickListener(new OnClickListener() {
boolean boo = false;
@Override
public void onClick(View v) {
boo = !boo;
if (boo) {
getViewOne();
} else {
getViewSecond();
}
}
});
/* 是否全屏 */
Button btnScreen = (Button) findViewById(R.id.btnScreen);
btnScreen.setOnClickListener(new OnClickListener() {
boolean isScreen = false;
@Override
public void onClick(View v) {
isScreen = !isScreen;
if (isScreen) {
frameSwitch.setVisibility(android.view.View.GONE);
} else {
frameSwitch.setVisibility(android.view.View.VISIBLE);
}
}
});
}
public void getViewOne() {
View viewOne = getLayoutInflater().inflate(R.layout.one, null);
frameSwitch.removeAllViews();
frameSwitch.addView(viewOne, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
}
public void getViewSecond() {
View viewSecond = getLayoutInflater().inflate(R.layout.two, null);
Button btn = (Button) viewSecond.findViewById(R.id.btnSecond);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(ZzzAndroidActivity.this, "hello world", Toast.LENGTH_LONG).show();
}
});
frameSwitch.removeAllViews();
frameSwitch.addView(viewSecond, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
}
}
切換的核心只有兩行:removeAllViews() 清掉容器現有內容,再用 addView() 放入新 inflate 出來的 View。用 getLayoutInflater().inflate() 而非直接 new,是為了沿用 XML 中定義好的版面與樣式。
另一個細節是子 View 的事件綁定必須在 addView 之前完成——因為 findViewById 只能在 View 已經 inflate 出來之後呼叫。範例中 second 頁的按鈕就是先取得、先綁監聽,再放進容器的。
最後,這種「破壞式替換」每次都會重建整棵 View 樹,若切換頻繁、內容複雜,會有效能與狀態保存的問題。實務上更推薦用 Fragment 搭配 FragmentTransaction.replace(),或直接以 ViewPager/ViewFlipper 處理,框架已幫你管好生命週期與返回堆疊。本文的做法勝在直觀,適合理解原理與簡單場景。希望對大家的 Android 開發有所幫助。
Android 手機衛士(一):實作 Splash 啟動畫面
什麼是 Splash? Splash 是應用程式啟動時先出現的簡短畫面,通常用來展示品牌 LOGO、應用名稱、版本資訊或初始化狀態。手機遊戲與第三方 App 常見這種設計
Android MediaPlayer 影音播放開發指南
Android 提供了多種控制音訊與影片播放的方式,其中最常用的一種,就是透過名為 MediaPlayer 的類別。 Android 的 MediaPlayer 類別能存
Android BroadcastReceiver 使用解析
BroadcastReceiver 是 Android 四大元件之一,用於接收系統或應用送出的廣播 Intent。它常用於監聽網路變化、電量、時區、語言切換等事件。 基本
Android 登入畫面設計與錯誤次數限制
登入畫面是許多 App 的第一道防線。設計時除了帳號與密碼欄位,也要考慮錯誤次數限制,避免有心人士不斷嘗試。 版面設計 使用兩個 EditText 分別輸入使用者名稱與密