实现方式一:通过TabWidget实现这种方式主要是在布局中将TabWidget标签嵌套在RelativeLayout中,并且在TabWidget标签中中设置 android:layout_alignParentBottom="true"另外,下划线和选项卡之间的线去除的方法时在TabWidget标签中设置属性android:tabStripEnabled="false"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. android:orientation="vertical" >
  7. <FrameLayout
  8. android:id="@android:id/tabcontent"
  9. android:layout_width="fill_parent"
  10. android:layout_height="fill_parent"
  11. android:padding="5dp"
  12. ></FrameLayout>

  13. <RelativeLayout
  14. android:layout_width="fill_parent"
  15. android:layout_height="fill_parent">
  16. <!-- tabStripEnabled属性去掉底部下划线与选项卡间的下划线 -->
  17. <!-- layout_alignParentBottom属性即可将其放在底部菜单栏,注意,必须在RelativeLayout里 -->
  18. <TabWidget
  19. android:id="@android:id/tabs"
  20. android:tabStripEnabled="false"
  21. android:background="#6E6E6E"
  22. android:layout_width="fill_parent"
  23. android:layout_height="wrap_content"
  24. android:layout_alignParentBottom="true"
  25. ></TabWidget>

  26. </RelativeLayout>

  27. </TabHost>
复制代码 实现代码:
  1. package com.loulijun.demo1;

  2. import android.app.TabActivity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.widget.TabHost;

  6. public class Demo1Activity extends TabActivity {
  7. <span style="color: rgb(0, 128, 0); ">/**</span><span style="color: rgb(0, 128, 0); "> Called when the activity is first created. </span><span style="color: rgb(0, 128, 0); ">*/</span>
  8. private TabHost tabhost;
  9. private Intent intent1, intent2, intent3, intent4;
  10. @Override
  11. public void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.main);
  14. tabhost = getTabHost();

  15. intent1 = new Intent(Demo1Activity.this, One.class);
  16. tabhost.addTab(tabhost.newTabSpec("one")
  17. .setIndicator("电话",getResources().getDrawable(android.R.drawable.ic_menu_call))
  18. .setContent(intent1));

  19. intent2 = new Intent(Demo1Activity.this, Two.class);
  20. tabhost.addTab(tabhost.newTabSpec("two")
  21. .setIndicator("相机",getResources().getDrawable(android.R.drawable.ic_menu_camera))
  22. .setContent(intent2));

  23. intent3 = new Intent(Demo1Activity.this, Three.class);
  24. tabhost.addTab(tabhost.newTabSpec("three")
  25. .setIndicator("分享",getResources().getDrawable(android.R.drawable.ic_menu_share))
  26. .setContent(intent3));

  27. intent4 = new Intent(Demo1Activity.this, Four.class);
  28. tabhost.addTab(tabhost.newTabSpec("four")
  29. .setIndicator("更多",getResources().getDrawable(android.R.drawable.ic_menu_more))
  30. .setContent(intent4));
  31. }
  32. }
