android底部菜單欄實(shí)現(xiàn)原理與代碼
更新時間:2013年01月02日 16:35:13 作者:
底部菜單欄很重要,我看了一下很多應(yīng)用軟件都是用了底部菜單欄做,我這里使用了tabhost做了一種通用的(就是可以像微信那樣顯示未讀消息數(shù)量的,雖然之前也做過但是layout下的xml寫的太臃腫,這里去掉了很多不必要的層,個人看起來還是不錯的,所以貼出來方便以后使用
上一個項(xiàng)目已經(jīng)做完了,這周基本上沒事,所以整理了下以前的項(xiàng)目,想把一些通用的部分封裝起來,這樣以后遇到相似的項(xiàng)目就不用重復(fù)發(fā)明輪子了,也節(jié)省了開發(fā)效率。今天把demo貼出來一是方便以后自己查詢,二是希望同時也能幫到大家。
底部菜單欄很重要,我看了一下很多應(yīng)用軟件都是用了底部菜單欄做。我這里使用了tabhost做了一種通用的(就是可以像微信那樣顯示未讀消息數(shù)量的,雖然之前也做過但是layout下的xml寫的太臃腫,這里去掉了很多不必要的層,個人看起來還是不錯的,所以貼出來方便以后使用)。
先看一下做出來之后的效果:
以后使用的時候就可以換成自己項(xiàng)目的圖片和字體了,主框架不用變哈哈,
首先是要布局layout下xml文件 main.xml:
<?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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/bg_gray"
android:orientation="vertical" >
<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="gone" />
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RadioGroup
android:id="@+id/main_tab_group"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@drawable/bottom1"
android:gravity="bottom"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/main_tab_addExam"
style="@style/MMTabButton"
android:layout_weight="1.0"
android:drawableTop="@drawable/bg_checkbox_icon_menu_0"
android:text="添加考試" />
<RadioButton
android:id="@+id/main_tab_myExam"
style="@style/MMTabButton"
android:layout_weight="1.0"
android:checked="true"
android:drawableTop="@drawable/bg_checkbox_icon_menu_1"
android:text="我的考試" />
<RadioButton
android:id="@+id/main_tab_message"
style="@style/MMTabButton"
android:layout_weight="1.0"
android:drawableTop="@drawable/bg_checkbox_icon_menu_2"
android:text="我的通知" />
<RadioButton
android:id="@+id/main_tab_settings"
style="@style/MMTabButton"
android:layout_weight="1.0"
android:drawableTop="@drawable/bg_checkbox_icon_menu_3"
android:text="設(shè)置" />
</RadioGroup>
<TextView
android:id="@+id/main_tab_new_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|top"
android:layout_marginLeft="60dip"
android:layout_marginTop="1dip"
android:background="@drawable/tips"
android:gravity="center"
android:text="1"
android:textColor="#ffffff"
android:textSize="10sp"
android:visibility="gone" />
</FrameLayout>
</LinearLayout>
</TabHost>
在RadioGroup的外面加了一個FrameLayout,主要是為了使用TextView顯示消息數(shù)量,這里是居中靠左60dip,可能你會問直接寫死能支持多分辨率嗎,這個我在320*480的手機(jī)上試過沒問題的,因?yàn)閐ip是與設(shè)備無關(guān)的支持多分辨率,至于1280*800平板電腦這樣的分辨率我就不能保證了,哈哈!
接下來是樣式布局:
<style name="MMTabButton">
<item name="android:textSize">12.0dip</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:background">@drawable/bg_checkbox_menus</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:button">@null</item>
<item name="android:textColor">@color/white</item>
<item name="android:layout_weight">1.0</item>
<item name="android:paddingBottom">2.0dip</item>
<item name="android:paddingTop">2.0dip</item>
</style>
在drawable下bg_checkbox_menus.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="false"
android:drawable="@drawable/mm_trans" />
<item
android:state_checked="true"
android:drawable="@drawable/home_btn_bg" />
</selector>
其他的那四個都合這個一樣點(diǎn)擊后圖片換成亮色的,所以就不一一貼出來了。
最后看MainActivity這個類:
package cn.com.karl.test;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
import android.widget.TextView;
public class MainActivity extends TabActivity {
/** Called when the activity is first created. */
private TabHost tabHost;
private TextView main_tab_new_message;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
main_tab_new_message=(TextView) findViewById(R.id.main_tab_new_message);
main_tab_new_message.setVisibility(View.VISIBLE);
main_tab_new_message.setText("10");
tabHost=this.getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent=new Intent().setClass(this, AddExamActivity.class);
spec=tabHost.newTabSpec("添加考試").setIndicator("添加考試").setContent(intent);
tabHost.addTab(spec);
intent=new Intent().setClass(this,MyExamActivity.class);
spec=tabHost.newTabSpec("我的考試").setIndicator("我的考試").setContent(intent);
tabHost.addTab(spec);
intent=new Intent().setClass(this, MyMessageActivity.class);
spec=tabHost.newTabSpec("我的通知").setIndicator("我的通知").setContent(intent);
tabHost.addTab(spec);
intent=new Intent().setClass(this, SettingActivity.class);
spec=tabHost.newTabSpec("設(shè)置").setIndicator("設(shè)置").setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(1);
RadioGroup radioGroup=(RadioGroup) this.findViewById(R.id.main_tab_group);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.main_tab_addExam://添加考試
tabHost.setCurrentTabByTag("添加考試");
break;
case R.id.main_tab_myExam://我的考試
tabHost.setCurrentTabByTag("我的考試");
break;
case R.id.main_tab_message://我的通知
tabHost.setCurrentTabByTag("我的通知");
break;
case R.id.main_tab_settings://設(shè)置
tabHost.setCurrentTabByTag("設(shè)置");
break;
default:
//tabHost.setCurrentTabByTag("我的考試");
break;
}
}
});
}
}
這樣就完成了,主要還是使用了tabhost完成,tabhost有緩存機(jī)制這四個界面都會緩存到內(nèi)存中,這樣即有利也有弊,有利是因?yàn)榍袚Q的時候不用在重新加載了,有弊是因?yàn)榫彺嫠膫€界面會耗費(fèi)內(nèi)存較多一些。如果只想緩存一個界面以后下一篇我會使用ActivityGroup實(shí)現(xiàn)頂部滑動欄,就像網(wǎng)易新聞的頂部滑動欄我相信也是只緩存了一個界面,切換的時候是從數(shù)據(jù)庫加載的,所以第二次滑動加載會比較快。
底部菜單欄很重要,我看了一下很多應(yīng)用軟件都是用了底部菜單欄做。我這里使用了tabhost做了一種通用的(就是可以像微信那樣顯示未讀消息數(shù)量的,雖然之前也做過但是layout下的xml寫的太臃腫,這里去掉了很多不必要的層,個人看起來還是不錯的,所以貼出來方便以后使用)。
先看一下做出來之后的效果:

