android中TabHost+intent的使用

分类: Android 1236人阅读 评论(5) 收藏 举报

本文将记录如何一步一步做TabHost的,最终实现效果如下图

    

下面具体讲解如何一步一步做出来这个效果的!

1:创建工程和类如下图所示 

2: 编写ic_tab_artists.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <!-- When selected, use grey -->  
  4.     <item android:drawable="@drawable/ic_tab_artists_grey"  
  5.           android:state_selected="true" />  
  6.     <!-- When not selected, use white-->  
  7.     <item android:drawable="@drawable/ic_tab_artists_white" />  
  8. </selector>  

作用: 这个选择器适用于当tab在不同之间切换的时候选择不同的图片,可以通过state_selected来设置

3: 编写main.xml

  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.     <RelativeLayout  
  7.         android:orientation="vertical"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent"  
  10.         android:padding="5dp">  
  11.         <TabWidget  
  12.             android:id="@android:id/tabs"  
  13.             android:layout_width="fill_parent"  
  14.             android:layout_height="wrap_content"  
  15.             android:layout_alignParentBottom="true"  
  16.           />  
  17.         <FrameLayout  
  18.             android:id="@android:id/tabcontent"  
  19.             android:layout_width="fill_parent"  
  20.             android:layout_height="fill_parent"  
  21.             android:padding="5dp" />  
  22.     </RelativeLayout>  
  23. </TabHost>  

作用:一个TabHost的配置文件  android:layout_alignParentBottom="true" 指明tab的方向(默认在上方,我这个例子实现的是放在下方) 其中 : TabWidget 指明的是tab

FrameLayout 指明的是content

4:为每个tab编写类容

    

  1. public class AlbumsActivity extends Activity {  
  2.     public void onCreate(Bundle savedInstanceState) {  
  3.         super.onCreate(savedInstanceState);  
  4.         TextView textview = new TextView(this);  
  5.         textview.setText("This is the Albums tab");  
  6.         setContentView(textview);  
  7.     }  
  8. }  

作用:显示content的区域,这里只是简单的使用了一个TextView 来进行显示区域里面的数据(其他几个类似)

5: 编写HelloTabWidget

  1. public class HelloTabWidget extends TabActivity {  
  2.     /** Called when the activity is first created. */  
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState) {  
  5.          super.onCreate(savedInstanceState);  
  6.             setContentView(R.layout.main);  
  7.   
  8.             Resources res = getResources(); // Resource object to get Drawables  
  9.             TabHost tabHost = getTabHost();  // The activity TabHost  
  10.             TabHost.TabSpec spec;  // Resusable TabSpec for each tab  
  11.             Intent intent;  // Reusable Intent for each tab  
  12.   
  13.             // Create an Intent to launch an Activity for the tab (to be reused)  
  14.             intent = new Intent().setClass(this, ArtistsActivity.class);  
  15.   
  16.             // Initialize a TabSpec for each tab and add it to the TabHost  
  17.             spec = tabHost.newTabSpec("artists").setIndicator("Artists",  
  18.                               res.getDrawable(R.drawable.ic_tab_artists))  
  19.                           .setContent(intent);  
  20.             tabHost.addTab(spec);  
  21.   
  22.             // Do the same for the other tabs  
  23.             intent = new Intent().setClass(this, AlbumsActivity.class);  
  24.             spec = tabHost.newTabSpec("albums").setIndicator("Albums",  
  25.                               res.getDrawable(R.drawable.ic_tab_artists))  
  26.                           .setContent(intent);  
  27.             tabHost.addTab(spec);  
  28.   
  29.             intent = new Intent().setClass(this, SongsActivity.class);  
  30.             spec = tabHost.newTabSpec("songs").setIndicator("Songs",  
  31.                               res.getDrawable(R.drawable.ic_tab_artists))  
  32.                           .setContent(intent);  
  33.             tabHost.addTab(spec);  
  34.   
  35.             intent = new Intent().setClass(this, newAddActivity.class);  
  36.   
  37.             // Initialize a TabSpec for each tab and add it to the TabHost  
  38.             spec = tabHost.newTabSpec("add").setIndicator("Add",  
  39.                               res.getDrawable(R.drawable.ic_tab_artists))  
  40.                           .setContent(intent);  
  41.             tabHost.addTab(spec);  
  42.   
  43.             tabHost.setCurrentTab(2);  
  44.             //处理tabId  
  45.             tabHost.setOnTabChangedListener(new OnTabChangeListener() {  
  46.                 public void onTabChanged(String tabId) {  
  47.                          Log.i("HelloTabWidget", tabId) ;  
  48.                 }  
  49.             });  
  50.   
  51.     }  


作用: 实现了TabActivity 并添加了一个新的newAddActivity ,如果你要实现TabChanged后的逻辑可以实现OnTabChangeListener这个接口

             这里具体讲解一下如何实现的过程:

            ①:TabHost tabHost = getTabHost();   获得TabHost根标签

            ②:spec = tabHost.newTabSpec("artists").setIndicator("Artists", res.getDrawable(R.drawable.ic_tab_artists)) .setContent(intent); 实例化一个具体的tab,并设置图标和类容

            ③: tabHost.addTab(spec); 添加到TabHost 中

            ④: tabHost.setCurrentTab(2); 默认选择卡

             本例的下载源码: http://download.csdn.net/source/3576083


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值