Android仿微信底部菜单

    1334820232_9933.png
2012-4-19 17:08 上传
下载附件 (103.57 KB)

    就下面的那个底部栏,下面看一下实现代码吧!

    首先是布局main.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"
  3.   xmlns:android="http://schemas.android.com/apk/res/android">
  4.     <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
  5.         <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="0.0dip" android:layout_weight="1.0" />
  6.         <TabWidget android:id="@android:id/tabs" android:visibility="gone" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.0" />

  7.         <LinearLayout android:gravity="bottom" android:layout_gravity="bottom" android:orientation="horizontal" android:id="@+id/main_tab_group" android:background="@drawable/bottom1" android:paddingTop="2.0dip" android:layout_width="fill_parent" android:layout_height="wrap_content">
  8.             <FrameLayout android:background="@null" android:layout_width="0.0dip" android:layout_height="fill_parent" android:layout_weight="1.0">
  9.                 <LinearLayout android:id="@+id/main_layout_addExam" android:gravity="bottom|center" android:layout_width="fill_parent" android:layout_height="fill_parent">
  10.                     <RadioButton android:id="@+id/main_tab_addExam" android:checked="true" android:text="添加考试" android:drawableTop="@drawable/label_1" style="@style/MMTabButton" />
  11.                 </LinearLayout>
  12.                 <LinearLayout  android:gravity="top|right|center" android:paddingRight="10.0dip" android:layout_width="fill_parent" android:layout_height="fill_parent">
  13.                     <TextView android:textSize="10.0dip" android:text="1" android:textColor="#ffffff" android:gravity="center" android:id="@+id/main_tab_unread_tv" android:background="@drawable/new_icon_2" android:visibility="invisible" android:layout_width="wrap_content" android:layout_height="wrap_content"  />
  14.                 </LinearLayout>
  15.             </FrameLayout>
  16.              <FrameLayout android:background="@null" android:layout_width="0.0dip" android:layout_height="fill_parent" android:layout_weight="1.0">
  17.                 <LinearLayout android:id="@+id/main_layout_myExam" android:gravity="bottom|center" android:layout_width="fill_parent" android:layout_height="fill_parent">
  18.                      <RadioButton android:id="@+id/main_tab_myExam" android:text="我的考试" android:drawableTop="@drawable/label_2" style="@style/MMTabButton" />
  19.                 </LinearLayout>
  20.                 <LinearLayout android:gravity="top|right|center" android:paddingRight="10.0dip" android:layout_width="fill_parent" android:layout_height="fill_parent">
  21.                     <TextView android:textSize="10.0dip" android:textColor="#ffffff" android:gravity="center" android:id="@+id/main_tab_address" android:background="@drawable/new_icon_2" android:visibility="invisible" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1" />
  22.                 </LinearLayout>
  23.             </FrameLayout>
  24.             <FrameLayout android:background="@null" android:layout_width="0.0dip" android:layout_height="fill_parent" android:layout_weight="1.0">
  25.                 <LinearLayout android:id="@+id/main_layout_message" android:gravity="bottom|center" android:layout_width="fill_parent" android:layout_height="fill_parent">
  26.                     <RadioButton android:id="@+id/main_tab_message" android:text="考试资讯" android:drawableTop="@drawable/label_3" style="@style/MMTabButton" />
  27.                 </LinearLayout>
  28.                 <LinearLayout android:gravity="top|right|center" android:paddingRight="10.0dip" android:layout_width="fill_parent" android:layout_height="fill_parent">
  29.                     <TextView android:textSize="12.0dip" android:textColor="#ffffff" android:gravity="center" android:id="@+id/main_tab_new_tv" android:background="@drawable/new_icon_2" android:visibility="visible" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1" />
  30.                 </LinearLayout>
  31.             </FrameLayout>
  32.             <FrameLayout android:background="@null" android:layout_width="0.0dip" android:layout_height="fill_parent" android:layout_weight="1.0">
  33.                 <LinearLayout android:id="@+id/main_layout_settings" android:gravity="bottom|center" android:layout_width="fill_parent" android:layout_height="fill_parent">
  34.                     <RadioButton android:id="@+id/main_tab_settings" android:text="设置" android:drawableTop="@drawable/label4" style="@style/MMTabButton" />
  35.                 </LinearLayout>
  36.                 <LinearLayout android:gravity="top|right|center" android:paddingRight="10.0dip" android:layout_width="fill_parent" android:layout_height="fill_parent">
  37.                     <TextView android:textSize="10.0dip" android:textColor="#ffffff" android:gravity="center" android:id="@+id/main_tab_setting_new_tv" android:background="@drawable/new_icon_2" android:paddingLeft="6.0dip" android:paddingRight="6.0dip" android:visibility="invisible" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1" />
  38.                 </LinearLayout>
  39.             </FrameLayout>
  40.         </LinearLayout>
  41.     </LinearLayout>
  42. </TabHost>
复制代码
   其实就是用FrameLayout,微信也是这样布局的我反编译过。这样就可以灵活的布局那个红色的图标了。

      下面看一下activity切换代码:


  1. import android.app.TabActivity;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.View.OnClickListener;
  6. import android.view.Window;
  7. import android.widget.LinearLayout;
  8. import android.widget.RadioButton;
  9. import android.widget.TabHost;
  10. import android.widget.TextView;

  11. public class MainTabActivity extends TabActivity {

  12.         TabHost tabHost;
  13.     private TextView main_tab_unread_tv;
  14.         private RadioButton main_tab_addExam, main_tab_myExam,main_tab_message,main_tab_settings;
  15.         private LinearLayout main_layout_addExam,main_layout_myExam,main_layout_message,main_layout_settings;

  16.         public void onCreate(Bundle savedInstanceState) {
  17.                 super.onCreate(savedInstanceState);
  18.                 this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  19.                 setContentView(R.layout.main);

  20.                 initTab();
  21.                
  22.                 init();
  23.                
  24.         }

  25.         private void init() {

  26.                 main_tab_addExam = (RadioButton) findViewById(R.id.main_tab_addExam);

  27.                 main_tab_myExam = (RadioButton) findViewById(R.id.main_tab_myExam);
  28.                
  29.                 main_tab_message=(RadioButton) findViewById(R.id.main_tab_message);
  30.                
  31.                 main_tab_settings=(RadioButton) findViewById(R.id.main_tab_settings);
  32.                
  33.                 main_layout_addExam=(LinearLayout) findViewById(R.id.main_layout_addExam);
  34.                 main_layout_myExam=(LinearLayout) findViewById(R.id.main_layout_myExam);
  35.                 main_layout_message=(LinearLayout) findViewById(R.id.main_layout_message);
  36.                 main_layout_settings=(LinearLayout) findViewById(R.id.main_layout_settings);
  37.                
  38.                 main_tab_addExam.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.label_one_one), null, null);
  39.                 main_layout_addExam.setBackgroundResource(R.drawable.labelbg);
  40.                
  41.                 main_tab_unread_tv=(TextView) findViewById(R.id.main_tab_unread_tv);
  42.                 //main_tab_unread_tv.setVisibility(View.VISIBLE);
  43.                 //main_tab_unread_tv.setText("3");
  44.                
  45.                 main_tab_addExam.setOnClickListener(new OnClickListener() {
  46.                        
  47.                         public void onClick(View arg0) {
  48.                                 tabHost.setCurrentTabByTag("first");
  49.                                 /*main_tab_addExam.setBackgroundResource(R.drawable.label_one_one);
  50.                                 main_tab_myExam.setBackgroundResource(R.drawable.label_2);
  51.                                 main_tab_message.setBackgroundResource(R.drawable.label_3);*/
  52.                                 //main_tab_addExam.setCompoundDrawables(null, getResources().getDrawable(R.drawable.label_one_one), null, null);
  53.                                 main_tab_addExam.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.label_one_one), null, null);
  54.                                 main_tab_myExam.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.label_2), null, null);
  55.                                 main_tab_message.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.label_3), null, null);
  56.                                 main_tab_settings.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.label4), null, null);
  57.                                 main_layout_addExam.setBackgroundResource(R.drawable.labelbg);
  58.                                 main_layout_myExam.setBackgroundResource(R.drawable.mm_trans);
  59.                                 main_layout_message.setBackgroundResource(R.drawable.mm_trans);
  60.                                 main_layout_settings.setBackgroundResource(R.drawable.mm_trans);
  61.                         }
  62.                 });
  63.                
  64.                 main_tab_myExam.setOnClickListener(new OnClickListener() {
  65.                        
  66.                         public void onClick(View arg0) {
  67.                                 tabHost.setCurrentTabByTag("second");
  68.                                 /*main_tab_addExam.setBackgroundResource(R.drawable.label_1);
  69.                                 main_tab_myExam.setBackgroundResource(R.drawable.label_two_one);
  70.                                 main_tab_message.setBackgroundResource(R.drawable.label_3);*/
  71.                                 main_tab_addExam.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.label_1), null, null);
  72.                                 main_tab_myExam.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.label_two_one), null, null);
  73.                                 main_tab_message.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.label_3), null, null);
  74.                                 main_tab_settings.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.label4), null, null);
  75.                                 main_layout_addExam.setBackgroundResource(R.drawable.mm_trans);
  76.                                 main_layout_myExam.setBackgroundResource(R.drawable.labelbg);
  77.                                 main_layout_message.setBackgroundResource(R.drawable.mm_trans);
  78.                                 main_layout_settings.setBackgroundResource(R.drawable.mm_trans);
  79.                         }
  80.                 });
  81.                 main_tab_message.setOnClickListener(new OnClickListener() {
  82.                        
  83.                         public void onClick(View v) {
  84.                                 // TODO Auto-generated method stub
  85.                                 tabHost.setCurrentTabByTag("third");
  86.                                 /*main_tab_addExam.setBackgroundResource(R.drawable.label_1);
  87.                                 main_tab_myExam.setBackgroundResource(R.drawable.label_2);
  88.                                 main_tab_message.setBackgroundResource(R.drawable.label_three_one);*/
  89.                                 main_tab_addExam.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.label_1), null, null);
  90.                                 main_tab_myExam.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.label_2), null, null);
  91.                                 main_tab_message.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.label_three_one), null, null);
  92.                                 main_tab_settings.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.label4), null, null);
  93.                                 main_layout_addExam.setBackgroundResource(R.drawable.mm_trans);
  94.                                 main_layout_myExam.setBackgroundResource(R.drawable.mm_trans);
  95.                                 main_layout_message.setBackgroundResource(R.drawable.labelbg);
  96.                                 main_layout_settings.setBackgroundResource(R.drawable.mm_trans);
  97.                         }
  98.                 });
  99.                
  100.                 main_tab_settings.setOnClickListener(new OnClickListener() {
  101.                        
  102.                         public void onClick(View v) {
  103.                                 // TODO Auto-generated method stub
  104.                                 tabHost.setCurrentTabByTag("four");
  105.                                 main_tab_addExam.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.label_1), null, null);
  106.                                 main_tab_myExam.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.label_2), null, null);
  107.                                 main_tab_message.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.label_3), null, null);
  108.                                 main_tab_settings.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.label_four_one), null, null);
  109.                                 main_layout_addExam.setBackgroundResource(R.drawable.mm_trans);
  110.                                 main_layout_myExam.setBackgroundResource(R.drawable.mm_trans);
  111.                                 main_layout_message.setBackgroundResource(R.drawable.mm_trans);
  112.                                 main_layout_settings.setBackgroundResource(R.drawable.labelbg);
  113.                         }
  114.                 });
  115.         }
  116.        
  117.        
  118.         private void initTab(){
  119.                 tabHost=getTabHost();
  120.                 tabHost.addTab(tabHost.newTabSpec("first").setIndicator("first").setContent(
  121.                                 new Intent(this, AddExamActivity.class)));

  122.                 tabHost.addTab(tabHost.newTabSpec("second").setIndicator("second").setContent(
  123.                                 new Intent(this, MyExamActivity.class)));
  124.                
  125.                 tabHost.addTab(tabHost.newTabSpec("third").setIndicator("third").setContent(
  126.                                 new Intent(this, MessageActivity.class)));
  127.                
  128.                 tabHost.addTab(tabHost.newTabSpec("four").setIndicator("four").setContent(
  129.                                 new Intent(this, SettingActivity.class)));

  130.         }

  131. }
复制代码
setCompoundDrawablesWithIntrinsicBounds方法是我用来设置顶部图片的,用来替换android:drawableTop这个属性的背景图,至于为什么那么麻烦每次都要设置背景图片颜色,是因为没用用RadioGroup包含RadioButton,所以RadioButton就不能做到单个切换。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值