底部菜单栏

来源:互联网 发布:软件与云计算 编辑:程序博客网 时间:2024/04/28 23:37
先贴图看效果

底部固定菜单栏 

Activity代码:
  1. package com.jay.test;


  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.widget.ImageButton;
  5. import android.widget.TabHost;
  6. import android.widget.TabHost.OnTabChangeListener;
  7. import android.widget.TabHost.TabSpec;

  8. public class BottmoMenuBarActivity extends Activity {
  9.     private TabHost tabs;
  10.         private ImageButton footer_tab_btn1;
  11.         private ImageButton footer_tab_btn2;
  12.         private ImageButton footer_tab_btn3;
  13.         private ImageButton footer_tab_btn4;
  14.         private ImageButton footer_tab_btn5;
  15.         private ImageButton footer_tab_btn6;
  16.         private ImageButton footer_tab_btn7;

  17.         /** Called when the activity is first created. */
  18.     @Override
  19.     public void onCreate(Bundle savedInstanceState) {
  20.         super.onCreate(savedInstanceState);
  21.         setContentView(R.layout.main);
  22.         
  23.         tabs = null;
  24.                 tabs = (TabHost)findViewById(R.id.tabhost);
  25.                 tabs.setup();
  26.         tabs.setOnTabChangedListener(TabChangeListener);
  27.         //设置Tab1
  28.         footer_tab_btn1 = new ImageButton(this);
  29.         footer_tab_btn1.setBackgroundResource(R.drawable.bottom_home_button);
  30.         TabSpec tab1 = tabs.newTabSpec("home");  
  31.         tab1.setIndicator(footer_tab_btn1);      // 设置tab1的名称
  32.         tab1.setContent(R.id.text1);    // 关联控件  
  33.         tabs.addTab(tab1);                // 添加tab1  
  34.         
  35.           
  36.         //设置Tab2
  37.         footer_tab_btn2 = new ImageButton(this);
  38.         footer_tab_btn2.setBackgroundResource(R.drawable.bottom_book_button);
  39.         TabSpec tab2 = tabs.newTabSpec("book");  
  40.         tab2.setIndicator(footer_tab_btn2);  
  41.         tab2.setContent(R.id.text2);    
  42.         tabs.addTab(tab2);                
  43.         
  44.         //设置Tab3  
  45.         footer_tab_btn3 = new ImageButton(this);
  46.         footer_tab_btn3.setBackgroundResource(R.drawable.bottom_video_button);
  47.         TabSpec tab3 = tabs.newTabSpec("video");  
  48.         tab3.setIndicator(footer_tab_btn3);        
  49.         tab3.setContent(R.id.text3);   
  50.         tabs.addTab(tab3);                

  51.         //设置Tab4  
  52.         footer_tab_btn4 = new ImageButton(this);
  53.         footer_tab_btn4.setBackgroundResource(R.drawable.bottom_magazine_button);
  54.         TabSpec tab4 = tabs.newTabSpec("magazine");  
  55.         tab4.setIndicator(footer_tab_btn4);        
  56.         tab4.setContent(R.id.text4);     
  57.         tabs.addTab(tab4);    
  58.         
  59.         // 设置Tab5
  60.         footer_tab_btn5 = new ImageButton(this);
  61.         footer_tab_btn5.setBackgroundResource(R.drawable.bottom_paper_button);
  62.         TabSpec tab5 = tabs.newTabSpec("paper");  
  63.         tab5.setIndicator(footer_tab_btn5);        
  64.         tab5.setContent(R.id.text5);     
  65.         tabs.addTab(tab5);    
  66.         
  67.         // 设置Tab6
  68.         footer_tab_btn6 = new ImageButton(this);
  69.         footer_tab_btn6.setBackgroundResource(R.drawable.bottom_thesis_button);
  70.         TabSpec tab6 = tabs.newTabSpec("thesis");  
  71.         tab6.setIndicator(footer_tab_btn6);        
  72.         tab6.setContent(R.id.text6);     
  73.         tabs.addTab(tab6);    
  74.         
  75.         // 设置Tab7
  76.         footer_tab_btn7 = new ImageButton(this);
  77.         footer_tab_btn7.setBackgroundResource(R.drawable.bottom_library_button);
  78.         TabSpec tab7 = tabs.newTabSpec("library");  
  79.         tab7.setIndicator(footer_tab_btn7);        
  80.         tab7.setContent(R.id.text7);       
  81.         tabs.addTab(tab7);    
  82.     }
  83.     private OnTabChangeListener TabChangeListener  = new OnTabChangeListener() {
  84.                 
  85.                 @Override
  86.                 public void onTabChanged(String tabId) {
  87.                         int j = tabs.getTabWidget().getTabCount();
  88.                         ImageButton currentView =(ImageButton) tabs.getCurrentTabView(); 
  89.                         for (int i = 0; i < j ; i++){
  90.                                 if(tabs.getCurrentTab() == i){
  91.                                         currentView.setEnabled(false);
  92.                                 }else{
  93.                                         if(tabs.getTabWidget().getChildTabViewAt(i) != null){
  94.                                                 ((ImageButton)tabs.getTabWidget().getChildTabViewAt(i)).setEnabled(true);
  95.                                         }
  96.                                 }
  97.                         }
  98.                 }
  99.         };;
  100. }