以后使用的時候就可以換成自己項(xiàng)目的圖片和字體了,主框架不用變哈哈,
首先是要布局layout下xml文件 main.xml:
復(fù)制代碼 代碼如下:
<?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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/bg_gray"
android:orientation="vertical" >
<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="gone" />
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RadioGroup
android:id="@+id/main_tab_group"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@drawable/bottom1"
android:gravity="bottom"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/main_tab_addExam"
style="@style/MMTabButton"
android:layout_weight="1.0"
android:drawableTop="@drawable/bg_checkbox_icon_menu_0"
android:text="添加考試" />
<RadioButton
android:id="@+id/main_tab_myExam"
style="@style/MMTabButton"
android:layout_weight="1.0"
android:checked="true"
android:drawableTop="@drawable/bg_checkbox_icon_menu_1"
android:text="我的考試" />
<RadioButton
android:id="@+id/main_tab_message"
style="@style/MMTabButton"
android:layout_weight="1.0"
android:drawableTop="@drawable/bg_checkbox_icon_menu_2"
android:text="我的通知" />
<RadioButton
android:id="@+id/main_tab_settings"
style="@style/MMTabButton"
android:layout_weight="1.0"
android:drawableTop="@drawable/bg_checkbox_icon_menu_3"
android:text="設(shè)置" />
</RadioGroup>
<TextView
android:id="@+id/main_tab_new_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|top"
android:layout_marginLeft="60dip"
android:layout_marginTop="1dip"
android:background="@drawable/tips"
android:gravity="center"
android:text="1"
android:textColor="#ffffff"
android:textSize="10sp"
android:visibility="gone" />
</FrameLayout>
</LinearLayout>
</TabHost>
在RadioGroup的外面加了一個FrameLayout,主要是為了使用TextView顯示消息數(shù)量,這里是居中靠左60dip,可能你會問直接寫死能支持多分辨率嗎,這個我在320*480的手機(jī)上試過沒問題的,因?yàn)閐ip是與設(shè)備無關(guān)的支持多分辨率,至于1280*800平板電腦這樣的分辨率我就不能保證了,哈哈!
接下來是樣式布局:
復(fù)制代碼 代碼如下:
<style name="MMTabButton">
<item name="android:textSize">12.0dip</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:background">@drawable/bg_checkbox_menus</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:button">@null</item>
<item name="android:textColor">@color/white</item>
<item name="android:layout_weight">1.0</item>
<item name="android:paddingBottom">2.0dip</item>
<item name="android:paddingTop">2.0dip</item>
</style>
在drawable下bg_checkbox_menus.xml
復(fù)制代碼 代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="false"
android:drawable="@drawable/mm_trans" />
<item
android:state_checked="true"
android:drawable="@drawable/home_btn_bg" />
</selector>
其他的那四個都合這個一樣點(diǎn)擊后圖片換成亮色的,所以就不一一貼出來了。
最后看MainActivity這個類:
復(fù)制代碼 代碼如下:
package cn.com.karl.test;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
import android.widget.TextView;
public class MainActivity extends TabActivity {
/** Called when the activity is first created. */
private TabHost tabHost;
private TextView main_tab_new_message;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
main_tab_new_message=(TextView) findViewById(R.id.main_tab_new_message);
main_tab_new_message.setVisibility(View.VISIBLE);
main_tab_new_message.setText("10");
tabHost=this.getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent=new Intent().setClass(this, AddExamActivity.class);
spec=tabHost.newTabSpec("添加考試").setIndicator("添加考試").setContent(intent);
tabHost.addTab(spec);
intent=new Intent().setClass(this,MyExamActivity.class);
spec=tabHost.newTabSpec("我的考試").setIndicator("我的考試").setContent(intent);
tabHost.addTab(spec);
intent=new Intent().setClass(this, MyMessageActivity.class);
spec=tabHost.newTabSpec("我的通知").setIndicator("我的通知").setContent(intent);
tabHost.addTab(spec);
intent=new Intent().setClass(this, SettingActivity.class);
spec=tabHost.newTabSpec("設(shè)置").setIndicator("設(shè)置").setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(1);
RadioGroup radioGroup=(RadioGroup) this.findViewById(R.id.main_tab_group);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.main_tab_addExam://添加考試
tabHost.setCurrentTabByTag("添加考試");
break;
case R.id.main_tab_myExam://我的考試
tabHost.setCurrentTabByTag("我的考試");
break;
case R.id.main_tab_message://我的通知
tabHost.setCurrentTabByTag("我的通知");
break;
case R.id.main_tab_settings://設(shè)置
tabHost.setCurrentTabByTag("設(shè)置");
break;
default:
//tabHost.setCurrentTabByTag("我的考試");
break;
}
}
});
}
}
這樣就完成了,主要還是使用了tabhost完成,tabhost有緩存機(jī)制這四個界面都會緩存到內(nèi)存中,這樣即有利也有弊,有利是因?yàn)榍袚Q的時候不用在重新加載了,有弊是因?yàn)榫彺嫠膫€界面會耗費(fèi)內(nèi)存較多一些。如果只想緩存一個界面以后下一篇我會使用ActivityGroup實(shí)現(xiàn)頂部滑動欄,就像網(wǎng)易新聞的頂部滑動欄我相信也是只緩存了一個界面,切換的時候是從數(shù)據(jù)庫加載的,所以第二次滑動加載會比較快。
您可能感興趣的文章:
- Android左右滑出菜單實(shí)例分析
- android popwindow實(shí)現(xiàn)左側(cè)彈出菜單層及PopupWindow主要方法介紹
- 基于Android實(shí)現(xiàn)點(diǎn)擊某個按鈕讓菜單選項(xiàng)從按鈕周圍指定位置彈出
- Android ListView長按彈出菜單二種實(shí)現(xiàn)方式示例
- Android界面設(shè)計(jì)(APP設(shè)計(jì)趨勢 左側(cè)隱藏菜單右邊顯示content)
- Android開發(fā)技巧之我的菜單我做主(自定義菜單)
- Android仿QQ空間底部菜單示例代碼
- android studio 的下拉菜單Spinner使用詳解
- Android實(shí)現(xiàn)原生側(cè)滑菜單的超簡單方式
- Android學(xué)習(xí)之菜單的使用方法
相關(guān)文章
Android RecyclerView緩存復(fù)用原理解析
RecyclerView是Android一個更強(qiáng)大的控件,其不僅可以實(shí)現(xiàn)和ListView同樣的效果,還有優(yōu)化了ListView中的各種不足。其可以實(shí)現(xiàn)數(shù)據(jù)縱向滾動,也可以實(shí)現(xiàn)橫向滾動(ListView做不到橫向滾動)。接下來講解RecyclerView的用法2022-11-11Android實(shí)現(xiàn)手指觸控圖片縮放功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)手指觸控圖片縮放功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12總結(jié)安卓(Android)中常用的跳轉(zhuǎn)工具
在大家日常開發(fā)的時候經(jīng)常會用到各式各樣的跳轉(zhuǎn),如跳轉(zhuǎn)到QQ、微信聊天界面、跳轉(zhuǎn)到聯(lián)系人界面或者跳轉(zhuǎn)到瀏覽器和照相機(jī)等等之類的,本文將常用到的一些跳轉(zhuǎn)集合到一起,這樣更方便大家以后使用,有需要的小伙伴們可以參考借鑒。2016-08-08Android 自定義View實(shí)現(xiàn)單擊和雙擊事件的方法
下面小編就為大家?guī)硪黄狝ndroid 自定義View實(shí)現(xiàn)單擊和雙擊事件的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-09-09Android開發(fā)實(shí)現(xiàn)應(yīng)用層面屏蔽狀態(tài)欄的方法小結(jié)
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)應(yīng)用層面屏蔽狀態(tài)欄的方法,結(jié)合實(shí)例形式分析了Android屏蔽狀態(tài)欄的相關(guān)函數(shù)調(diào)用、權(quán)限控制及函數(shù)重寫等相關(guān)操作技巧,需要的朋友可以參考下2017-08-08Android開發(fā)手冊Chip監(jiān)聽及ChipGroup監(jiān)聽
這篇文章主要為大家介紹了Android開發(fā)手冊Chip監(jiān)聽及ChipGroup監(jiān)聽,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06