上一個項目已經做完了,這周基本上沒事,所以整理了下以前的項目,想把一些通用的部分封裝起來,這樣以後遇到相似的項目就不用重複發明輪子了,也節省了開發效率。今天把demo貼出來一是方便以後自己查詢,二是希望同時也能幫到大家。
底部功能表列很重要,我看了一下很多應用軟體都是用了底部功能表列做。我這裡使用了tabhost做了一種通用的(就是可以像那樣顯示未讀訊息數量的,雖然之前也做過但是layout下的xml寫的太臃腫,這裡去掉了很多不必要的層,個人看起來還是不錯的,所以貼出來方便以後使用)。
先看一下做出來之後的效果:
以後使用的時候就可以換成自己項目的圖片和字型了,主架構不用變哈哈,
首先是要布局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="設定" /> </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顯示訊息數量,這裡是置中靠左60dip,可能你會問直接寫死能支援多解析度嗎,這個我在320*480的手機上試過沒問題的,因為dip是與裝置無關的支援多解析度,至於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>
其他的那四個都合這個一樣點擊後圖片換成亮色的,所以就不一一貼出來了。
最後看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("設定").setIndicator("設定").setContent(intent); tabHost.addTab(spec); tabHost.setCurrentTab(1); RadioGroup radioGroup=(RadioGroup) this.findViewById(R.id.main_tab_group); radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {// TODO Auto-generated method stubswitch (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://設定tabHost.setCurrentTabByTag("設定");break;default://tabHost.setCurrentTabByTag("我的考試");break;}}}); } }
這樣就完成了,主要還是使用了tabhost完成,tabhost有緩衝機制這四個介面都會緩衝到記憶體中,這樣即有利也有弊,有利是因為切換的時候不用在重新載入了,有弊是因為緩衝四個介面會耗費記憶體較多一些。如果只想緩衝一個介面以後下一篇我會使用ActivityGroup實現頂部滑動欄,就像網易新聞的頂部滑動欄我相信也是只緩衝了一個介面,切換的時候是從資料庫載入的,所以第二次滑動載入會比較快。
最後奉上,如果有需要的希望大家自己去下載吧,有些代碼存在本地時間長了我也會弄丟。[rar檔案] android底部功能表列demo