复制代码
一些注意点:
1.没有使用TabActivity,这是在3.0中不建议使用的。
2.setContent()函数除了直接添加视图ID,还可以添加intent直接在Tab的内容部分打开一个activity。如需要了解详细实现过程可自行搜索,也可以发邮件至我的邮箱jayzhou215@163.com
3.重载onTabChanged(String tabId) 函数的目的是实现按下面按钮时变蓝的效果,具体请查看源码
4.之所以菜单栏会在底部显示是在TabHost和TabWidget之间加了一层RelativeLayout,将TabWidget设置为alignParentBottom即可。
main.xml源码:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3.     xmlns:android="http://schemas.android.com/apk/res/android"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.         android:layout_width="fill_parent"
  6.         android:layout_height="fill_parent"
  7.         >
  8.         <TabHost 
  9.                 android:layout_width="fill_parent"
  10.                 android:layout_height="fill_parent"
  11.                 android:id="@+id/tabhost"
  12.             tools:ignore="UselessParent" >
  13.             <RelativeLayout
  14.                         android:layout_width="fill_parent"
  15.                         android:layout_height="match_parent"
  16.                         >
  17.                     <TabWidget
  18.                         android:id="@android:id/tabs"
  19.                         android:layout_width="fill_parent"
  20.                         android:layout_height="50dp"
  21.                         android:layout_alignParentBottom="true"
  22.                         >
  23.                     </TabWidget>
  24.                 
  25.                     <FrameLayout
  26.                         android:id="@android:id/tabcontent"
  27.                         android:layout_width="fill_parent"
  28.                         android:layout_height="fill_parent"
  29.                                 android:layout_above="@android:id/tabs"
  30.                         >
  31.                         <TextView 
  32.                                 android:layout_width="fill_parent"
  33.                                 android:layout_height="fill_parent"
  34.                                 android:text="@string/text1"
  35.                                 android:id="@+id/text1"
  36.                             />
  37.                         <TextView 
  38.                                 android:layout_width="fill_parent"
  39.                                 android:layout_height="fill_parent"
  40.                                 android:text="@string/text2"
  41.                                 android:id="@+id/text2"
  42.                             />
  43.                         <TextView 
  44.                                 android:layout_width="fill_parent"
  45.                                 android:layout_height="fill_parent"
  46.                                 android:text="@string/text3"
  47.                                 android:id="@+id/text3"
  48.                             />
  49.                         <TextView 
  50.                                 android:layout_width="fill_parent"
  51.                                 android:layout_height="fill_parent"
  52.                                 android:text="@string/text4"
  53.                                 android:id="@+id/text4"
  54.                             />
  55.                         <TextView 
  56.                                 android:layout_width="fill_parent"
  57.                                 android:layout_height="fill_parent"
  58.                                 android:text="@string/text5"
  59.                                 android:id="@+id/text5"
  60.                             />
  61.                         <TextView 
  62.                                 android:layout_width="fill_parent"
  63.                                 android:layout_height="fill_parent"
  64.                                 android:text="@string/text6"
  65.                                 android:id="@+id/text6"
  66.                             />
  67.                         <TextView 
  68.                                 android:layout_width="fill_parent"
  69.                                 android:layout_height="fill_parent"
  70.                                 android:text="@string/text7"
  71.                                 android:id="@+id/text7"
  72.                             />
  73.                     </FrameLayout>
  74.                 </RelativeLayout>
  75.         </TabHost>
  76. </RelativeLayout>