Android仿微信底部菜單欄功能顯示未讀消息數(shù)量
底部菜單欄很重要,我看了一下很多應(yīng)用軟件都是用了底部菜單欄,這里使用了tabhost做了一種通用的(就是可以像微信那樣顯示未讀消息數(shù)量的,雖然之前也做過但是layout下的xml寫的太臃腫,這里去掉了很多不必要的層,個(gè)人看起來還是不錯(cuò)的,所以貼出來方便以后使用)。
先看一下做出來之后的效果:
以后使用的時(shí)候就可以換成自己項(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的外面加了一個(gè)FrameLayout,主要是為了使用TextView顯示消息數(shù)量,這里是居中靠左60dip,可能你會(huì)問直接寫死能支持多分辨率嗎,這個(gè)我在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>
其他的那四個(gè)都合這個(gè)一樣點(diǎn)擊后圖片換成亮色的,所以就不一一貼出來了。
最后看MainActivity這個(gè)類:
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ī)制這四個(gè)界面都會(huì)緩存到內(nèi)存中,這樣即有利也有弊,有利是因?yàn)榍袚Q的時(shí)候不用在重新加載了,有弊是因?yàn)榫彺嫠膫€(gè)界面會(huì)耗費(fèi)內(nèi)存較多一些。如果只想緩存一個(gè)界面,大家可以參考這篇文章:Android項(xiàng)目實(shí)戰(zhàn)之仿網(wǎng)易頂部導(dǎo)航欄效果,使用ActivityGroup實(shí)現(xiàn)頂部滑動(dòng)欄,就像網(wǎng)易新聞的頂部滑動(dòng)欄我相信也是只緩存了一個(gè)界面,切換的時(shí)候是從數(shù)據(jù)庫加載的,所以第二次滑動(dòng)加載會(huì)比較快。
本文源碼下載地址:Android仿微信底部菜單欄
以上就是本文的全部?jī)?nèi)容,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
RecyclerView滑動(dòng)到指定Position的方法
這篇文章主要為大家詳細(xì)介紹了RecyclerView滑動(dòng)到指定Position的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04Android 帶進(jìn)度條的WebView 示例代碼
本文主要介紹Android WebView,這里提供實(shí)例代碼,和效果圖供大家參考,希望能幫助有需要的小伙伴2016-07-07Android中FloatingActionButton的顯示與隱藏示例
本篇文章主要介紹了Android中FloatingActionButton的顯示與隱藏示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-10-10android避免彈出軟鍵盤遮蓋listview的簡(jiǎn)單方法
下面小編就為大家?guī)硪黄猘ndroid避免彈出軟鍵盤遮蓋listview的簡(jiǎn)單方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09Android開發(fā)之基于DialogFragment創(chuàng)建對(duì)話框的方法示例
這篇文章主要介紹了Android開發(fā)之基于DialogFragment創(chuàng)建對(duì)話框的方法,結(jié)合實(shí)例形式分析了DialogFragment創(chuàng)建對(duì)話框的具體功能與布局相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-08-08動(dòng)態(tài)添加LinearLayout的高度實(shí)例
下面小編就為大家?guī)硪黄獎(jiǎng)討B(tài)添加LinearLayout的高度實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04Android SQLite數(shù)據(jù)庫操作代碼類分享
這篇文章主要介紹了Android SQLite數(shù)據(jù)庫操作代碼類分享,本文直接給出實(shí)現(xiàn)代碼和使用代碼,需要的朋友可以參考下2015-03-03Android使用Intent傳遞組件大數(shù)據(jù)
這篇文章主要介紹了Android使用Intent傳遞組件大數(shù)據(jù),文章圍繞主題展開詳細(xì)的內(nèi)容詳情,感興趣的朋友可以參考一下2022-07-07