首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 操作系统 >

TabHost两中筑标签方法

2013-10-18 
TabHost两中建标签方法一、在activity中继承TabActivity,然后从布局文件中加载各个tab的内容即可。例如java

TabHost两中建标签方法

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

例如java代码

  1. private TabHost myTabHost;  
  2.   
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.        TabHost host = getTabHost();  
  7.         LayoutInflater.from(this).inflate(R.layout.tabs,  
  8.                 host.getTabContentView(), true);  
  9.   
  10.         host.addTab(host  
  11.                 .newTabSpec("t1")  
  12.                 .setIndicator("t1", getResources().getDrawable(R.drawable.icon))  
  13.                 .setContent(R.id.sll01));  
  14.         host.addTab(host.newTabSpec("t2").setIndicator("t2")  
  15.                 .setContent(R.id.sll02));  
  16.         host.addTab(host.newTabSpec("t3").setIndicator("t3")  
  17.                 .setContent(R.id.sll03));  
  18.         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.   
  12.             <LinearLayout android:id="@+id/sll01"  
  13.                 android:layout_width="fill_parent"   
  14.                 android:layout_height="fill_parent"  
  15.                 android:gravity="center_horizontal"  
  16.                 android:orientation="vertical">  
  17.                 <ImageView android:src="@drawable/t1"  
  18.                     android:layout_width="fill_parent" android:layout_height="fill_parent"  
  19.                     android:id="@+id/v1"></ImageView>  
  20.             </LinearLayout>  
  21.             <LinearLayout android:id="@+id/sll02"  
  22.                 android:layout_width="fill_parent" android:layout_height="fill_parent"  
  23.                 android:gravity="center_horizontal" android:orientation="vertical">  
  24.                 <ImageView android:src="@drawable/t2"  
  25.                     android:layout_width="fill_parent" android:layout_height="fill_parent"  
  26.                     android:id="@+id/v2"></ImageView>  
  27.             </LinearLayout>  
  28.             <LinearLayout android:id="@+id/sll03"  
  29.                 android:layout_width="fill_parent" android:layout_height="fill_parent"  
  30.                 android:gravity="center_horizontal" android:orientation="vertical">  
  31.                 <ImageView android:src="@drawable/t3"  
  32.                     android:layout_width="fill_parent" android:layout_height="fill_parent"  
  33.                     android:id="@+id/v3"></ImageView>  
  34.             </LinearLayout>  
  35.   
  36.         </FrameLayout>  
  37.     </LinearLayout>  
  38. </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));  

热点排行