Android实现底部导航栏功能(选项卡)

时间:2021-08-15 02:38:22

现在很多android的应用都采用底部导航栏的功能,这样可以使得用户在使用过程中随意切换不同的页面,现在我采用tabhost组件来自定义一个底部的导航栏的功能。
我们先看下该demo实例的框架图:

Android实现底部导航栏功能(选项卡)

其中各个类的作用以及资源文件就不详细解释了,还有资源图片(在该demo中借用了其它应用程序的资源图片)也不提供了,大家可以自行更换自己需要的资源图片。直接上各个布局文件或各个类的代码:
1、 res/layout目录下的 maintabs.xml 源码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="utf-8"?>
<tabhost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"
 xmlns:android="http://schemas.android.com/apk/res/android">
 <linearlayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
  <framelayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="0.0dip" android:layout_weight="1.0" />
  <tabwidget android:id="@android:id/tabs" android:visibility="gone" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.0" />
   <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">
    <radiobutton android:id="@id/radio_button0" android:layout_margintop="2.0dip" android:text="@string/main_home" android:drawabletop="@drawable/icon_1_n" style="@style/main_tab_bottom" />
    <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" />
    <radiobutton android:id="@id/radio_button2" android:layout_margintop="2.0dip" android:text="@string/main_manage_date" android:drawabletop="@drawable/icon_3_n" style="@style/main_tab_bottom" />
    <radiobutton android:id="@id/radio_button3" android:layout_margintop="2.0dip" android:text="@string/main_friends" android:drawabletop="@drawable/icon_4_n" style="@style/main_tab_bottom" />
    <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" />
   </radiogroup>
 </linearlayout>
</tabhost>


2、res/drawable 下的 home_btn_bg.xml 源码:

?
1
2
3
4
5
6
7
<selector
 xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_focused="true" android:state_enabled="true" android:state_pressed="false" android:drawable="@drawable/home_btn_bg_s" />
 <item android:state_enabled="true" android:state_pressed="true" android:drawable="@drawable/home_btn_bg_s" />
 <item android:state_enabled="true" android:state_checked="true" android:drawable="@drawable/home_btn_bg_d" />
 <item android:drawable="@drawable/transparent" />
</selector>

3、res/values 下的源码:
dimens.xml源码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?xml version="1.0" encoding="utf-8"?>
<resources>
 
 <dimen name="bottom_tab_font_size">10.0sp</dimen>
 <dimen name="bottom_tab_padding_up">5.0dip</dimen>
 <dimen name="bottom_tab_padding_drawable">2.0dip</dimen>
 <dimen name="switch_logo_bottom_padding">30.0sp</dimen>
 <dimen name="widget_height">100.0dip</dimen>
 <dimen name="sta_height">48.0dip</dimen>
 <dimen name="large_padding_length">20.0dip</dimen>
 <dimen name="widget_write_margin_top">19.0dip</dimen>
 <dimen name="widget_write_margin_left">10.0dip</dimen>
 <dimen name="widget_content_margin_top">20.0dip</dimen>
 <dimen name="widget_content_margin_left">10.0dip</dimen>
 <dimen name="widget_logo_size">35.0dip</dimen>
 <dimen name="title_height">74.0dip</dimen>
 <dimen name="new_blog_size">70.0dip</dimen>
 <dimen name="emotion_item_view_height">13.299988dip</dimen>
 <dimen name="splash_test_top_margin_top">20.0dip</dimen>
 <dimen name="splash_test_center_margin_right">0.0dip</dimen>
 <dimen name="title_text_size">20.0sp</dimen>
 <dimen name="normal_padding_length">10.0dip</dimen>
 <dimen name="no_result_padding_length">50.0dip</dimen>
 
</resources>

drawables.xml源码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8"?>
<resources>
 
 <item type="drawable" name="ltgray">#fff4f4f4</item>
 <item type="drawable" name="ltyellow">#fffff4db</item>
 <item type="drawable" name="black">#ff000000</item>
 <item type="drawable" name="transparent">#00000000</item>
 <item type="drawable" name="widget_edit_block_bg_normal">@android:color/transparent</item>
 <item type="drawable" name="transparent_background">#99000000</item>
 <item type="drawable" name="list_background">#fff4f4f4</item>
 <item type="drawable" name="namcard_picker_bkg_normal">#ff272727</item>
 <item type="drawable" name="namcard_picker_bkg_hover">#ff333333</item>
 
</resources>

ids.xml源码:

?
1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="utf-8"?>
<resources>
 
 <item type="id" name="main_radio">false</item>
 <item type="id" name="radio_button0">false</item>
 <item type="id" name="radio_button1">false</item>
 <item type="id" name="radio_button2">false</item>
 <item type="id" name="radio_button3">false</item>
 <item type="id" name="radio_button4">false</item>
 
</resources>

strings.xml源码:

?
1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="utf-8"?>
<resources>
 <string name="hello">hello world, maintabactivity!</string>
 <string name="app_name">tabdemo</string>
 <string name="main_news">消息</string>
 <string name="main_home">首页</string>
 <string name="more">更多</string>
 <string name="main_manage_date">时间</string>
 <string name="main_friends">好友</string>
</resources>

