实例代码:
package com.data.hy.combination.component;
import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
import android.widget.Toast;
/**
* 创建横向选项卡
*/
public class TabHostHorizontalActivity extends TabActivity implements TabHost.OnTabChangeListener {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
TabHost myTabHost =getTabHost();
//加载TabHost布局
LayoutInflater.from(this).inflate(R.layout.activity_tab_host_horizontal,
myTabHost.getTabContentView(), true);
//添加第一个标签页
myTabHost.addTab(myTabHost.newTabSpec("tab1")
.setIndicator("黎明之战").setContent(R.id.tab01));
//添加第二个标签页
myTabHost.addTab(myTabHost.newTabSpec("tab2")
.setIndicator("权力之眼").setContent(R.id.tab02));
//添加第三个标签页
myTabHost.addTab(myTabHost.newTabSpec("tab3")
.setIndicator("圆梦巨人").setContent(R.id.tab03));
}
@Override
public void onTabChanged(String tabId) {
Toast.makeText(this,"第几 "+tabId,Toast.LENGTH_SHORT).show();
}
}
activity_tab_host_horizontal.xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.data.hy.combination.component.TabHostHorizontalActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabHost
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 定义第一个选项卡的内容 -->
<LinearLayout
android:id="@+id/tab01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#121212"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="50dp"
android:gravity="center"
android:text="这是使用TabHost创建的第一选项卡页" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="50dp"
android:src="@mipmap/myimage1" />
</LinearLayout>
<!-- 定义第二个选项卡的内容 -->
<LinearLayout
android:id="@+id/tab02"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#aabbaa"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="50dp"
android:gravity="center"
android:text="这是使用TabHost创建的第二选项卡页" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="50dp"
android:src="@mipmap/myimage2" />
</LinearLayout>
<!-- 定义第三个选项卡的内容 -->
<LinearLayout
android:id="@+id/tab03"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#aabbaa"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="50dp"
android:gravity="center"
android:text="这是使用TabHost创建的第三选项卡页" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="50dp"
android:src="@mipmap/myimage3" />
</LinearLayout>
</TabHost>
</LinearLayout>
</RelativeLayout>
一、TabHost概述
TabHost是一种非常实用的组件,TabHost可以很方便地在窗口上放置多个标签页,每个标签页相当于获得了一个与外部容器相同大小的组件摆放区域。通过这种方式,就可以在一个容器里放置更多组件。
TabHost是整个Tab的容器,包含TabWidget和FrameLayout两个部分,TabWidget是每个Tab的标签,FrameLayout是Tab内容。与TabHost结合使用的有如下2个组件。
TabWidget:代表选项卡的标题条。
TabSpec:代表选项卡的一个Tab页面。
TabHost仅仅是一个简单的容器,它提供了如下两个方法来创建、添加标签页。
newTabSpec(String tag):创建选项卡。
addTab(TabHost.TabSpec tabSpec):添加选项卡。
实现TabHost有两种方式:
直接让一个Activity程序继承TabActivity类。
定义XML布局文件利用findViewById()方法取得TagHost组件,通过setup()方法实例化并进行配置
二、继承TabActivity实现
通过继承TabActivity类,使用TabHost的一般步骤如下。
在界面布局文件中定义TabHost组件,并为该组件定义该选项卡的内容。
Activity 应该继承 TabActivity。
调用 TabActivity 的 getTabHost()方法获取 TabHost 对象。
通过TabHost对象的方法来创建、添加选项卡。
除此之外,TabHost还提供了一些方法来获取当前选项卡。如果程序需要监控TabHost里当前标签页的改变,则可以为它设置 TabHost.OnTabChangeListener 监听器。
接下来通过一个简单的示例程序来学习TabHost的使用。
继续使用WidgetSample工程的advancedviewsample模块,在app/main/res/layout/目录下创建tabhosttab_layout.xml文件,在其中填充如下代码片段:
<?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/widget_layout_red"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
android:orientation="vertical"/>
<LinearLayout
android:id="@+id/widget_layout_green"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ff00"
android:orientation="vertical"/>
<LinearLayout
android:id="@+id/widget_layout_blue"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0000ff"
android:orientation="vertical"/>
</FrameLayout>
</LinearLayout>
</TabHost>
请注意上面的布局文件中代码,从上面的布局文件可以发现,TabHost容器内部需要组合两个组件:TabWidget和FrameLayout,其中TabWidget用于定义选项卡的标题条, FrameLayout则用于层叠组合多个选项页面。不仅如此,上面的布局文件中这三个组件的 ID也有要求。
TabHost 的 ID 应该为@android:id/tabhost。
TabWidget 的 ID 应该为@android:id/tabs。
FrameLayout 的 ID 应该为@android:id/tabcontent。
上面这三个ID并不是开发者自己定义的,而是引用了 Android系统已有的ID。
接下来主程序即可加载该布局资源,并将布局文件中的三个Tab页面添加到该TabHost 容器中。新建TabHostTabActivity.java文件,加载上面新建的布局文件,具体代码如下:
package com.data.hy.combination.component;
import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;
/**
* @description: TabActivity选项活动卡
* author: yh
*
*/
public class TabHostTabActivity extends TabActivity {
private TabHost mTabHost = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabhosttab_layout);
// 从TabActivity上面获取放置Tab的TabHost
mTabHost = getTabHost();
// 添加第一个标签
TabHost.TabSpec tab1 = mTabHost.newTabSpec("0");
tab1.setIndicator("红色");
tab1.setContent(R.id.widget_layout_red);
mTabHost.addTab(tab1);
// 添加第二个标签
mTabHost.addTab(mTabHost
// 创建新标签
.newTabSpec("1")
// 设置标签标题
.setIndicator("绿色")
// 设置该标签的布局内容
.setContent(R.id.widget_layout_green));
// 添加第三个标签
mTabHost.addTab(mTabHost.newTabSpec("2")
.setIndicator("蓝色")
.setContent(R.id.widget_layout_blue));
}
}
上面程序中的代码就是为TabHost创建并添加Tab页面的代码。上面的程序一共添加了三个标签页,用了两种方式来实现。
修改启动的Activity,运行程序,可以看到下图所示界面效果。
点击标签,可以切换显示的内容。
上面的程序调用了 TabHost.TabSpec对象的 setContent(int viewld)方法来设置标签页内容。
除此之外,还可调用setContent(Intent intent)方法来设置标签页内容,Intent还可用于启动其他Activity——这意味着TabHost.TabSpec可直接装载另一个Activity。
三、继承Activity实现
与继承TabActivity实现TabHost大体步骤差不多,唯一的区别就是没有TabActivity系统封装,必须由开发者自己获取TabHost组件而已。
在上面的示例基础上进行修改,创建activity_tab_host_fragment.xml文件,在其中填充如下代码片段:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mytabhost"
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/widget_layout_red"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#aabb11"
android:orientation="vertical"/>
<LinearLayout
android:id="@+id/widget_layout_green"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ff00"
android:orientation="vertical"/>
<LinearLayout
android:id="@+id/widget_layout_blue"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0000ff"
android:orientation="vertical"/>
<LinearLayout
android:id="@+id/widget_layout_aa"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#11aaff"
android:orientation="vertical"/>
<LinearLayout
android:id="@+id/widget_layout_bb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#22ffff"
android:orientation="vertical"/>
<LinearLayout
android:id="@+id/widget_layout_cc"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#99ffff"
android:orientation="vertical"/>
</FrameLayout>
</LinearLayout>
</TabHost>
从上述代码可以发现,除了TabHost 的id可以自定义外,TabWidget和FrameLayout仍然必须为系统的ID。
界面交互代码稍微有所不同,新建TabHostLayoutHorizontalActivity.java文件,加载上面新建的布局文件,具体代码如下:
package com.data.hy.combination.component;
import android.os.Bundle;
import android.widget.TabHost;
import androidx.appcompat.app.AppCompatActivity;
/**
* 通过在布局文件的TabHost标签创建选项卡视图
*/
public class TabHostLayoutHorizontalActivity extends AppCompatActivity {
private TabHost mTabHost = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_host_fragment);
// 获取TabHost组件
mTabHost = findViewById(R.id.mytabhost);
//调用 TabHost.setup()
mTabHost.setup();
// 添加三个标签
mTabHost.addTab(mTabHost.newTabSpec("0").setIndicator("浅绿").setContent(R.id.widget_layout_red));;
mTabHost.addTab(mTabHost.newTabSpec("1").setIndicator("绿色").setContent(R.id.widget_layout_green));
mTabHost.addTab(mTabHost.newTabSpec("2").setIndicator("蓝色").setContent(R.id.widget_layout_blue));
mTabHost.addTab(mTabHost.newTabSpec("3").setIndicator("浅蓝").setContent(R.id.widget_layout_aa));
mTabHost.addTab(mTabHost.newTabSpec("4").setIndicator("灰红").setContent(R.id.widget_layout_bb));
mTabHost.addTab(mTabHost.newTabSpec("5").setIndicator("洋红").setContent(R.id.widget_layout_cc));
}
}
效果图:
会发现除了查找我们自定义的TabHost组件外,还多了一条语句setup()来加载TabHost,否则程序会报错。
运行修改后的程序,最终效果同继承TabActivity一样。
有木有发现这个界面很不美观,所以在实际开发中经常会借用RadioButton来定制TabHost。
其实TabHost组件在安卓4.0之后已经被废弃了,建议使用Fragment组件来代替它。由于其设计违反了Activity单一窗口原则,它可以同时加载多个Activity,然后再它们之间进行来回切换;另外有个很致命的问题就是当点击别的选项时,按下Back后退键,它会使整个应用程序都退出,而不是切换到前一个选项卡,虽然可以在主程序里覆写OnKeyDown这个方法,但这样就会导致每一次按下Back后退键都只能回到第一个选项菜单。