TabHost实现界面布局
Tab标签页是界面设计中经常使用的界面控件,可以实现多个分页之间的切换,每个标签页可以显示不同内容。Android系统的拨号程序界面等就是通过TabHost来实现的。
在AndroidSDK3.0中,随着新的UI设计西乡的引入,android.app.Fragment成为一种新的界面设计模式。不在建议使用tabhost,但因旧版本Android系统还有一定的生存周期,且使用TabActivity实现的Tab标签页的方法在android sdk4.0中仍可以正常运行。
Tab标签页的使用首先要设计所有分页的界面布局,在分页设计完成后,使用代码建立Tab标签页,并给每个分页添加表示和标题,最后确定每个分页所显示的界面布局。在设计分页的接卖弄布局时,使用的方法与设计普通用户界面没有什么区别。为了便于可视化和编码,为每个分页建立一个XML文件,用以编辑和保存分页的界面布局。在layout目录下建立三个XML文件,分别为tab1.xml,tab2.xml,tab3.xml,这三个文件分别使用不同的布局方式,并将布局的ID分别定义为layout01.xml,layout02.xml和layout03.xml。
TabActivity已经过期,强制使用会出现大量的警告信息,这里使用@Suppress Warnings("deprecation")
package edu.hrbeu.TabDemo;
import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;
import android.view.LayoutInflater;
@SuppressWarnings("deprecation")
public class TabDemoActivity extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost tabHost = getTabHost()
LayoutInflater.from(this).inflate(R.layout.tab1, tabHost.getTabContentView(),true);
LayoutInflater.from(this).inflate(R.layout.tab2, tabHost.getTabContentView(),true);
LayoutInflater.from(this).inflate(R.layout.tab3, tabHost.getTabContentView(),true);
tabHost.addTab(tabHost.newTabSpec("TAB1")
.setIndicator("线性布局").setContent(R.id.layout01));
tabHost.addTab(tabHost.newTabSpec("TAB2")
.setIndicator("绝对布局").setContent(R.id.layout02));
tabHost.addTab(tabHost.newTabSpec("TAB3")
.setIndicator("相对布局").setContent(R.id.layout03));
}
}
声明TabDemoActivity继承TabActivity,与以往继承Activity不同TabActivity支持内嵌多个Activity或View。
通过getTabHost()函数获得了Tab标签页的容器,用以承载可以点击的Tab标签和分页的界面布局。
通过LayoutInflater将tab1.xml文件中的布局文件转换为Tab标签页可以使用的 View对象。
使用addTab()函数添加了第1个分页,tabHost.newTabSpec("TAB1")表明在代码中建立的tabHost上,添加一个表示为TAB1的Tab分页。使用setIndicator()函数设定分页显示的标题。使用setContent()函数设定分页所关联的界面布局。
-------------------------------------------------------分割线--------------------------------------------------------------------------
在实现Tab标签页时,除了可以将多个Tab分页放置在同一个Activity中,还可以将不同Tab分页加载到不同的Activity上,两种方式在界面显示上是没有区别的,但后一种处理方式,每个Tab分页对应一个Activity,有利于用户对界面控件的管理和控制。
第二种方式比第一种方式在布局目录中(/res/layout)中多了一个main.xml文件,代码目录中增加了Tab1Activity.java、Tab2Activity.java和Tab3Activity.java三个文件。
public class Tab1Activity extends Activity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.tab1);
}
}
将布局目录中的tab1.xml文件中的布局加载到Tab1Activity中。
Tab1Activity.java、Tab2Activity.java和Tab3Activity.java的作用完全相同,分别将tab1xml、tab2.xml和tab3.xml加载到三个不同的Activity。
@SuppressWarnings("deprecation")
public class TabDemo2Activity extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentVeiw(R.layout.main);
TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("TAB1")
.setIndicator("线性布局").setContent(new Intent().setClass(this,Tab1Activity.class)));
tabHost.addTab(tabHost.newTabSpec("TAB2")
.setIndicator("绝对布局").setContent(new Intent().setClass(this,Tab2Activity.class)));
tabHost.addTab(tabHost.newTabSpec("TAB3")
.setIndicator("相对布局").setContent(new Intent().setClass(this,Tab3Activity.class)));
}
}
在第一种方式中,setContent()函数的参数是布局文件,而第二种方式中setContent()的参数是Intent,通过Intent启动Tab1Activity()。这里就是两个势力的明显不同之,TabDemo2示例为每个Tab分页指定了不同的Activity。第13行代码声明了TabDemo2Activity的布局文件main.xml。
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="0dp"
>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="3dp"
/>
</LinearLayout>
</TabHost>
作为TabActivity的布局,必须以TabHost为根元素,同时包含TabWidget元素,和FrameLayout元素。TabWidget承载Tab导航栏,FrameLayout承载Tab页的内容,现在FrameLayout是空的,在程序运行时,TabHost会自动使用Activity填充FrameLayout。
最后,在Androidmainifest.xml文件中添加三个新建Activity的声明。