styles.xml源码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="utf-8"?>
<resources>
 
  <style name="main_tab_bottom">
  <item name="android:textsize">@dimen/bottom_tab_font_size</item>
  <item name="android:textcolor">#ffffffff</item>
  <item name="android:ellipsize">marquee</item>
  <item name="android:gravity">center_horizontal</item>
  <item name="android:background">@drawable/home_btn_bg</item>
  <item name="android:paddingtop">@dimen/bottom_tab_padding_up</item>
  <item name="android:paddingbottom">2.0dip</item>
  <item name="android:layout_width">fill_parent</item>
  <item name="android:layout_height">wrap_content</item>
  <item name="android:layout_marginbottom">2.0dip</item>
  <item name="android:button">@null</item>
  <item name="android:singleline">true</item>
  <item name="android:drawablepadding">@dimen/bottom_tab_padding_drawable</item>
  <item name="android:layout_weight">1.0</item>
 </style>
 
</resources>

4、 src/com.andyidea.tabdemo包下面各个ui界面类源码:
maintabactivity.java源码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.andyidea.tabdemo;
 
import android.app.tabactivity;
import android.content.intent;
import android.os.bundle;
import android.view.window;
import android.widget.compoundbutton;
import android.widget.radiobutton;
import android.widget.compoundbutton.oncheckedchangelistener;
import android.widget.tabhost;
 
public class maintabactivity extends tabactivity implements oncheckedchangelistener{
  
 private tabhost mtabhost;
 private intent maintent;
 private intent mbintent;
 private intent mcintent;
 private intent mdintent;
 private intent meintent;
  
 /** called when the activity is first created. */
 @override
 public void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  requestwindowfeature(window.feature_no_title);
  setcontentview(r.layout.maintabs);
   
  this.maintent = new intent(this,aactivity.class);
  this.mbintent = new intent(this,bactivity.class);
  this.mcintent = new intent(this,cactivity.class);
  this.mdintent = new intent(this,dactivity.class);
  this.meintent = new intent(this,eactivity.class);
   
  ((radiobutton) findviewbyid(r.id.radio_button0))
  .setoncheckedchangelistener(this);
  ((radiobutton) findviewbyid(r.id.radio_button1))
  .setoncheckedchangelistener(this);
  ((radiobutton) findviewbyid(r.id.radio_button2))
  .setoncheckedchangelistener(this);
  ((radiobutton) findviewbyid(r.id.radio_button3))
  .setoncheckedchangelistener(this);
  ((radiobutton) findviewbyid(r.id.radio_button4))
  .setoncheckedchangelistener(this);
   
  setupintent();
 }
 
 @override
 public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {
  if(ischecked){
   switch (buttonview.getid()) {
   case r.id.radio_button0:
    this.mtabhost.setcurrenttabbytag("a_tab");
    break;
   case r.id.radio_button1:
    this.mtabhost.setcurrenttabbytag("b_tab");
    break;
   case r.id.radio_button2:
    this.mtabhost.setcurrenttabbytag("c_tab");
    break;
   case r.id.radio_button3:
    this.mtabhost.setcurrenttabbytag("d_tab");
    break;
   case r.id.radio_button4:
    this.mtabhost.setcurrenttabbytag("more_tab");
    break;
   }
  }
   
 }
  
 private void setupintent() {
  this.mtabhost = gettabhost();
  tabhost localtabhost = this.mtabhost;
 
  localtabhost.addtab(buildtabspec("a_tab", r.string.main_home,
    r.drawable.icon_1_n, this.maintent));
 
  localtabhost.addtab(buildtabspec("b_tab", r.string.main_news,
    r.drawable.icon_2_n, this.mbintent));
 
  localtabhost.addtab(buildtabspec("c_tab",
    r.string.main_manage_date, r.drawable.icon_3_n,
    this.mcintent));
 
  localtabhost.addtab(buildtabspec("d_tab", r.string.main_friends,
    r.drawable.icon_4_n, this.mdintent));
 
  localtabhost.addtab(buildtabspec("more_tab", r.string.more,
    r.drawable.icon_5_n, this.meintent));
 
 }
  
 private tabhost.tabspec buildtabspec(string tag, int reslabel, int resicon,
   final intent content) {
  return this.mtabhost.newtabspec(tag).setindicator(getstring(reslabel),
    getresources().getdrawable(resicon)).setcontent(content);
 }
}

其中 aactivity.java 与 bactivity.java ,cactivity.java ,dactivity.java ,eactivity.java 中的源码都一样,只是用来表示不同的界面展示,故这里只列出 aactivity.java的源码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.andyidea.tabdemo;
 
import android.app.activity;
import android.os.bundle;
import android.view.gravity;
import android.widget.textview;
 
public class aactivity extends activity{
  
 @override
 public void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
   
  textview tv = new textview(this);
  tv.settext("this is a activity!");
  tv.setgravity(gravity.center);
  setcontentview(tv);
 }
 
}

最后,大家别忘了在 androidmanifest.xml 文件中注册各个 activity 组件哦。好了,现在我们看下我们程序运行的效果图:

Android实现底部导航栏功能(选项卡)

源码下载: android实现底部导航栏功能(选项卡)

到此就实现了一个比较流行的底部导航栏的功能了,希望本文所述对大家学习android软件编程有所帮助。