歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 分頁控件制成底部菜單

Android 分頁控件制成底部菜單

日期:2017/3/1 10:48:33   编辑:Linux編程

其實Android 中的底部菜單, 可以用分頁控件很好的實現。 我們先將自定義分頁控件做好, 就可以做到頂底兩個位置的菜單了。

TabHost只是作為一個容器來存放一些Activity, 所以需要自己另外創建幾個新的Activity, 然後由主TabHost加載。

tab_style.xml

是每個Tab的自定義樣式

  1. //分頁控件樣式
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content"
  6. android:paddingLeft="5dip"
  7. android:paddingRight="5dip"
  8. android:paddingTop="5dip"
  9. android:background="@drawable/tab_bg"
  10. >
  11. <FrameLayout
  12. android:layout_width="fill_parent"
  13. android:layout_height="fill_parent"
  14. android:layout_weight="0.6"
  15. >
  16. <TextView
  17. android:id="@+id/tab_label"
  18. android:layout_width="fill_parent"
  19. android:layout_height="fill_parent"
  20. android:gravity="center"
  21. android:background="@drawable/tab_title_selector"
  22. android:textColor="#FFFFFF"
  23. android:text
  24. />
  25. </FrameLayout>
  26. </LinearLayout>

main_tab.xml 是主TabHost布局文件

  1. //TabHost布局
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:id="@android:id/tabhost"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent"
  7. >
  8. //必須包含下列三個View
  9. <LinearLayout
  10. android:orientation="vertical"
  11. android:layout_width="fill_parent"
  12. android:layout_height="fill_parent"
  13. >
  14. <FrameLayout
  15. android:gravity="center"
  16. android:id="@android:id/tabcontent"
  17. android:layout_width="fill_parent"
  18. android:layout_height="wrap_content"
  19. android:layout_weight="1.0"
  20. />
  21. //TabWidget位置在FrameLayout之下則顯示在低部, 在之上則顯示在頂部
  22. <TabWidget
  23. android:id="@android:id/tabs"
  24. android:layout_height="wrap_content"
  25. android:layout_width="fill_parent"
  26. android:layout_weight="0.0"
  27. />
  28. </LinearLayout>
  29. </TabHost>

tab_title_selector.xml

是Tab中TextView的按下背景

  1. //選擇器,指示Text按下後的背景
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  4. <item
  5. android:state_focused="true"
  6. android:drawable="@drawable/tab_btn_bg_d"
  7. />
  8. <item
  9. android:state_selected="true"
  10. android:drawable="@drawable/tab_btn_bg_d"
  11. />
  12. <item
  13. android:state_pressed="true"
  14. android:drawable="@drawable/tab_btn_bg_d"
  15. />
  16. </selector>

Activity類

另外還需要幾個Activity類, 普通的Activity類即可, 在此不顯示。

  1. public class TabTest extends TabActivity
  2. {
  3. private TabWidget mTabWidget;
  4. private TabHost mTabHost;
  5. /** Called when the activity is first created. */
  6. @Override
  7. public void onCreate(Bundle savedInstanceState)
  8. {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.main_tabs);
  11. mTabHost = getTabHost();
  12. //將要顯示的Activity載入TabHost控件
  13. //要顯示的Activity由自己自由創建
  14. setTabIndicator("one", 1, new Intent(this, OneActivity.class));
  15. setTabIndicator("Two", 2, new Intent(this, TwoActivity.class));
  16. setTabIndicator("Three", 3, new Intent(this, OneActivity.class));
  17. setTabIndicator("Four", 4, new Intent(this, TwoActivity.class));
  18. }
  19. private void setTabIndicator(String title, int nId, Intent intent)
  20. {
  21. //使用指定Tab樣式
  22. View view = LayoutInflater.from(this.mTabHost.getContext())
  23. .inflate(R.layout.tab_style, null);
  24. TextView text = (TextView)view.findViewById(R.id.tab_label);
  25. String strId = String.valueOf(nId);
  26. text.setText(title);
  27. //創建一個新Tab
  28. TabHost.TabSpec localTabSpec = mTabHost.newTabSpec(strId)
  29. .setIndicator(view).setContent(intent);
  30. //加載新Tab
  31. mTabHost.addTab(localTabSpec);
  32. }
  33. }

Android 分頁控件制成底部菜單源碼下載地址:

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /pub/Android源碼集錦/2011年/12月/Android 分頁控件制成底部菜單/

Copyright © Linux教程網 All Rights Reserved