Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

仿新浪微博底部導航:TabActivity 結合 RadioGroup 的分頁管理實作

編輯:關於 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; } } 

佈局檔 tab_layout.xml

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>

效果圖:

幾個細節與後續建議

  • 最後那句 getChildAt(0).toggle() 不能省,否則啟動時會出現「沒有任何分頁被選中」的空窗狀態。
  • RadioButton 預設自帶一個圓形單選圖示,若要純粹當選單用,記得在樣式中把 android:button 設為 @null。
  • 四個 RadioButton 的寬度建議設為 layout_weight="1" 並把 button 的寬度設為 0dp,才能平均等分。
  • TabActivity 早在 API 13 就已被標記為 Deprecated。新專案請改用 FragmentActivity 搭配 FragmentPagerAdapter 或 ViewPager2,再用 BottomNavigationView 呈現底部選單;若想保留「每個分頁各自獨立」的架構,也可用 Navigation Component 的 navigation graph 來管理。
  • 每個分頁都是獨立 Activity 的設計,切換時會反覆走生命週期,記憶體與效能開銷比 Fragment 方案大,分頁數量多時差異會很明顯。

以上就是仿微博分頁管理界面的實作分享,希望對你的學習有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved