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

将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代码就不贴了

效果图:

       

posted @ 2013-06-04 10:16  听着music睡  阅读(573)  评论(0编辑  收藏  举报