复制代码 运行图:
2012-7-13 10:11 上传 下载附件 (18.79 KB)
实现方式二:隐藏TabWidget,通过RadioGroup和RadioButton实现底部菜单栏这种方式更漂亮,也更灵活,网上基本用的是这种方式,通过setCurrentTabByTag来切换不同的选项卡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. >
  7. <LinearLayout
  8. android:orientation="vertical"
  9. android:layout_width="fill_parent"
  10. android:layout_height="fill_parent">
  11. <FrameLayout
  12. android:id="@android:id/tabcontent"
  13. android:layout_width="fill_parent"
  14. android:layout_height="0.0dip"
  15. android:layout_weight="1.0"/>
  16. <TabWidget
  17. android:id="@android:id/tabs"
  18. android:layout_width="fill_parent"
  19. android:layout_height="wrap_content"
  20. android:layout_weight="0.0"
  21. android:visibility="gone"/>
  22. <RadioGroup
  23. android:id="@+id/main_tab"
  24. android:background="@drawable/maintab_toolbar_bg"
  25. android:orientation="horizontal"
  26. android:layout_width="fill_parent"
  27. android:layout_height="wrap_content"
  28. android:gravity="center_vertical"
  29. android:layout_gravity="bottom">
  30. <RadioButton
  31. android:layout_marginTop="2.0dip"
  32. android:text="@string/main_home"
  33. android:drawableTop="@drawable/icon_1_n"
  34. android:id="@+id/radio_button0"
  35. style="@style/main_tab_bottom"/>
  36. <RadioButton
  37. android:layout_marginTop="2.0dip"
  38. android:text="@string/main_news"
  39. android:drawableTop="@drawable/icon_2_n"
  40. android:id="@+id/radio_button1"
  41. style="@style/main_tab_bottom"/>
  42. <RadioButton
  43. android:layout_marginTop="2.0dip"
  44. android:text="@string/main_my_info"
  45. android:drawableTop="@drawable/icon_3_n"
  46. android:id="@+id/radio_button2"
  47. style="@style/main_tab_bottom"/>
  48. <RadioButton
  49. android:layout_marginTop="2.0dip"
  50. android:text="@string/menu_search"
  51. android:drawableTop="@drawable/icon_4_n"
  52. android:id="@+id/radio_button3"
  53. style="@style/main_tab_bottom"/>
  54. <RadioButton
  55. android:layout_marginTop="2.0dip"
  56. android:text="@string/more"
  57. android:drawableTop="@drawable/icon_5_n"
  58. android:id="@+id/radio_button4"
  59. style="@style/main_tab_bottom"/>
  60. </RadioGroup>
  61. </LinearLayout>
  62. </TabHost>
复制代码 drawable/home_btn_bg.xml:

切换时的效果:代码:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <selector
  3. xmlns:android="http://schemas.android.com/apk/res/android">
  4. <item android:state_focused="true" android:state_enabled="true" android:state_pressed="false" android:drawable="@drawable/home_btn_bg_s" />
  5. <item android:state_enabled="true" android:state_pressed="true" android:drawable="@drawable/home_btn_bg_s" />
  6. <item android:state_enabled="true" android:state_checked="true" android:drawable="@drawable/home_btn_bg_d" />
  7. <item android:drawable="@drawable/transparent" />
  8. </selector>
复制代码 string/dimens.xml 尺寸文件:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <dimen name="bottom_tab_padding_drawable">2.0dip</dimen>
  4. <dimen name="bottom_tab_padding_up">5.0dip</dimen>
  5. <dimen name="bottom_tab_font_size">10.0dip</dimen>
  6. </resources>
  7. string/drawables.xml 设置为透明:
  8. <?xml version="1.0" encoding="utf-8"?>
  9. <resources>
  10. <item type="drawable" name="transparent">#00000000</item>
  11. </resources>
复制代码 string/styles.xml 样式文件:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <style name="main_tab_bottom">
  4. <item name="android:textSize">@dimen/bottom_tab_font_size</item>
  5. <item name="android:textColor">#ffffffff</item>
  6. <item name="android:ellipsize">marquee</item>
  7. <item name="android:gravity">center_horizontal</item>
  8. <item name="android:background">@drawable/home_btn_bg</item>
  9. <item name="android:paddingTop">@dimen/bottom_tab_padding_up</item>
  10. <item name="android:layout_width">fill_parent</item>
  11. <item name="android:layout_height">wrap_content</item>
  12. <item name="android:button">@null</item>
  13. <item name="android:singleLine">true</item>
  14. <item name="android:drawablePadding">@dimen/bottom_tab_padding_drawable</item>
  15. <item name="android:layout_weight">1.0</item>
  16. </style>
  17. </resources>
