关于安卓开发实现底部菜单栏(已过时做法,不建议使用)

简介: 将TabHost的标签放在底部   直接上代码 主代码: 1 package sdut; 2 3 import com.example.sdutfriends.R; 4 5 import android.

将TabHost的标签放在底部

 

直接上代码

主代码:

 1 package sdut;
 2 
 3 import com.example.sdutfriends.R;
 4 
 5 import android.app.AlertDialog;
 6 import android.app.TabActivity;
 7 import android.content.DialogInterface;
 8 import android.content.Intent;
 9 import android.os.Bundle;
10 import android.view.KeyEvent;
11 import android.view.Window;
12 import android.widget.RadioGroup;
13 import android.widget.RadioGroup.OnCheckedChangeListener;
14 import android.widget.TabHost;
15 import android.widget.TabHost.TabSpec;
16 
17 public class SdutMainActivity extends TabActivity{
18     private TabHost mth;
19     private RadioGroup radioGroup;
20     @Override
21     protected void onCreate(Bundle savedInstanceState) {
22         // TODO Auto-generated method stub
23         super.onCreate(savedInstanceState);
24         //去除标题
25         requestWindowFeature(Window.FEATURE_NO_TITLE);
26         setContentView(R.layout.sdut_activity);
27         
28         //初始化底部菜单栏
29         mth = this.getTabHost();
30         
31         TabSpec str1 = mth.newTabSpec("function_1").setIndicator("功能1");
32         str1.setContent(new Intent(SdutMainActivity.this,SdutFindOldman.class));
33         mth.addTab(str1); 
34         
35         TabSpec str2 = mth.newTabSpec("function_2").setIndicator("功能2");
36         str2.setContent(new Intent(SdutMainActivity.this,SdutFindActivity.class));
37         mth.addTab(str2);
38         
39         TabSpec str3 = mth.newTabSpec("function_3").setIndicator("功能3");
40         str3.setContent(new Intent(SdutMainActivity.this,SdutFindOldman.class));
41         mth.addTab(str3);
42         
43         TabSpec str4 = mth.newTabSpec("function_4").setIndicator("功能4");
44         str4.setContent(new Intent(SdutMainActivity.this,SdutFindOldman.class));
45         mth.addTab(str4);
46         
47         TabSpec str5 = mth.newTabSpec("function_5").setIndicator("功能5");
48         str5.setContent(new Intent(SdutMainActivity.this,SdutFindOldman.class));
49         mth.addTab(str5);
50         
51         //底部菜单栏点击事件
52         radioGroup = (RadioGroup) findViewById(R.id.main_radio);
53         radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
54             
55             @Override
56             public void onCheckedChanged(RadioGroup group, int checkedId) {
57                 // TODO Auto-generated method stub
58                 switch(checkedId)
59                 {
60             case R.id.radio_button0:mth.setCurrentTabByTag("function_1");break;
61             case R.id.radio_button1:mth.setCurrentTabByTag("function_2");break;
62             case R.id.radio_button2:mth.setCurrentTabByTag("function_3");break;
63             case R.id.radio_button3:mth.setCurrentTabByTag("function_4");break;
64             case R.id.radio_button4:mth.setCurrentTabByTag("function_5");break;
65 
66                 }
67             }
68         });
69     }
70     
71     //设置 返回键的 按钮响应事件
72     @Override
73     public boolean dispatchKeyEvent(KeyEvent event) {
74         // TODO Auto-generated method stub
75          if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_BACK)
76           {
77            new AlertDialog.Builder(this).setCancelable(false).setTitle("温馨提示").setMessage("您确定要退出吗?").setPositiveButton("确定", new DialogInterface.OnClickListener()
78            {
79             public void onClick(DialogInterface dialog, int which)
80             {
81              finish();
82             }
83            }).setNegativeButton("取消", new DialogInterface.OnClickListener()
84            {
85             public void onClick(DialogInterface dialog, int which)
86             {
87             }
88            }).show();
89            return true;// 不知道返回true或是false有什么区别??
90           }
91 
92         return super.dispatchKeyEvent(event);
93     }
94 }
主代码


