(1)在Android学习过程中,用户界面设计模块的TabHost是基础且比较重要的一部分(虽然已经过时,但仍然值得了解)。
(2)
1. TabHost常用组件
TabWidget : 该组件就是TabHost标签页中上部 或者 下部的按钮, 可以点击按钮切换选项卡;
TabSpec : 代表了选项卡界面, 添加一个TabSpec即可添加到TabHost中;
-- 创建选项卡 : newTabSpec(String tag), 创建一个选项卡;
-- 添加选项卡 : addTab(tabSpec);
2. TabHost使用步骤
a. 定义布局 : 在XML文件中使用TabHost组件, 并在其中定义一个FrameLayout选项卡内容;
b. 继承TabActivity : 显示选项卡组件的Activity继承TabActivity;
c. 获取组件 : 通过调用getTabHost()方法, 获取TabHost对象;
d. 创建添加选项卡 : 通过TabHost创建添加选项卡;
(3)打开Android studio,建立一个项目名为ClassTabHost的工程。
(4)activity_main.xml代码如下所示(可直接拖拽控件稍作修改):
<?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:orientation="vertical">
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<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:layout_weight="1"
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/mLinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="@+id/mLinearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="@+id/mLinearLayout3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
(5) MainActivity.java代码如下所示:
package com.hbu.classtabhost;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
public class MainActivity extends TabActivity {
//选项卡
private TabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//直接获取到TabHost getTabHost()相对于findViewById
mTabHost = getTabHost();
//初始化
mTabHost.setup();
TabHost.TabSpec tabSpec1 = mTabHost.newTabSpec("tab1").setIndicator("首页").setContent(new Intent(this,Tab1Activity.class));
TabHost.TabSpec tabSpec2 = mTabHost.newTabSpec("tab2").setIndicator("新闻").setContent(new Intent(this,Tab2Activity.class));
TabHost.TabSpec tabSpec3 = mTabHost.newTabSpec("tab3").setIndicator("设置").setContent(new Intent(this,Tab3Activity.class));
mTabHost.addTab(tabSpec1);
mTabHost.addTab(tabSpec2);
mTabHost.addTab(tabSpec3);
}
}
(6)新建Tab1Activity.java,Tab2Activity.java,Tab3Activity.java,activity_tab1.xml,activity_tab2.xml,activity_tab3.xml。
(7)以Tab1Activity.java为例,代码如下所示:
package com.hbu.classtabhost;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
/**
* 首页
*/
public class Tab1Activity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab1);
}
}
(8) 以activity_tab1.xml为例,代码如下所示:
<?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:orientation="vertical"
android:gravity="center">
<TextView
android:text="首页"
android:textSize="35sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
(9)修改样式(style.xml),代码如下所示:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
(10)在AndroidManifest.xml文件里注册Activity,代码如下所示:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hbu.classtabhost">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Tab1Activity"/>
<activity android:name=".Tab2Activity"/>
<activity android:name=".Tab3Activity"/>
</application>
</manifest>
(11)运行结果如下所示: