java代碼
package com.example.heuvan_nb_set;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 獲取該Activity裏面的TabHost組件
// 方法1使用:
// TabHost tabHost = getTabHost();
// 方法1使用:
// LayoutInflater.from(this).inflate(R.layout.main_tab, tabHost.getTabContentView(), true);
// 方法2、3使用
TabHost tabHost = (TabHost)findViewById(R.id.tabhost);
tabHost.setup();
// 方法3使用,動態載入xml,不需要Activity
LayoutInflater.from(this).inflate(R.layout.tab1, tabHost.getTabContentView());
LayoutInflater.from(this).inflate(R.layout.tab2, tabHost.getTabContentView());
LayoutInflater.from(this).inflate(R.layout.tab3, tabHost.getTabContentView());
// 創建第一個Tab頁
/*TabHost.TabSpec tab1 = tabHost.newTabSpec("tab1")
.setIndicator("標籤頁一") // 設置標題
.setContent(R.id.tab01); //設置內容
// 添加第一個標籤頁
tabHost.addTab(tab1);
TabHost.TabSpec tab2 = tabHost.newTabSpec("tab2")
.setIndicator("標籤頁二")
.setContent(R.id.tab02);
// 添加第二個標籤頁
tabHost.addTab(tab2);
TabHost.TabSpec tab3 = tabHost.newTabSpec("tab3")
.setIndicator("標籤頁三")
.setContent(R.id.tab03);
// 添加第三個標籤頁
tabHost.addTab(tab3);*/
/* 以上創建和添加標籤頁也可以用如下代碼實現 */
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("標籤頁一").setContent(R.id.tab01));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("標籤頁二").setContent(R.id.tab02));
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("標籤頁三").setContent(R.id.tab03));
//標籤切換事件處理,setOnTabChangedListener
tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener(){
@Override
// tabId是newTabSpec第一個參數設置的tab頁名,並不是layout裏面的標識符id
public void onTabChanged(String tabId) {
if (tabId.equals("tab1")) { //第一個標籤
Toast.makeText(MainActivity.this, "點擊標籤頁一", Toast.LENGTH_SHORT).show();
}
if (tabId.equals("tab2")) { //第二個標籤
Toast.makeText(MainActivity.this, "點擊標籤頁二", Toast.LENGTH_SHORT).show();
}
if (tabId.equals("tab3")) { //第三個標籤
Toast.makeText(MainActivity.this, "點擊標籤頁三", Toast.LENGTH_SHORT).show();
}
}
});
}
}
activity_main.xml代碼
<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<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="match_parent">
</FrameLayout>
</LinearLayout>
</TabHost>
tab1代碼
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab01"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一個標籤頁的第1個TextView組件"
android:textSize="8pt" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一個標籤頁的第2個TextView組件"
android:textSize="8pt" />
</LinearLayout>
tab2代碼
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab02"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二個標籤頁的第1個TextView組件"
android:textSize="8pt" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二個標籤頁的第2個TextView組件"
android:textSize="8pt" />
</LinearLayout>
tab3代碼
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab03"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第三個標籤頁的第1個TextView組件"
android:textSize="8pt" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第三個標籤頁的第2個TextView組件"
android:textSize="8pt" />
</LinearLayout>
顯示效果