一、使用GridView布局
配置文件
<GridView android:id="@+id/GridView_toolbar"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:layout_alignParentBottom="true"></GridView>
// 创建底部菜单 Toolbar
toolbarGrid = (GridView) findViewById(R.id.GridView_toolbar);
toolbarGrid.setBackgroundResource(R.drawable.channelgallery_bg);// 设置背景
toolbarGrid.setNumColumns(5);// 设置每行列数
toolbarGrid.setGravity(Gravity.CENTER);// 位置居中
toolbarGrid.setVerticalSpacing(10);// 垂直间隔
toolbarGrid.setHorizontalSpacing(10);// 水平间隔
toolbarGrid.setAdapter(getMenuAdapter(menu_toolbar_name_array,
menu_toolbar_image_array));// 设置菜单Adapter
/** 监听底部菜单选项 **/
toolbarGrid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(MainMenuView.this,
menu_toolbar_name_array[arg2], Toast.LENGTH_SHORT)
.show();
switch (arg2) {
case TOOLBAR_ITEM_PAGEHOME:
break;
case TOOLBAR_ITEM_BACK:
break;
case TOOLBAR_ITEM_FORWARD:
break;
case TOOLBAR_ITEM_NEW:
break;
case TOOLBAR_ITEM_MENU:
menuDialog.show();
break;
}
}
});
二、使用tabHost来使用
http://flysnow.iteye.com/blog/946564
1、activity继承TabActivity
2、配置文件,使用自定义RadioButton取代,默认的tab风格,但是要不可见
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <!-- tabhost中的一个成员对象--> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="0.0dip" android:layout_weight="1.0"/> <!-- 设置不可见了 --> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.0" android:visibility="visible"/> <RadioGroup android:id="@+id/main_tab" android:background="@drawable/maintab_toolbar_bg" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:layout_gravity="bottom"> <RadioButton android:layout_marginTop="2.0dip" android:text="@string/main_home" android:drawableTop="@drawable/icon_1_n" android:id="@+id/radio_button0" style="@style/main_tab_bottom"/> <RadioButton android:layout_marginTop="2.0dip" android:text="@string/main_news" android:drawableTop="@drawable/icon_2_n" android:id="@+id/radio_button1" style="@style/main_tab_bottom"/> <RadioButton android:layout_marginTop="2.0dip" android:text="@string/main_my_info" android:drawableTop="@drawable/icon_3_n" android:id="@+id/radio_button2" style="@style/main_tab_bottom"/> <RadioButton android:layout_marginTop="2.0dip" android:text="@string/menu_search" android:drawableTop="@drawable/icon_4_n" android:id="@+id/radio_button3" style="@style/main_tab_bottom"/> <RadioButton android:layout_marginTop="2.0dip" android:text="@string/more" android:drawableTop="@drawable/icon_5_n" android:id="@+id/radio_button4" style="@style/main_tab_bottom"/> </RadioGroup> </LinearLayout> </TabHost>
4、activity中使用TabHost localHost=getTabHost();,新建选项卡,设置intent
/**
* 构建TabHost的Tab页
* @param tag 标记
* @param resLabel 标签
* @param resIcon 图标
* @param content 该tab展示的内容
* @return 一个tab
*/
private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon,final Intent content) {
return this.mTabHost.newTabSpec(tag).setIndicator(getString(resLabel),
getResources().getDrawable(resIcon)).setContent(content);
}
5、使用此切换Activity,mTabHost.setCurrentTabByTag(TAB_TAG_HOME);就会自动调用intent
三、使用Fragment来布局
![](http://dl.iteye.com/upload/attachment/0082/5517/19f411cc-da36-3171-8838-90cc98e6c366.jpg)
主页面 底部菜单采用自定义LinerLayOut生成。需要标记当前选中了哪一个。为每一个View配置监听器。
在MainActivity中实现显示与隐藏fragment
private void setFragmentIndicator(int whichIsDefault) {
mFragments = new Fragment[3];
mFragments[0] = getSupportFragmentManager().findFragmentById(R.id.fragment_home);
mFragments[1] = getSupportFragmentManager().findFragmentById(R.id.fragment_search);
mFragments[2] = getSupportFragmentManager().findFragmentById(R.id.fragment_settings);
getSupportFragmentManager().beginTransaction().hide(mFragments[0])
.hide(mFragments[1]).hide(mFragments[2]).show(mFragments[whichIsDefault]).commit();
FragmentIndicator mIndicator = (FragmentIndicator) findViewById(R.id.indicator);
FragmentIndicator.setIndicator(whichIsDefault);
mIndicator.setOnIndicateListener(new OnIndicateListener() {
@Override
public void onIndicate(View v, int which) {
getSupportFragmentManager().beginTransaction()
.hide(mFragments[0]).hide(mFragments[1])
.hide(mFragments[2]).show(mFragments[which]).commit();
}
});
}