1
|
public TabHost(Context context)
|
构造
|
创建TabHost类对象
|
2
|
public void addTab(TabHost.TabSpec tabSpec)
|
普通
|
添加一个Tab
|
3
|
public TabHost.TabSpec newTabSpec(String tag)
|
普通
|
创建一个TabHost.TabSpec对象
|
4
|
public View getCurrentView()
|
普通
|
取得当前的View对象
|
5
|
public void setup()
|
普通
|
建立TabHost对象
|
6
|
public void setCurrentTab(int index)
|
普通
|
设置当前显示的Tab编号
|
7
|
public void setCurrentTabByTag(String tag)
|
普通
|
设置当前显示的Tab名称
|
8
|
public FrameLayout getTabContentView()
|
普通
|
返回标签容器
|
9
|
public void setOnTabChangedListener
(TabHost.OnTabChangeListener l)
|
普通
|
设置标签改变时触发
|
<?
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" > <LinearLayout android:layout_marginTop="50dp" android:id="@+id/login" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TableRow android:id="@+id/tableRow1" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="账号" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:textSize="25sp" /> </TableRow> <TableRow android:id="@+id/tableRow2" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="password" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:textSize="25sp" /> </TableRow> <TableRow android:id="@+id/tableRow3" android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="取消" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="登录" /> </TableRow> </LinearLayout> <LinearLayout android:layout_marginTop="50dp" android:id="@+id/image" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/a2" /> </LinearLayout> <LinearLayout android:layout_marginTop="50dp" android:id="@+id/timer" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TimePicker android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_marginTop="50dp" android:id="@+id/timer1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TimePicker android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_marginTop="50dp" android:id="@+id/timer2" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TimePicker android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
package com.example.tabhost;
import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class MainActivity extends TabActivity {
private TabHost tabHost;//初始化TabHost组件
private int reslayout[] = { R.id.login, R.id.image, R.id.timer , R.id.timer1,R.id.timer2};//设置相应的额xml文件
private int images[]={R.drawable.cart,R.drawable.cloud,R.drawable.comment,R.drawable.gear,R.drawable.joystick};//设置显示的标题文件
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.tabHost = super.getTabHost();//实例化TabHost组件
// 取得LayoutInflater对象
LayoutInflater.from(this).inflate(R.layout.linearlayout,//制定布局
this.tabHost.getTabContentView(),//制定标签添加的容器
true);
for (int i = 0; i < reslayout.length; i++) {
TabSpec myTab = tabHost.newTabSpec("tab" + i);// 定义TabSpec
myTab.setIndicator(null,getResources().getDrawable(images[i])) ; // 设置标签
myTab.setContent(this.reslayout[i]) ; // 设置显示的组件
this.tabHost.addTab(myTab) ;
}
}
}
效果图例如以下
使用配置文件设置。
<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="25sp" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="password" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="25sp" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="取消" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="登录" />
</TableRow>
</LinearLayout>
<LinearLayout
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/a2" />
</LinearLayout>
<LinearLayout
android:id="@+id/timer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TimePicker
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/timer1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TimePicker
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/timer2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TimePicker
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
</RelativeLayout >
</TabHost>
JAVA文件配置
package com.example.tabhost;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class MainActivity extends Activity { // 直接继承Activity
private TabHost myTabHost; // 定义TabHost
private int[] layRes = { R.id.login, R.id.image
, R.id.timer, R.id.timer1, R.id.timer2 }; // 定义内嵌布局管理器ID
private int images[]={R.drawable.cart,R.drawable.cloud,R.drawable.comment,R.drawable.gear,R.drawable.joystick};//标题图片数据
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main) ; // 调用默认布局管理器
this.myTabHost = (TabHost) super.findViewById(R.id.tabhost); // 取得TabHost对象
this.myTabHost.setup() ; // 建立TabHost对象
for (int x = 0; x < this.layRes.length; x++) { // 循环取出全部布局标记
TabSpec myTab = myTabHost.newTabSpec("tab" + x); // 定义TabSpec
myTab.setIndicator(null,getResources().getDrawable(images[x])) ; // 设置标签文字
myTab.setContent(this.layRes[x]) ; // 设置显示的组件
this.myTabHost.addTab(myTab) ; // 添加标签
}
this.myTabHost.setCurrentTab(0) ; // 设置開始索引
}
}