一、TabHost簡介
TabHost是Android中的一個視圖容器,用於展示多個界面,以便用戶可以輕鬆地在它們之間進行切換。TabHost通常具有標籤頁和內容兩個部分,標籤頁用來展示各個頁面的標題,內容則用來展示各個頁面的具體內容。
在TabHost中,標籤頁和內容部分是通過嵌套子控件的形式實現的。每個標籤頁對應一個內容視圖。TabHost通過管理這兩個部分的關係,實現頁面切換和展示。
二、實現方式
TabHost的實現可以通過布局文件或代碼動態添加兩種方式進行。
1. 布局文件實現
在布局文件中定義TabHost,使用<TabWidget>子標籤定義標籤頁,使用<FrameLayout>子標籤定義內容部分,從而實現各標籤發生切換時對應內容切換的效果。
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="標籤頁1"/>
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="標籤頁2"/>
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="標籤頁3"/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
2. 代碼動態添加實現
通過代碼動態添加實現TabHost,需要創建TabHost實例,設置其布局參數,創建TabWidget和FrameLayout等子控件,並將其添加至TabHost實例中。同時,需要在相應的控件中添加標籤頁和內容部分,實現頁面的切換。
//創建TabHost
TabHost tabHost = new TabHost(this);
tabHost.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
//創建TabWidget
TabWidget tabWidget = new TabWidget(this);
tabWidget.setId(android.R.id.tabs);
tabHost.addView(tabWidget);
//創建FrameLayout
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.setId(android.R.id.tabcontent);
tabHost.addView(frameLayout);
//創建標籤頁1
LinearLayout tab1 = new LinearLayout(this);
tab1.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
TextView textView1 = new TextView(this);
textView1.setText("標籤頁1");
tab1.addView(textView1);
//創建標籤頁2
LinearLayout tab2 = new LinearLayout(this);
tab2.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
TextView textView2 = new TextView(this);
textView2.setText("標籤頁2");
tab2.addView(textView2);
//創建標籤頁3
LinearLayout tab3 = new LinearLayout(this);
tab3.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
TextView textView3 = new TextView(this);
textView3.setText("標籤頁3");
tab3.addView(textView3);
//將標籤頁添加至FrameLayout中
frameLayout.addView(tab1);
frameLayout.addView(tab2);
frameLayout.addView(tab3);
//將標籤頁和內容部分綁定
tabHost.setup();
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("標籤頁1").setContent(tab1.getId()));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("標籤頁2").setContent(tab2.getId()));
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("標籤頁3").setContent(tab3.getId()));
//將TabHost添加至指定父控件中
LinearLayout parentLayout = (LinearLayout) findViewById(R.id.parent_layout);
parentLayout.addView(tabHost);
三、效果展示
通過以上的方式,TabHost可以實現如下效果:
四、總結
TabHost是Android中用於展示多個頁面切換的一種視圖容器。TabHost可以通過布局文件或代碼動態添加兩種方式進行實現。在布局文件中實現時,需要藉助TabWidget和FrameLayout兩種控件,在代碼實現時需要分別創建TabHost、TabWidget、FrameLayout等控件,並將它們嵌套在一起實現標籤與對應內容的切換。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/204640.html