TabHost两中建标签方法
一、在activity中继承TabActivity,然后从布局文件中加载各个tab的内容即可。
例如java代码
- private TabHost myTabHost;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- TabHost host = getTabHost();
- LayoutInflater.from(this).inflate(R.layout.tabs,
- host.getTabContentView(), true);
- host.addTab(host
- .newTabSpec("t1")
- .setIndicator("t1", getResources().getDrawable(R.drawable.icon))
- .setContent(R.id.sll01));
- host.addTab(host.newTabSpec("t2").setIndicator("t2")
- .setContent(R.id.sll02));
- host.addTab(host.newTabSpec("t3").setIndicator("t3")
- .setContent(R.id.sll03));
- setContentView(host); }
- <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <LinearLayout android:id="@+id/sll01" android:layout_width="fill_parent"
- android:layout_height="fill_parent" android:gravity="center_horizontal"
- android:orientation="vertical">
- <ImageView android:src="@drawable/t1" android:layout_width="fill_parent"
- android:layout_height="fill_parent" android:id="@+id/v1"></ImageView>
- </LinearLayout>
- <LinearLayout android:id="@+id/sll02" android:layout_width="fill_parent"
- android:layout_height="fill_parent" android:gravity="center_horizontal"
- android:orientation="vertical">
- <ImageView android:src="@drawable/t2" android:layout_width="fill_parent"
- android:layout_height="fill_parent" android:id="@+id/v2"></ImageView>
- </LinearLayout>
- <LinearLayout android:id="@+id/sll03" android:layout_width="fill_parent"
- android:layout_height="fill_parent" android:gravity="center_horizontal"
- android:orientation="vertical">
- <ImageView android:src="@drawable/t3" android:layout_width="fill_parent"
- android:layout_height="fill_parent" android:id="@+id/v3"></ImageView>
- </LinearLayout>
- </FrameLayout>
TabHost是Tab的容器,包括两部分TabWidget和FrameLayout,TabWidget是tab的标签,FrameLayout是tab的内容。
再来说说,选项卡的使用:先来说说,xml布局文件的使用
1-----TabHost必须设置为@android:id/tabHost
2------TabWidget必须将android:id设置为@android:id/tabs
3-------FrameLayout需要将android:id设置为@android:id/tabcontent
4------通过findViewById获得TabHost之后,必须要调用setup方法。
接下来说说Activity的使用。如果想新建一个Activity实现tabhost,必须要继承TabActivity,此后,又是三步走战略。
1-------获得TabHost的对象,
TabHost tabHost = getTabHost();
2-------通过TabHost.TabSpec增加tab的一页,通过setContent()增加内容,通过setIndicator()增加页的标签。
TabHost.TabSpec spec = tabHost.newTabSpec();
spec.setContent(new Intent());
spec.setIndicator("音乐",Resource res);
tabHost.addTab(spec);
3--------通过setCurrentTab(index)指定显示的页,从0开始。
tabHost.setCurrentTab(0);

- <?xml version="1.0" encoding="utf-8"?>
- <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/tabhost" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <LinearLayout 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" />
- <FrameLayout android:id="@android:id/tabcontent"
- android:layout_width="fill_parent" android:layout_height="fill_parent">
- <LinearLayout android:id="@+id/sll01"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:gravity="center_horizontal"
- android:orientation="vertical">
- <ImageView android:src="@drawable/t1"
- android:layout_width="fill_parent" android:layout_height="fill_parent"
- android:id="@+id/v1"></ImageView>
- </LinearLayout>
- <LinearLayout android:id="@+id/sll02"
- android:layout_width="fill_parent" android:layout_height="fill_parent"
- android:gravity="center_horizontal" android:orientation="vertical">
- <ImageView android:src="@drawable/t2"
- android:layout_width="fill_parent" android:layout_height="fill_parent"
- android:id="@+id/v2"></ImageView>
- </LinearLayout>
- <LinearLayout android:id="@+id/sll03"
- android:layout_width="fill_parent" android:layout_height="fill_parent"
- android:gravity="center_horizontal" android:orientation="vertical">
- <ImageView android:src="@drawable/t3"
- android:layout_width="fill_parent" android:layout_height="fill_parent"
- android:id="@+id/v3"></ImageView>
- </LinearLayout>
- </FrameLayout>
- </LinearLayout>
- </TabHost>
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.tabs);
- TabHost host = (TabHost) findViewById(R.id.tabhost);
- host.setup();
- host.addTab(host
- .newTabSpec("t1")
- .setIndicator("t1", getResources().getDrawable(R.drawable.icon))
- .setContent(R.id.sll01));
- host.addTab(host.newTabSpec("t2").setIndicator("t2")
- .setContent(R.id.sll02));
- host.addTab(host.newTabSpec("t3").setIndicator("t3")
- .setContent(R.id.sll03));