读书人

TabHost两中筑标签方法

发布时间: 2013-10-18 20:53:13 作者: rapoo

TabHost两中建标签方法

一、在activity中继承TabActivity,然后从布局文件中加载各个tab的内容即可。

例如java代码

  1. private TabHost myTabHost;
  2. @Override
  3. public void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. TabHost host = getTabHost();
  6. LayoutInflater.from(this).inflate(R.layout.tabs,
  7. host.getTabContentView(), true);
  8. host.addTab(host
  9. .newTabSpec("t1")
  10. .setIndicator("t1", getResources().getDrawable(R.drawable.icon))
  11. .setContent(R.id.sll01));
  12. host.addTab(host.newTabSpec("t2").setIndicator("t2")
  13. .setContent(R.id.sll02));
  14. host.addTab(host.newTabSpec("t3").setIndicator("t3")
  15. .setContent(R.id.sll03));
  16. setContentView(host); }
布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <LinearLayout android:id="@+id/sll01" android:layout_width="fill_parent"
  6. android:layout_height="fill_parent" android:gravity="center_horizontal"
  7. android:orientation="vertical">
  8. <ImageView android:src="@drawable/t1" android:layout_width="fill_parent"
  9. android:layout_height="fill_parent" android:id="@+id/v1"></ImageView>
  10. </LinearLayout>
  11. <LinearLayout android:id="@+id/sll02" android:layout_width="fill_parent"
  12. android:layout_height="fill_parent" android:gravity="center_horizontal"
  13. android:orientation="vertical">
  14. <ImageView android:src="@drawable/t2" android:layout_width="fill_parent"
  15. android:layout_height="fill_parent" android:id="@+id/v2"></ImageView>
  16. </LinearLayout>
  17. <LinearLayout android:id="@+id/sll03" android:layout_width="fill_parent"
  18. android:layout_height="fill_parent" android:gravity="center_horizontal"
  19. android:orientation="vertical">
  20. <ImageView android:src="@drawable/t3" android:layout_width="fill_parent"
  21. android:layout_height="fill_parent" android:id="@+id/v3"></ImageView>
  22. </LinearLayout>
  23. </FrameLayout>
二、直接继承activity

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代码 TabHost两中筑标签方法
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/tabhost" android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <LinearLayout android:orientation="vertical"
  6. android:layout_width="fill_parent" android:layout_height="fill_parent">
  7. <TabWidget android:id="@android:id/tabs"
  8. android:layout_width="fill_parent" android:layout_height="wrap_content" />
  9. <FrameLayout android:id="@android:id/tabcontent"
  10. android:layout_width="fill_parent" android:layout_height="fill_parent">
  11. <LinearLayout android:id="@+id/sll01"
  12. android:layout_width="fill_parent"
  13. android:layout_height="fill_parent"
  14. android:gravity="center_horizontal"
  15. android:orientation="vertical">
  16. <ImageView android:src="@drawable/t1"
  17. android:layout_width="fill_parent" android:layout_height="fill_parent"
  18. android:id="@+id/v1"></ImageView>
  19. </LinearLayout>
  20. <LinearLayout android:id="@+id/sll02"
  21. android:layout_width="fill_parent" android:layout_height="fill_parent"
  22. android:gravity="center_horizontal" android:orientation="vertical">
  23. <ImageView android:src="@drawable/t2"
  24. android:layout_width="fill_parent" android:layout_height="fill_parent"
  25. android:id="@+id/v2"></ImageView>
  26. </LinearLayout>
  27. <LinearLayout android:id="@+id/sll03"
  28. android:layout_width="fill_parent" android:layout_height="fill_parent"
  29. android:gravity="center_horizontal" android:orientation="vertical">
  30. <ImageView android:src="@drawable/t3"
  31. android:layout_width="fill_parent" android:layout_height="fill_parent"
  32. android:id="@+id/v3"></ImageView>
  33. </LinearLayout>
  34. </FrameLayout>
  35. </LinearLayout>
  36. </TabHost>
java代码

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.tabs);
  5. TabHost host = (TabHost) findViewById(R.id.tabhost);
  6. host.setup();
  7. host.addTab(host
  8. .newTabSpec("t1")
  9. .setIndicator("t1", getResources().getDrawable(R.drawable.icon))
  10. .setContent(R.id.sll01));
  11. host.addTab(host.newTabSpec("t2").setIndicator("t2")
  12. .setContent(R.id.sll02));
  13. host.addTab(host.newTabSpec("t3").setIndicator("t3")
  14. .setContent(R.id.sll03));

读书人网 >操作系统

热点推荐