layout代码

  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:background="#334455" >                           //总体的背景  可以是图片   可以是颜色
  7     <LinearLayout 
  8         android:layout_width="fill_parent"
  9         android:layout_height="fill_parent"
 10         android:orientation="vertical"
 11         >
 12         <FrameLayout 
 13             android:id="@+id/msg_title"
 14             android:layout_width="fill_parent"
 15             android:layout_height="wrap_content"
 16             android:visibility="gone"
 17             >
 18             <TextView 
 19                 android:id="@+id/tv_wb"
 20                 android:layout_width="fill_parent"
 21                 android:layout_height="wrap_content"
 22                 android:background="@color/白色"/>
 23         </FrameLayout>
 24         <FrameLayout 
 25             android:id="@android:id/tabcontent"
 26             android:layout_width="fill_parent"
 27             android:layout_height="0.0dip"
 28             android:layout_weight="1.0"
 29             />
 30             <TabWidget
 31             android:id="@android:id/tabs"
 32             android:layout_width="fill_parent"
 33             android:layout_height="wrap_content"
 34             android:layout_weight="0.0"
 35             android:visibility="gone" />
 36             <RadioGroup
 37             android:id="@+id/main_radio"
 38             android:layout_width="fill_parent"
 39             android:layout_height="45dp"
 40             android:layout_gravity="bottom"
 41             
 42             android:gravity="center_vertical"
 43             android:orientation="horizontal" >
 44             <RadioButton
 45                 android:id="@+id/radio_button0"
 46                 style="@style/main_tab_bottom"
 47                 android:layout_height="fill_parent"
 48                 android:layout_marginTop="2.0dip"
 49                 android:textColor="@color/白色"                         
 50                 android:background="@color/黑色"
 51                 android:drawableTop="@drawable/icon"         //将图片放在文字上
 52                 android:tag="radio_button0"
 53                 android:text="标签1" />
 54             <RadioButton
 55                 android:id="@+id/radio_button1"
 56                 style="@style/main_tab_bottom"
 57                 android:layout_height="fill_parent"
 58                 android:layout_marginTop="2.0dip"
 59                 android:tag="radio_button1"
 60                 android:textColor="@color/白色"
 61                 android:background="@color/黑色"
 62                 android:drawableTop="@drawable/icon"
 63                 android:text="标签2" />
 64             <RadioButton
 65                 android:id="@+id/radio_button2"
 66                 style="@style/main_tab_bottom"
 67                 android:layout_height="fill_parent"
 68                 android:layout_marginTop="2.0dip"
 69                 android:textColor="@color/白色"
 70                 android:background="@color/黑色"
 71                 android:drawableTop="@drawable/icon"
 72                 android:tag="radio_button2"
 73                 android:text="标签3" />
 74             <RadioButton
 75                 android:id="@+id/radio_button3"
 76                 style="@style/main_tab_bottom"
 77                 android:layout_height="fill_parent"
 78                 android:layout_marginTop="2.0dip"
 79                 android:textColor="@color/白色"
 80                 android:background="@color/黑色"
 81                 android:drawableTop="@drawable/icon"
 82                 android:tag="radio_button3"
 83                 android:text="标签4" />
 84             <RadioButton
 85                 android:id="@+id/radio_button4"
 86                 style="@style/main_tab_bottom"
 87                 android:layout_height="fill_parent"
 88                 android:layout_marginTop="2.0dip"
 89                 android:textColor="@color/白色"
 90                 android:background="@color/黑色"
 91                 android:drawableTop="@drawable/icon"
 92                 android:tag="radio_button4"
 93                 android:text="标签5" />
 94         </RadioGroup>
 95             
 96         
 97     </LinearLayout>
 98     
 99     
100 
101 </TabHost>
layout


style.xml部分增加代码

 1     <style name="main_tab_bottom">
 2         <item name="android:textSize">10.0dip</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:paddingTop">1.0dip</item>
 7         <item name="android:layout_width">fill_parent</item>
 8         <item name="android:layout_height">fill_parent</item>
 9         <item name="android:button">@null</item>
10         <item name="android:singleLine">true</item>
11         <item name="android:drawablePadding">1.0dip</item>
12         <item name="android:layout_weight">1.0</item>
13     </style>
style

color.xml部分

