編輯:關於 Android 程式設計
本文示範如何用 TabActivity 搭配 RadioGroup 做出一組底部選單,切換時各自對應一個 Activity。為了方便取得上下文,這裡採用繼承 TabActivity 的傳統寫法。
用 RadioGroup 的單選特性承接「同一時間只有一個分頁被選中」的語意,每個 RadioButton 對應一個 Tab 標籤;點擊時透過 setCurrentTabByTag 讓 TabHost 切到對應的 Activity。標籤名稱與 Activity 類別集中放在常數類中管理,避免散落各處。
/** * 主Activity * 通過點擊RadioGroup下的RadioButton來切換不同界面 * Created by D&LL on 2016/7/20. */ public class MainActivity extends TabActivity { //定義TabHost對象 private TabHost tabHost; //定義RadioGroup對象 private RadioGroup radioGroup; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tab_layout); initView(); initData(); } /** * 初始化組件 */ private void initView() { //實例化TabHost,得到TabHost對象 tabHost = getTabHost(); //得到Activity的個數 int count = Constant.ConValue.mTabClassArray.length; for (int i = 0; i < count; i++) { //為每一個Tab按鈕設置圖標、文字和內容 TabSpec tabSpec = tabHost.newTabSpec(Constant.ConValue.mTextviewArray[i]) .setIndicator(Constant.ConValue.mTextviewArray[i]).setContent(getTabItemIntent(i)); //將Tab按鈕添加進Tab選項卡中 tabHost.addTab(tabSpec); } //實例化RadioGroup radioGroup = (RadioGroup) findViewById(R.id.main_radiogroup); } /** * 初始化組件 */ private void initData() { // 給radioGroup設置監聽事件 radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.RadioButton0: tabHost.setCurrentTabByTag(Constant.ConValue.mTextviewArray[0]); break; case R.id.RadioButton1: tabHost.setCurrentTabByTag(Constant.ConValue.mTextviewArray[1]); break; case R.id.RadioButton2: tabHost.setCurrentTabByTag(Constant.ConValue.mTextviewArray[2]); break; case R.id.RadioButton3: tabHost.setCurrentTabByTag(Constant.ConValue.mTextviewArray[3]); break; case R.id.RadioButton4: tabHost.setCurrentTabByTag(Constant.ConValue.mTextviewArray[4]); break; } } }); ((RadioButton) radioGroup.getChildAt(0)).toggle(); } /** * 給Tab選項卡設置內容(每個內容都是一個Activity) */ private Intent getTabItemIntent(int index) { Intent intent = new Intent(this, Constant.ConValue.mTabClassArray[index]); return intent; } }
TabHost 內部包含三部分:FrameLayout 作為內容容器、TabWidget 作為系統預設的標籤列(這裡隱藏起來,因為改用自定義的 RadioGroup),以及最下方的 RadioGroup 作為選單列。
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="https://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: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"/> <RadioGroup android:id="@+id/main_radiogroup" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:background="@drawable/tab_widget_background" android:gravity="center_vertical" android:orientation="horizontal" android:padding="2dip"> <RadioButton android:id="@+id/RadioButton0" android:drawableTop="@drawable/tab_home" android:text="主頁" android:textColor="#ffffff"/> <RadioButton android:id="@+id/RadioButton1" android:drawableTop="@drawable/tab_msg" android:text="評論" android:textColor="#ffffff"/> <RadioButton android:id="@+id/RadioButton2" android:drawableTop="@drawable/tab_write" android:text="發微博" android:textColor="#ffffff"/> <RadioButton android:id="@+id/RadioButton3" android:drawableTop="@drawable/tab_me" android:text="用戶信息" android:textColor="#ffffff"/> <RadioButton android:id="@+id/RadioButton4" android:drawableTop="@drawable/tab_more" android:text="更多" android:textColor="#ffffff"/> </RadioGroup> </LinearLayout> </TabHost>
效果圖:

以上就是仿微博分頁管理界面的實作分享,希望對你的學習有所幫助。
Android AIDL 與 Binder 框架淺析:從跨進程加減法看底層通信
1、概述 Binder 能給我們什麼?答案是:系統中任何程序都能訪問的全局服務。這幾乎是任何作業系統都必須具備的能力。先從宏觀結構看 Android 的 Binder 框
Android 仿騰訊 QQ 實現滑動刪除(附完整源碼)
看了許多高手寫的文章,受益良多,也非常欣賞大家的分享精神,於是決定開始寫 Blog,把自己的心得記錄下來與各位交流。 先來看看效果圖: 原本我打算在 ListView
Android Fragment 的兩種用法:XML 靜態聲明與代碼動態加載
Fragment 是 Android 3.0 之後引入的組件,目的是讓界面邏輯能夠復用到不同尺寸的屏幕上。實際開發中,Fragment 的用法大致可以歸成兩類:一種是在布
Android 漫游記(6):App 啟動之旅(上)
Android 建立在 Linux 2.6+ 核心之上。在看懂 App 是怎麼被拉起來之前,先對整體架構有個感性的認識會輕鬆很多。 一、分層架構速覽 1. Kernel