通过TabActivity创建底部菜单栏

隐藏TabWidget,通过RadioGroup和RadioButton实现底部菜单栏,因为这种方法更灵活,界面更容易控制,更好看。大部分应用程序基本上都是使用这种方法来实现的。通过setCurrentTabByTag()方法来切换不同的选项卡。

首先创建一个布局界面:

[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.   
  12.         <FrameLayout  
  13.             android:id="@android:id/tabcontent"  
  14.             android:layout_width="fill_parent"  
  15.             android:layout_height="0.0dip"  
  16.             android:layout_weight="1.0" />  
  17.   
  18.         <TabWidget  
  19.             android:id="@android:id/tabs"  
  20.             android:layout_width="fill_parent"  
  21.             android:layout_height="wrap_content"  
  22.             android:layout_weight="0.0"  
  23.             android:visibility="gone" />  
  24.   
  25.         <RadioGroup  
  26.             android:id="@+id/main_radiogroup"  
  27.             android:layout_width="fill_parent"  
  28.             android:layout_height="wrap_content"  
  29.             android:layout_gravity="bottom"  
  30.             android:background="@drawable/tab_widget_background"  
  31.             android:gravity="center_vertical"  
  32.             android:orientation="horizontal"  
  33.             android:padding="2dip" >  
  34.   
  35.             <RadioButton  
  36.                 android:id="@+id/RadioButton0"  
  37.                 style="@style/tab_item_background"  
  38.                 android:drawableTop="@drawable/tab_icon1"  
  39.                 android:text="主页" />  
  40.   
  41.             <RadioButton  
  42.                 android:id="@+id/RadioButton1"  
  43.                 style="@style/tab_item_background"  
  44.                 android:drawableTop="@drawable/tab_icon2"  
  45.                 android:text="关于" />  
  46.   
  47.             <RadioButton  
  48.                 android:id="@+id/RadioButton2"  
  49.                 style="@style/tab_item_background"  
  50.                 android:drawableTop="@drawable/tab_icon3"  
  51.                 android:text="设置" />  
  52.   
  53.             <RadioButton  
  54.                 android:id="@+id/RadioButton3"  
  55.                 style="@style/tab_item_background"  
  56.                 android:drawableTop="@drawable/tab_icon4"  
  57.                 android:text="搜索" />  
  58.   
  59.             <RadioButton  
  60.                 android:id="@+id/RadioButton4"  
  61.                 style="@style/tab_item_background"  
  62.                 android:drawableTop="@drawable/tab_icon5"  
  63.                 android:text="更多" />  
  64.         </RadioGroup>  
  65.     </LinearLayout>  
  66.   
  67. </TabHost> 


2、然后在定义几个用来存放Tab选项卡内容的activity布局文件,这里只列出一个activity1_layout.xml:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent" >  
  5.   
  6.     <ImageView  
  7.         android:id="@+id/imageview"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent"  
  10.         android:scaleType="fitCenter"  
  11.         android:src="@drawable/xianjian01" >  
  12.     </ImageView>  
  13.   
  14. </LinearLayout>  


3、最后再定义一个自定义Tab按钮的资源文件,selector_tab_background2.xml:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4.     <item android:drawable="@drawable/tab_item_p" android:state_pressed="true"/>  
  5.     <item android:drawable="@drawable/tab_item_d" android:state_checked="true"/>  
  6.   
  7. </selector>  


4、布局界面讲解完毕,接下来详细讲解java代码

[java]  view plain copy
  1. package com.yangyu.mycustomtab01;  
  2. import android.app.TabActivity;  
  3. import android.content.Intent;  
  4. import android.os.Bundle;  
  5. import android.widget.RadioButton;  
  6. import android.widget.RadioGroup;  
  7. import android.widget.RadioGroup.OnCheckedChangeListener;  
  8. import android.widget.TabHost;  
  9. import android.widget.TabHost.TabSpec;  
  10.   
  11. import com.yangyu.mycustomtab01.Constant.ConValue;  
  12.   
  13. /** 
  14.  * @author yangyu 
  15.  *  功能描述:第二种实现方式,自定义RadioGroup 
  16.  */  
  17. public class CustomTabActivity2 extends TabActivity{  
  18.       
  19.     //定义TabHost对象  
  20.     private TabHost tabHost;  
  21.       
  22.     //定义RadioGroup对象  
  23.     private RadioGroup radioGroup;  
  24.       
  25.     public void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.main_tab_layout2);  
  28.           
  29.         initView();  
  30.           
  31.         initData();  
  32.     }  
  33.       
  34.     /** 
  35.      * 初始化组件 
  36.      */  
  37.     private void initView(){  
  38.         //实例化TabHost,得到TabHost对象  
  39.         tabHost = getTabHost();  
  40.           
  41.         //得到Activity的个数  
  42.         int count = ConValue.mTabClassArray.length;               
  43.                   
  44.         for(int i = 0; i < count; i++){    
  45.             //为每一个Tab按钮设置图标、文字和内容  
  46.             TabSpec tabSpec = tabHost.newTabSpec(ConValue.mTextviewArray[i]).setIndicator(ConValue.mTextviewArray[i]).setContent(getTabItemIntent(i));  
  47.             //将Tab按钮添加进Tab选项卡中  
  48.             tabHost.addTab(tabSpec);  
  49.         }  
  50.           
  51.         //实例化RadioGroup  
  52.         radioGroup = (RadioGroup) findViewById(R.id.main_radiogroup);  
  53.     }  
  54.       
  55.     /** 
  56.      * 初始化组件 
  57.      */  
  58.     private void initData() {  
  59.         // 给radioGroup设置监听事件  
  60.         radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
  61.             @Override  
  62.             public void onCheckedChanged(RadioGroup group, int checkedId) {  
  63.                 switch (checkedId) {  
  64.                 case R.id.RadioButton0:  
  65.                     tabHost.setCurrentTabByTag(ConValue.mTextviewArray[0]);  
  66.                     break;  
  67.                 case R.id.RadioButton1:  
  68.                     tabHost.setCurrentTabByTag(ConValue.mTextviewArray[1]);  
  69.                     break;  
  70.                 case R.id.RadioButton2:  
  71.                     tabHost.setCurrentTabByTag(ConValue.mTextviewArray[2]);  
  72.                     break;  
  73.                 case R.id.RadioButton3:  
  74.                     tabHost.setCurrentTabByTag(ConValue.mTextviewArray[3]);  
  75.                     break;  
  76.                 case R.id.RadioButton4:  
  77.                     tabHost.setCurrentTabByTag(ConValue.mTextviewArray[4]);  
  78.                     break;  
  79.                 }  
  80.             }  
  81.         });  
  82.         ((RadioButton) radioGroup.getChildAt(0)).toggle();  
  83.     }  
  84.       
  85.     /** 
  86.      * 给Tab选项卡设置内容(每个内容都是一个Activity) 
  87.      */  
  88.     private Intent getTabItemIntent(int index){  
  89.         Intent intent = new Intent(this, ConValue.mTabClassArray[index]);     
  90.         return intent;  
  91.     }  
  92. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值