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>
显示效果