先贴图看效果
Activity代码:
package com.jay.test; import android.app.Activity; import android.os.Bundle; import android.widget.ImageButton; import android.widget.TabHost; import android.widget.TabHost.OnTabChangeListener; import android.widget.TabHost.TabSpec; public class BottmoMenuBarActivity extends Activity { private TabHost tabs; private ImageButton footer_tab_btn1; private ImageButton footer_tab_btn2; private ImageButton footer_tab_btn3; private ImageButton footer_tab_btn4; private ImageButton footer_tab_btn5; private ImageButton footer_tab_btn6; private ImageButton footer_tab_btn7; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tabs = null; tabs = (TabHost)findViewById(R.id.tabhost); tabs.setup(); tabs.setOnTabChangedListener(TabChangeListener); //设置Tab1 footer_tab_btn1 = new ImageButton(this); footer_tab_btn1.setBackgroundResource(R.drawable.bottom_home_button); TabSpec tab1 = tabs.newTabSpec("home"); tab1.setIndicator(footer_tab_btn1); // 设置tab1的名称 tab1.setContent(R.id.text1); // 关联控件 tabs.addTab(tab1); // 添加tab1 //设置Tab2 footer_tab_btn2 = new ImageButton(this); footer_tab_btn2.setBackgroundResource(R.drawable.bottom_book_button); TabSpec tab2 = tabs.newTabSpec("book"); tab2.setIndicator(footer_tab_btn2); tab2.setContent(R.id.text2); tabs.addTab(tab2); //设置Tab3 footer_tab_btn3 = new ImageButton(this); footer_tab_btn3.setBackgroundResource(R.drawable.bottom_video_button); TabSpec tab3 = tabs.newTabSpec("video"); tab3.setIndicator(footer_tab_btn3); tab3.setContent(R.id.text3); tabs.addTab(tab3); //设置Tab4 footer_tab_btn4 = new ImageButton(this); footer_tab_btn4.setBackgroundResource(R.drawable.bottom_magazine_button); TabSpec tab4 = tabs.newTabSpec("magazine"); tab4.setIndicator(footer_tab_btn4); tab4.setContent(R.id.text4); tabs.addTab(tab4); // 设置Tab5 footer_tab_btn5 = new ImageButton(this); footer_tab_btn5.setBackgroundResource(R.drawable.bottom_paper_button); TabSpec tab5 = tabs.newTabSpec("paper"); tab5.setIndicator(footer_tab_btn5); tab5.setContent(R.id.text5); tabs.addTab(tab5); // 设置Tab6 footer_tab_btn6 = new ImageButton(this); footer_tab_btn6.setBackgroundResource(R.drawable.bottom_thesis_button); TabSpec tab6 = tabs.newTabSpec("thesis"); tab6.setIndicator(footer_tab_btn6); tab6.setContent(R.id.text6); tabs.addTab(tab6); // 设置Tab7 footer_tab_btn7 = new ImageButton(this); footer_tab_btn7.setBackgroundResource(R.drawable.bottom_library_button); TabSpec tab7 = tabs.newTabSpec("library"); tab7.setIndicator(footer_tab_btn7); tab7.setContent(R.id.text7); tabs.addTab(tab7); } private OnTabChangeListener TabChangeListener = new OnTabChangeListener() { @Override public void onTabChanged(String tabId) { int j = tabs.getTabWidget().getTabCount(); ImageButton currentView =(ImageButton) tabs.getCurrentTabView(); for (int i = 0; i < j ; i++){ if(tabs.getCurrentTab() == i){ currentView.setEnabled(false); }else{ if(tabs.getTabWidget().getChildTabViewAt(i) != null){ ((ImageButton)tabs.getTabWidget().getChildTabViewAt(i)).setEnabled(true); } } } } };; }
一些注意点:
1.没有使用TabActivity,这是在3.0中不建议使用的。
2.setContent()函数除了直接添加视图ID,还可以添加intent直接在Tab的内容部分打开一个activity。
3.重载onTabChanged(String tabId)
函数的目的是实现按下面按钮时变蓝的效果,具体请查看源码
4.之所以菜单栏会在底部显示是在TabHost和TabWidget之间加了一层RelativeLayout,将TabWidget设置为alignParentBottom即可。
main.xml源码:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TabHost android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tabhost" tools:ignore="UselessParent" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="match_parent" > <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="50dp" android:layout_alignParentBottom="true" > </TabWidget> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_above="@android:id/tabs" > <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="@string/text1" android:id="@+id/text1" /> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="@string/text2" android:id="@+id/text2" /> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="@string/text3" android:id="@+id/text3" /> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="@string/text4" android:id="@+id/text4" /> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="@string/text5" android:id="@+id/text5" /> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="@string/text6" android:id="@+id/text6" /> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="@string/text7" android:id="@+id/text7" /> </FrameLayout> </RelativeLayout> </TabHost> </RelativeLayout>