复制代码
  1. import android.app.TabActivity;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.view.Window;
  5. import android.widget.RadioGroup;
  6. import android.widget.TabHost;
  7. import android.widget.RadioGroup.OnCheckedChangeListener;
  8. public class MainTabActivity extends TabActivity implements OnCheckedChangeListener{
  9. private RadioGroup mainTab;
  10. private TabHost tabhost;
  11. private Intent iHome;
  12. private Intent iNews;
  13. private Intent iInfo;
  14. private Intent iSearch;
  15. private Intent iMore;

  16. @Override
  17. public void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. requestWindowFeature(Window.FEATURE_NO_TITLE);
  20. setContentView(R.layout.main);
  21. mainTab=(RadioGroup)findViewById(R.id.main_tab);
  22. mainTab.setOnCheckedChangeListener(this);
  23. tabhost = getTabHost();

  24. iHome = new Intent(this, HomeActivity.class);
  25. tabhost.addTab(tabhost.newTabSpec("iHome")
  26. .setIndicator(getResources().getString(R.string.main_home), getResources().getDrawable(R.drawable.icon_1_n))
  27. .setContent(iHome));

  28. iNews = new Intent(this, NewsActivity.class);
  29. tabhost.addTab(tabhost.newTabSpec("iNews")
  30. .setIndicator(getResources().getString(R.string.main_news), getResources().getDrawable(R.drawable.icon_2_n))
  31. .setContent(iNews));

  32. iInfo = new Intent(this, MyInfoActivity.class);
  33. tabhost.addTab(tabhost.newTabSpec("iInfo")
  34. .setIndicator(getResources().getString(R.string.main_my_info), getResources().getDrawable(R.drawable.icon_3_n))
  35. .setContent(iInfo));

  36. iSearch = new Intent(this,SearchActivity.class);
  37. tabhost.addTab(tabhost.newTabSpec("iSearch")
  38. .setIndicator(getResources().getString(R.string.menu_search), getResources().getDrawable(R.drawable.icon_4_n))
  39. .setContent(iSearch));

  40. iMore = new Intent(this, MoreActivity.class);
  41. tabhost.addTab(tabhost.newTabSpec("iMore")
  42. .setIndicator(getResources().getString(R.string.more), getResources().getDrawable(R.drawable.icon_5_n))
  43. .setContent(iMore));
  44. }


  45. @Override
  46. public void onCheckedChanged(RadioGroup group, int checkedId) {
  47. switch(checkedId){
  48. case R.id.radio_button0:
  49. this.tabhost.setCurrentTabByTag("iHome");
  50. break;
  51. case R.id.radio_button1:
  52. this.tabhost.setCurrentTabByTag("iNews");
  53. break;
  54. case R.id.radio_button2:
  55. this.tabhost.setCurrentTabByTag("iInfo");
  56. break;
  57. case R.id.radio_button3:
  58. this.tabhost.setCurrentTabByTag("iSearch");
  59. break;
  60. case R.id.radio_button4:
  61. this.tabhost.setCurrentTabByTag("iMore");
  62. break;
  63. }
  64. }
  65. }
复制代码 运行图:
2012-7-13 10:11 上传 下载附件 (18.4 KB)


更多相关文章

  1. Android:高效的Android代码编写
  2. webkit framework for android 4.0.3 代码总结
  3. android实现swipe的手势及页面拖动动画
  4. Android(安卓)混淆代码问题总结
  5. Android(安卓)Studio调试native或者service
  6. Android
  7. 给android 2.3 提供鼠标支持
  8. android TextView多行文本(超过3行)使用ellipsize属性无效问题的
  9. RTC搭建android下三层应用程序访问服务器MsSql-客户端

随机推荐

  1. and 使用以及添加一个自己的mime type在C
  2. 《H5 App开发》判断当前环境是Android还
  3. Android构建报错:Android resource linkin
  4. android linux 基础知识总结
  5. Android 获取设备信息
  6. android的单元测试摘要
  7. 虚拟键盘弹出时挡住EditText的解决方案
  8. Android01————快速入门
  9. Android(安卓)Studio 在 Mac 上如何真机
  10. 【Android view】获取状态栏高度statu ba