Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 實現局部界面動態切換:固定骨架 + 可替換區域

編輯:Android 開發實例

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

整個畫面由三個 layout 檔組成。

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="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——它本身就是一個容器佔位,內容稍後由程式碼填入。

one.xml 與 two.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: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> 

Java 程式碼

 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 開發有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved