1.概述:TabHost(选项卡组件),可以在一个窗口中显示多个标签栏的内容。主要功能是进行应用程序的分类管理,比如手机的通话记录,未接来电,已接电话。
2.TabHost(选项卡)构成
- TabHost:根布局
- 选项卡整体布局:可设置标签栏和标签页内容的布局位置
- 标签栏(TabWidget):用于实现一个多标签的用户见面,通过将一个复杂的对话框分割成若干个标签页,实现对信息的分类显示和管理
- 标签页内容(FrameLayout):以层的方式,放置每一个标签页显示的内容
3.TabHost类的常用方法
方法 | 作用 |
---|---|
newTabSpec(String tag) | 创建一个标签页 |
addTab (TabHost.TabSpectabSpec) | 添加标签页 |
getCurrentView() | 获取当前的View组件 |
setup() | 初始化TabHost组件 |
setCurrentTab(int index) | 设置当前标签页的编号 |
setCurrentTabByTag(String tag) | 设置当前标签页的编号 |
getTabContentView | 获取标签页内容视图对象,即FrameLayout对象 |
setOnTabChangeListener(TabHost.OnTabChangeListener)) | 注册选项卡中标签改变时的事件监听器 |
4.TabWidget类(标签栏)
方法 | 作用 |
---|---|
TabWidget(Context context) | 创建TabWidget对象 |
addView(View child) | 向TabWidget添加组件 |
getTabCount() | 获取标签个数 |
setEnabled(boolean enabled) | 设置是否启用 |
5.实现选项卡的一般步骤
1) (主布局文件)
首先使用TabHost作为根布局管理器,再设置选项卡的整体布局。在选项卡的整体布局管理器中,再添加标签栏TabWidget和选项卡中标签页内容FrameLayout两个组件
2) (选项卡的标签页内容)
- 方式1:独立创建各个标签页的XML文件
- 方式2:在布局文件中的TabH的FrameLayout组件中,直接设置各个标签页的布局内容(需要定义ID来区别)
3) (控制TabHost组件)
在Activity的onCreate()中,获取并初始化TabHost组件,然后创建设置标签页,最后为TabHost组件添加标签页
方法1:
-
LayoutInflater(布局填充器)
-
方法1不是通过< TabHost >元素的< FrameLayout> 布局管理器中定义组件的形式,此时需要通过LayoutInflater(布局填充器)来实现标签页内容的填充
-
常用方法
方法 | 作用 |
---|---|
public static LayoutInflater from(Context context) | 从指定的上下文中获取LayoutInflater(布局填充器)对象 |
public View inflater(int resource,ViewGroup root) | 按照指定的布局管理器资源ID,向选中标签内容对象填充内容,即向FrameLayout对象填充内容 |
-案例
activity-main
<?xml version="1.0" encoding="utf-8"?>
<TabHost //-选项卡
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tabhost">
<LinearLayout //选项卡整体布局
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget //-标签栏
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@android:id/tabs"/>
<FrameLayout //标签页内容
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/tabcontent"/>
</LinearLayout>
</TabHost>
tab1
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linearlayout1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="宁静致远"
android:layout_marginTop="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="雪儿"
android:layout_marginTop="20dp"/>
</LinearLayout>
tab2
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linearlayout2"
android:orientation="vertical"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小草"
android:drawableLeft="@mipmap/ic_launcher"
android:drawablePadding="20dp"
android:paddingTop="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小花"
android:drawableLeft="@mipmap/ic_launcher_round"
android:drawablePadding="20dp"
android:paddingTop="20dp"/>
</LinearLayout>
mainActivity
package com.example.a5_5;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
public class MainActivity extends AppCompatActivity {
private TabHost tabHost; //1.定义TabHost对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找到TabHost选项卡组件并初始化
tabHost=(TabHost) findViewById(R.id.tabhost);
tabHost.setup();
// 创建LayoutInflater(布局填充器)
LayoutInflater inflater=LayoutInflater.from(this);
//将标签布局文件资源填充到选项卡的FrameLayout对象中
inflater.inflate(R.layout.tab1,tabHost.getTabContentView());
inflater.inflate(R.layout.tab2,tabHost.getTabContentView());
//4. 向选项卡中添加标签页
tabHost.addTab(tabHost.newTabSpec("tag1").setIndicator("未接来电").setContent(R.id.linearlayout1));
tabHost.addTab(tabHost.newTabSpec("tag2").setIndicator("已接来电").setContent(R.id.linearlayout2));
}
}
方法二
- TabHost.TabSpec类(标签页)
TabHost.TabSpec类是TabHost的内部类,,是标签页类。需要通过TableHost类的newTabSpec(String tag)方法创建标签页对象 - 常用方法
方法 | 作用 |
---|---|
public TabHost.TabSpec.setIndicator(CharSpeqence label | 设置标签页的标签 |
public TabHos.TabSpec setContent(int viewid) | 按照组件ID的内容设置标签页的内容 |
- 例子
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost //--选项卡
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tabhost">
<RelativeLayout //--整体布局
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget //--标签
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@android:id/tabs"
android:layout_alignParentBottom="true"/>
<FrameLayout //--页面布局
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/tabcontent">
<LinearLayout //--编辑框
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tab_edit"
android:layout_margin="10dp"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入搜索内容"
android:id="@+id/edit"
android:textSize="18px"/>
<Button
android:id="@+id/but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tab_clock"
android:orientation="vertical">
<AnalogClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/analogclock"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/radio"
android:orientation="vertical">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/radiogroup"
android:orientation="vertical">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:id="@+id/male"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:id="@+id/female"/>
</RadioGroup>
</LinearLayout>
</FrameLayout>
</RelativeLayout>
</TabHost>
package com.example.a5_6;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TabHost;
public class MainActivity extends AppCompatActivity {
private TabHost tabHost; //--定义自变量
private int[] arrayList=new int[]{R.id.edit,R.id.analogclock,R.id.radiogroup};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//-找到组件,初始化
tabHost=(TabHost) findViewById(R.id.tabhost);
tabHost.setup();
for (int i=0;i<arrayList.length;i++){ //--建立循环
TabHost.TabSpec mytab=tabHost.newTabSpec("tag"+i);//--新建标签
mytab.setIndicator("标签-"+i);//标签标记
mytab.setContent(arrayList[i]); //为标签添加内容
tabHost.addTab(mytab);//将标签添加到选项卡
}
tabHost.setCurrentTab(0); //默认显示第0个
}
}