TabHost 简介

菜鸟学习android之TabHost.查询资料说android有这么个玩意--------选项卡。首先聊聊什么是选项卡,从网上搜索资料显示:一个顶部的按钮(可点击的)的切换卡部分,一个主内容区(上图显示“第二个窗体”字体的)的主显示区。举个简单点的例子吧,大家可以看看android手机上的通讯录,用的就是选项卡,点击顶部的选项卡一,二,三,在显示区会有不同的内容。强烈推荐看看手机上的通讯录。

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

接下来说说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);

以下是代码。
[java]  view plain copy
  1. package cn.com.karl.music;  
  2.   
  3. import android.app.TabActivity;  
  4. import android.content.Intent;  
  5. import android.content.res.Resources;  
  6. import android.os.Bundle;  
  7. import android.view.Window;  
  8. import android.view.WindowManager;  
  9. import android.widget.TabHost;  
  10.   
  11. /** 
  12.  * 选项卡的使用 1 . 继承TabActivity 2、用getTabHost()方法获取TabHost; 3、各Tab内容在布局文件中定义。 
  13.  * 在手机屏幕中,Tab也是比较常用的,通常和List结合,例如我们手机的通信录。下面是Tag的结构。 
  14.  *  
  15.  * TabHost是整个Tab的容器,包括两部分,TabWidget和FrameLayout。TabWidget就是每个tab的标签, 
  16.  * FrameLayout则是tab内容。 
  17.  *  
  18.  * 如果我们使用extends TabAcitivty,如同ListActivity,TabHost必须设置为@android:id/tabhost 
  19.  * TabWidget必须设置android:id为@android:id/tabs 
  20.  * FrameLayout需要设置android:id为@android:id/tabcontent 
  21.  *  
  22.  * @author SYJ 
  23.  *  
  24.  */  
  25. public class MainActivity extends TabActivity {  
  26.     /** Called when the activity is first created. */  
  27.     @Override  
  28.     public void onCreate(Bundle savedInstanceState) {  
  29.         super.onCreate(savedInstanceState);  
  30.         requestWindowFeature(Window.FEATURE_NO_TITLE);// 应用窗体显示状态操作该参数表示无标题  
  31.         this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
  32.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);// 去掉任务栏  
  33.         setContentView(R.layout.main);// 该语句必须在以上两句之后  
  34.   
  35.         Resources res = getResources(); // 得到资源对象  
  36.         //步骤1:获得TabHost的对象,并进行初始化setup()  
  37.         TabHost tabHost = getTabHost(); // 选项卡类似于java中的卡片布局  
  38.         TabHost.TabSpec spec;  
  39.         Intent intent;  
  40.         intent = new Intent().setClass(this, ListActivity.class);// 启动音乐列表界面  
  41.         // 步骤2:通过TabHost.TabSpec增加tab的一页,通过setContent()增加内容,通过setIndicator增加页的标签  
  42.         spec = tabHost.newTabSpec("音乐")  
  43.                 .setIndicator("音乐", res.getDrawable(R.drawable.item))  
  44.                 .setContent(intent);  
  45.         tabHost.addTab(spec);  
  46.   
  47.         intent = new Intent().setClass(this, ArtistsActivity.class);  
  48.         spec = tabHost.newTabSpec("艺术家")  
  49.                 .setIndicator("艺术家", res.getDrawable(R.drawable.artist))  
  50.                 .setContent(intent);  
  51.         tabHost.addTab(spec);  
  52.   
  53.         intent = new Intent().setClass(this, AlbumsActivity.class);  
  54.         spec = tabHost.newTabSpec("专辑")  
  55.                 .setIndicator("专辑", res.getDrawable(R.drawable.album))  
  56.                 .setContent(intent);  
  57.         tabHost.addTab(spec);  
  58.         intent = new Intent().setClass(this, SongsActivity.class);  
  59.         spec = tabHost.newTabSpec("最近播放")  
  60.                 .setIndicator("最近播放", res.getDrawable(R.drawable.album))  
  61.                 .setContent(intent);  
  62.         tabHost.addTab(spec);  
  63.   
  64.         // 步骤3:可通过setCurrentTab(index)指定显示的页,从0开始计算。  
  65.         tabHost.setCurrentTab(0);  
  66.   
  67.     }  
  68. }  


[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@android:id/tabhost"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent" >  
  6.   
  7.     <LinearLayout  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent"  
  10.         android:orientation="vertical"  
  11.         android:padding="5dp" >  
  12.   
  13.         <TabWidget  
  14.             android:id="@android:id/tabs"  
  15.             android:layout_width="fill_parent"  
  16.             android:layout_height="wrap_content" />  
  17.   
  18.         <FrameLayout  
  19.             android:id="@android:id/tabcontent"  
  20.             android:layout_width="fill_parent"  
  21.             android:layout_height="fill_parent"  
  22.             android:padding="5dp" />  
  23.     </LinearLayout>  
  24.   
  25. </TabHost>  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值