---
title: "Android版底部工具栏"
author: "dss16694"
date: "2011-12-13T11:46:03Z"
tags: []
categories: ["移动开发"]
slug: "android"
draft: false
lastmod: "2011-12-13T11:46:03Z"
id: 1305432
url: "https://dss16694.iteye.com/blog/1305432"
---

**前言**

　　为了更好的开发Android应用程序，除了熟练掌握基本的UI组件和API外，还需要掌握一些技巧，而这些技巧可以通过阅读一些代码来提高，本系列将与大家分享一些新浪微博布局方面的收获，欢迎交流！

 

**声明**

　　欢迎转载，但请保留文章原始出处:)

　　　　博客园：http://www.cnblogs.com

　　　　农民伯伯： http://www.cnblogs.com/over140

 

**版本**

　　新浪微博 weibo\_10235010.apk

 

**正文**

　　一、效果图

　　　　![](http://images.cnblogs.com/cnblogs_com/over140/2011/3/2011-3-1_1.png)

　　　　红色部分是本文要实现的目标。

 

　　二、实现maintabs.xml

Xml代码     [![收藏代码](http://flysnow.iteye.com/images/icon_star.png)](http://flysnow.iteye.com/blog/938363 "收藏这段代码")

01. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
02. &lt;TabHost android:id="@android:id/tabhost" android:layout\_width="fill\_parent" android:layout\_height="fill\_parent"
03. xmlns:android="http://schemas.android.com/apk/res/android"&gt;
04. &lt;LinearLayout android:orientation="vertical" android:layout\_width="fill\_parent" android:layout\_height="fill\_parent"&gt;
05. &lt;FrameLayout android:id="@android:id/tabcontent" android:layout\_width="fill\_parent" android:layout\_height="0.0dip" android:layout\_weight="1.0" /&gt;
06. &lt;TabWidget android:id="@android:id/tabs" android:visibility="gone" android:layout\_width="fill\_parent" android:layout\_height="wrap\_content" android:layout\_weight="0.0" /&gt;
07. &lt;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"&gt;
08. &lt;RadioButton   android:text="@string/main\_home" android:checked="true" android:id="@+id/radio\_button0" android:layout\_marginTop="2.0dip" android:drawableTop="@drawable/icon\_1\_n" style="@style/main\_tab\_bottom" /&gt;
09. &lt;RadioButton android:id="@+id/radio\_button1" android:layout\_marginTop="2.0dip" android:text="@string/main\_news" android:drawableTop="@drawable/icon\_2\_n" style="@style/main\_tab\_bottom" /&gt;
10. &lt;RadioButton android:id="@+id/radio\_button2" android:layout\_marginTop="2.0dip" android:text="@string/main\_my\_info" android:drawableTop="@drawable/icon\_3\_n" style="@style/main\_tab\_bottom" /&gt;
11. &lt;RadioButton android:id="@+id/radio\_button3" android:layout\_marginTop="2.0dip" android:text="@string/menu\_search" android:drawableTop="@drawable/icon\_4\_n" style="@style/main\_tab\_bottom" /&gt;
12. &lt;RadioButton android:id="@+id/radio\_button4" android:layout\_marginTop="2.0dip" android:text="@string/more" android:drawableTop="@drawable/icon\_5\_n" style="@style/main\_tab\_bottom" /&gt;
13. &lt;/RadioGroup&gt;
14. &lt;/LinearLayout&gt;
15. &lt;/TabHost&gt;

 

　　　　styles.xml

Java代码     [![收藏代码](http://flysnow.iteye.com/images/icon_star.png)](http://flysnow.iteye.com/blog/938363 "收藏这段代码")

01. &lt;style name="main\_tab\_bottom"&gt;
02. &lt;item name="android:textSize"&gt;@dimen/bottom\_tab\_font\_size&lt;/item&gt;
03. &lt;item name="android:textColor"&gt;#ffffffff&lt;/item&gt;
04. &lt;item name="android:ellipsize"&gt;marquee&lt;/item&gt;
05. &lt;item name="android:gravity"&gt;center\_horizontal&lt;/item&gt;
06. &lt;item name="android:background"&gt;@drawable/home\_btn\_bg&lt;/item&gt;
07. &lt;item name="android:paddingTop"&gt;@dimen/bottom\_tab\_padding\_up&lt;/item&gt;
08. &lt;item name="android:layout\_width"&gt;fill\_parent&lt;/item&gt;
09. &lt;item name="android:layout\_height"&gt;wrap\_content&lt;/item&gt;
10. &lt;item name="android:button"&gt;@null&lt;/item&gt;
11. &lt;item name="android:singleLine"&gt;true&lt;/item&gt;
12. &lt;item name="android:drawablePadding"&gt;@dimen/bottom\_tab\_padding\_drawable&lt;/item&gt;
13. &lt;item name="android:layout\_weight"&gt;1.0&lt;/item&gt;
14. &lt;/style&gt;

 

 　　　　home\_btn\_bg.xml

       

Java代码     [![收藏代码](http://flysnow.iteye.com/images/icon_star.png)](http://flysnow.iteye.com/blog/938363 "收藏这段代码")

1. &lt;selector
2. xmlns:android="http://schemas.android.com/apk/res/android"&gt;
3. &lt;item android:state\_focused="true" android:state\_enabled="true" android:state\_pressed="false" android:drawable="@drawable/home\_btn\_bg\_s" /&gt;
4. &lt;item android:state\_enabled="true" android:state\_pressed="true" android:drawable="@drawable/home\_btn\_bg\_s" /&gt;
5. &lt;item android:state\_enabled="true" android:state\_checked="true" android:drawable="@drawable/home\_btn\_bg\_d" /&gt;
6. &lt;item android:drawable="@drawable/transparent" /&gt;
7. &lt;/selector&gt;

 

　　　　代码说明：

　　　　　　　　1.　　需要注意的是他这里把TabWidget的Visibility设置成了gone!也就是默认难看的风格不见了：![](http://images.cnblogs.com/cnblogs_com/over140/2011/3/2011-3-1_2.jpg) ，取而代之的是5个带风格的单选按钮.

　　　　　　　　2.　　注意为单选按钮设置的style，其中最重要的是为其background设置了home\_btn\_bg.xml，也就是自定义了选中效果。

　　　　Java文件

Java代码     [![收藏代码](http://flysnow.iteye.com/images/icon_star.png)](http://flysnow.iteye.com/blog/938363 "收藏这段代码")

01. public class MainTabActivity extends TabActivity implements
02. OnCheckedChangeListener {
03. private TabHost mHost;
04. private Intent mMBlogIntent;
05. private Intent mMoreIntent;
06. private Intent mInfoIntent;
07. private Intent mSearchIntent;
08. private Intent mUserInfoIntent;
09. @Override
10. protected void onCreate(Bundle savedInstanceState) {
11. super.onCreate(savedInstanceState);
12. requestWindowFeature(Window.FEATURE\_NO\_TITLE);
13. setContentView(R.layout.maintabs);
14. // ~~~~~~~~~~~~ 初始化
15. this.mMBlogIntent = new Intent(this, HomeListActivity.class);
16. this.mSearchIntent = new Intent(this, SearchSquareActivity.class);
17. this.mInfoIntent = new Intent(this, MessageGroup.class);
18. this.mUserInfoIntent = new Intent(this, MyInfoActivity.class);
19. this.mMoreIntent = new Intent(this, MoreItemsActivity.class);
20. initRadios();
21. setupIntent();
22. }
23. /\**
24. * 初始化底部按钮
25. \*/
26. private void initRadios() {
27. ((RadioButton) findViewById(R.id.radio\_button0)).setOnCheckedChangeListener(this);
28. ((RadioButton) findViewById(R.id.radio\_button1)).setOnCheckedChangeListener(this);
29. ((RadioButton) findViewById(R.id.radio\_button2)).setOnCheckedChangeListener(this);
30. ((RadioButton) findViewById(R.id.radio\_button3)).setOnCheckedChangeListener(this);
31. ((RadioButton) findViewById(R.id.radio\_button4)).setOnCheckedChangeListener(this);
32. }
33. /\**
34. * 切换模块
35. \*/
36. @Override
37. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
38. if (isChecked) {
39. switch (buttonView.getId()) {
40. case R.id.radio\_button0:
41. this.mHost.setCurrentTabByTag("mblog\_tab");
42. break;
43. case R.id.radio\_button1:
44. this.mHost.setCurrentTabByTag("message\_tab");
45. break;
46. case R.id.radio\_button2:
47. this.mHost.setCurrentTabByTag("userinfo\_tab");
48. break;
49. case R.id.radio\_button3:
50. this.mHost.setCurrentTabByTag("search\_tab");
51. break;
52. case R.id.radio\_button4:
53. this.mHost.setCurrentTabByTag("more\_tab");
54. break;
55. }
56. }
57. }
58. private void setupIntent() {
59. this.mHost = getTabHost();
60. TabHost localTabHost = this.mHost;
61. localTabHost.addTab(buildTabSpec("mblog\_tab", R.string.main\_home,
62. R.drawable.icon\_1\_n, this.mMBlogIntent));
63. localTabHost.addTab(buildTabSpec("message\_tab", R.string.main\_news,
64. R.drawable.icon\_2\_n, this.mInfoIntent));
65. localTabHost.addTab(buildTabSpec("userinfo\_tab", R.string.main\_my\_info,
66. R.drawable.icon\_3\_n, this.mUserInfoIntent));
67. localTabHost.addTab(buildTabSpec("search\_tab", R.string.menu\_search,
68. R.drawable.icon\_4\_n, this.mSearchIntent));
69. localTabHost.addTab(buildTabSpec("more\_tab", R.string.more,
70. R.drawable.icon\_5\_n, this.mMoreIntent));
71. }
72. private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon,
73. final Intent content) {
74. return this.mHost
75. .newTabSpec(tag)
76. .setIndicator(getString(resLabel),
77. getResources().getDrawable(resIcon))
78. .setContent(content);
79. }

 

　　　　代码说明

　　　　　　1.　　由于TabWidget被隐藏，所以相关的事件也会无效，这里取巧用RadioGroup与RadioButton的特性来处理切换，然后监听事件调用setCurrentTabByTag来切换Activity。

　　　　　　2.　　注意即使TabWidget被隐藏，也要为其设置indicator，否则会保持。

 

　　三、总结

　 　　　在这之前如果要做这种效果我恐怕第一时间就会想到用ActivityGroup来做，主要是因为TabHost的TabWidget非常难看，用起 来也不方便。其实从源码可以看出，TabActivity也是继承自ActivityGroup，这里结合了单选按钮和TabHost，各取其长，有时间 可以专门写一个这样的自定义控件:)

 

　　四、相关文章

　　　　[\[Android\]使用ActivityGroup来切换Activity和Layout](http://www.cnblogs.com/over140/archive/2010/09/07/1820876.html)

 

**结束**

　　 本文中使用的资源均反编译自apk文件，这里主要是讲思路，欢迎大家交流。

 

示例DEMO：

[SinaWeibo.zip](http://dl.iteye.com/topics/download/153463c3-3caa-3d5a-a10a-fc1be59eae36)