底部导航的RadioButton 方式实现

文件 maintabs.xml:

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"  
  3.   xmlns:android="http://schemas.android.com/apk/res/android">  
  4.     <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">  
  5.   
  6.         <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="0.0dip" android:layout_weight="1.0" />  
  7.         <TabWidget android:id="@android:id/tabs" android:visibility="gone" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.0" />  
  8.         <RadioGroup android:gravity="center_vertical" android:layout_gravity="bottom" android:orientation="horizontal" android:id="@+id/main_radio" android:background="@drawable/maintab_toolbar_bg" android:layout_width="fill_parent" android:layout_height="wrap_content">  
  9.             <RadioButton android:id="@+id/radio_button0" android:tag="radio_button0" android:layout_marginTop="2.0dip" android:text="@string/main_home" android:drawableTop="@drawable/icon_1_n" style="@style/main_tab_bottom" />  
  10.             <RadioButton android:id="@+id/radio_button1" android:tag="radio_button1" android:layout_marginTop="2.0dip" android:text="@string/main_message" android:drawableTop="@drawable/icon_2_n" style="@style/main_tab_bottom" />  
  11.             <RadioButton android:id="@+id/radio_button2" android:tag="radio_button2" android:layout_marginTop="2.0dip" android:text="@string/main_comment" android:drawableTop="@drawable/icon_3_n" style="@style/main_tab_bottom" />  
  12.             <RadioButton android:id="@+id/radio_button3" android:tag="radio_button3" android:layout_marginTop="2.0dip" android:text="@string/main_reload" android:drawableTop="@drawable/icon_4_n" style="@style/main_tab_bottom" />  
  13.             <RadioButton android:id="@+id/radio_button4" android:tag="radio_button4" android:layout_marginTop="2.0dip" android:text="@string/more" android:drawableTop="@drawable/icon_5_n" style="@style/main_tab_bottom" />  
  14.         </RadioGroup>  
  15.     </LinearLayout>  
  16. </TabHost>  

styles.xml:

[html]  view plain copy
  1. <style name="main_tab_bottom">  
  2.     <item name="android:textSize">@dimen/bottom_tab_font_size</item>  
  3.     <item name="android:textColor">#ffffffff</item>  
  4.     <item name="android:ellipsize">marquee</item>  
  5.     <item name="android:gravity">center_horizontal</item>    
  6.     <item name="android:background">@drawable/home_btn_bg</item>  
  7.     <item name="android:paddingTop">@dimen/bottom_tab_padding_up</item>  
  8.     <item name="android:layout_width">fill_parent</item>  
  9.     <item name="android:layout_height">wrap_content</item>  
  10.     <item name="android:button">@null</item>    
  11.     <item name="android:singleLine">true</item>  
  12.     <item name="android:drawablePadding">@dimen/bottom_tab_padding_drawable</item>  
  13.     <item name="android:layout_weight">1.0</item>  
  14. </style>  

strings.xml:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="main_home">首页</string>  
  4.     <string name="main_comment">评论</string>  
  5.     <string name="main_message">私信</string>  
  6.     <string name="main_edit">编写</string>  
  7.     <string name="main_reload">刷新</string>  
  8.     <string name="more">更多</string>  
  9. </resources>  

TabTest:

[html]  view plain copy
  1. package com.eric;  
  2.   
  3. import android.app.TabActivity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.widget.RadioGroup;  
  7. import android.widget.TabHost;  
  8. import android.widget.RadioGroup.OnCheckedChangeListener;  
  9.   
  10. public class TabTest extends TabActivity{  
  11.     private RadioGroup group;  
  12.     private TabHost tabHost;  
  13.     public static final String TAB_HOME="tabHome";  
  14.     public static final String TAB_MES="tabMes";  
  15.     public static final String TAB_TOUCH="tab_touch";  
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         // TODO Auto-generated method stub  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.maintabs);  
  21.         group = (RadioGroup)findViewById(R.id.main_radio);  
  22.         tabHost = getTabHost();  
  23.         tabHost.addTab(tabHost.newTabSpec(TAB_HOME)  
  24.                     .setIndicator(TAB_HOME)  
  25.                     .setContent(new Intent(this,Main.class)));  
  26.         tabHost.addTab(tabHost.newTabSpec(TAB_MES)  
  27.                     .setIndicator(TAB_MES)  
  28.                     .setContent(new Intent(this,Main2.class)));  
  29.         tabHost.addTab(tabHost.newTabSpec(TAB_TOUCH)  
  30.                 .setIndicator(TAB_TOUCH)  
  31.                 .setContent(new Intent(this,TouchTest.class)));  
  32.         group.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
  33.             public void onCheckedChanged(RadioGroup group, int checkedId) {  
  34.                 switch (checkedId) {  
  35.                 case R.id.radio_button0:  
  36.                     tabHost.setCurrentTabByTag(TAB_HOME);  
  37.                     break;  
  38.                 case R.id.radio_button1:  
  39.                     tabHost.setCurrentTabByTag(TAB_MES);  
  40.                     break;  
  41.                 case R.id.radio_button2:  
  42.                     tabHost.setCurrentTabByTag(TAB_TOUCH);  
  43.                     break;  
  44.   
  45.                 default:  
  46.                     break;  
  47.                 }  
  48.             }  
  49.         });  
  50.     }  
  51. }  

home_btn_bg_d.png     home_btn_bg_n.png      home_btn_bg_s.png

                                                  

maintab_toolbar_bg.png

效果图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值