<resources>
        <color name="黑色">#000000</color>
        <color name="白色">#FFFFFF</color>
</resources>

 

TabSpec str1 = mth.newTabSpec("function_1").setIndicator("功能1");
  str1.setContent(new Intent(SdutMainActivity.this,SdutFindOldman.class));
  mth.addTab(str1);
  
  TabSpec str2 = mth.newTabSpec("function_2").setIndicator("功能2");
  str2.setContent(new Intent(SdutMainActivity.this,SdutFindActivity.class));
  mth.addTab(str2);

 

这里的SdutFindOldman.class,,,SdutFindActivity.class代码就不贴了

效果图:

       

相关文章
|
3月前
|
移动开发 JavaScript C#
分享53戏源代码总有一个是你想要的(亲测每一个均可用)
分享53戏源代码总有一个是你想要的(亲测每一个均可用)
22 0
|
6月前
|
测试技术 开发工具 数据库
《移动互联网技术》第十一章 Android应用工程案例: 掌握Android系统的需求分析和设计以及 Android项目的程序测试和版本管理方法
《移动互联网技术》第十一章 Android应用工程案例: 掌握Android系统的需求分析和设计以及 Android项目的程序测试和版本管理方法
71 0
|
12月前
|
iOS开发 MacOS
iOS 的这个功能你肯定听过,但用对它能极大提升效率
iOS 的这个功能你肯定听过,但用对它能极大提升效率
|
uml 开发者 Windows
推荐5款冷门小工具,看一看有没有你喜欢的?
每个人的电脑中都会安装很多软件,可能还保留着很多不为人知的冷门软件。不过虽然冷门,但绝不意味着低能,相反很多冷门软件的功能十分出色。闲话少说,接下来我就给大家推荐5款冷门小工具,看一看有没有你喜欢的。
142 0
推荐5款冷门小工具,看一看有没有你喜欢的?
|
Web App开发 JSON JavaScript
仅20+行核心代码的Chrome插件,这不是有手就行吗?(上)
大家好,我是零一,不知道你们有没有开发过Chrome插件,但你们一定用得很多。其实Chrome插件的开发一点都不麻烦,基本就是有手就行,一个功能极简的插件核心代码也就不过30行左右。那你还怕不会插件的开发吗? 本文将手把手(真 · 手把手)介绍Chrome插件的开发,并末尾附带一个插件实战教学
326 0
仅20+行核心代码的Chrome插件,这不是有手就行吗?(上)
|
Android开发 开发者
android应用中去android市场去评分的功能实现(吐槽一波个人应用上线...)
一般的app可能会有这中功能,在应用中去android商店评分来提高排名,前段时间也把我的博客园上传到商店,这里不得不吐槽一些android商店的开放平台。 酷派,vivo,oppo,联想不支持个人开发者……. 有得还要软件著作证书,一个个人开发者,搞个应用练练手,哪有没精力和财力去搞这些玩意。
2337 0
|
前端开发 JavaScript 小程序
印象最深的一个bug——使用uinapp做混合开发静态图片在安卓端不显示
这几天一直在做混合开发,使用的是uni-app开发的,一套代码,多端使用,适用于各个平台。听起来很完美,使用过程不可多说,不知道是因为我们的需求变态还是我们团队两端技术水平太差。总之,开发联调过程十分痛苦,加上uniapp的调试十分困难,一度让我们两端互怼。这其中我印象最深的一个bug就是在对接联调总出现的
1664929 33
印象最深的一个bug——使用uinapp做混合开发静态图片在安卓端不显示
|
Web App开发 MySQL 关系型数据库
10款对开发者有用的Android应用
本文收集10款对开发者有用的Android应用,希望能对你的开发有所帮助。如果你还知道有其他对开发者很有用的Android应用?也分享出来吧。 列表如下: 1.Remote Desktop Client 该款应用帮你连接你的Windows电脑。
1100 0
|
Android开发
如何解决Android开发过程中缺乏UI素材的问题
如何解决开发过程中缺乏UI素材的问题 原则上来说,我们应该尽量把时间花在自己的专业开发上。但偶尔我们还是不得不面临需要UI素材问题,比如应付学校的项目作业,自己的个人开源项目,这些都是不太可能有UI设计师帮你完成的。
1417 0