Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Android TabHost 底部白線困擾?改寫 Tab 背景與文字顏色就好

以前做分頁我都用 GridView 加 ActivityGroup,一方面是覺得 TabHost 不好用,另一方面也得承認是自己功力不足。但其實只要願意重寫 view,多數問題都能解掉。以下示範如何消掉 TabHost 頁籤下方那條惱人的白線,過程相當簡單。

TabHost 的基本用法網上範例很多,這裡不重複,重點放在後處理。

程式碼

  1. public class MyActivity extends TabActivity {
  2. private TabWidget tabWidget;
  3. /** Called when the activity is first created. */
  4. public void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.main);
  7. Resources res = getResources(); // Resource object to get Drawables
  8. final TabHost tabHost = getTabHost(); // The activity TabHost
  9. TabHost.TabSpec spec; // Resusable TabSpec for each tab
  10. Intent intent; // Reusable Intent for each tab
  11. // Create an Intent to launch an Activity for the tab (to be reused)
  12. intent = new Intent().setClass(this, ArtistsActivity.class);
  13. // Initialize a TabSpec for each tab and add it to the TabHost
  14. spec = tabHost
  15. .newTabSpec("artists")
  16. .setIndicator("Artists",
  17. res.getDrawable(R.drawable.ic_tab_artists))
  18. .setContent(intent);
  19. tabHost.addTab(spec);
  20. // Do the same for the other tabs
  21. intent = new Intent().setClass(this, AlbumsActivity.class);
  22. spec = tabHost
  23. .newTabSpec("albums")
  24. .setIndicator("Albums",
  25. res.getDrawable(R.drawable.ic_tab_artists))
  26. .setContent(intent);
  27. tabHost.addTab(spec);
  28. intent = new Intent().setClass(this, SongsActivity.class);
  29. spec = tabHost
  30. .newTabSpec("songs")
  31. .setIndicator("Songs",
  32. res.getDrawable(R.drawable.ic_tab_artists))
  33. .setContent(intent);
  34. tabHost.addTab(spec);
  35. tabHost.setCurrentTab(2);
  36. View v;
  37. tabWidget = tabHost.getTabWidget();
  38. for (int i = 0; i < tabWidget.getChildCount(); i++) {
  39. // 獲取tabview項
  40. v = tabWidget.getChildAt(i);
  41. // 設置tab背景顏色
  42. v.setBackgroundResource(Android.R.color.white);
  43. // 獲取textview控件,(默認為白色)
  44. TextView textView = (TextView) v.findViewById(android.R.id.title);
  45. textView.setTextColor(Color.BLACK);
  46. // 默認選項要處理
  47. if (tabHost.getCurrentTab() == i)
  48. v.setBackgroundResource(R.drawable.renren_sdk_pay_repair_btn);
  49. }
  50. // tabchanged的監聽
  51. tabHost.setOnTabChangedListener(new OnTabChangeListener() {
  52. // tabId顯示的是:newTabSpec裡面的值
  53. @Override
  54. public void onTabChanged(String tabId) {
  55. // 首先把所有的view背景初始化了.
  56. for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
  57. View v = tabHost.getTabWidget().getChildAt(i);
  58. // 設置tab背景顏色
  59. v.setBackgroundResource(android.R.color.white);
  60. // 選中的進行處理
  61. if (tabHost.getCurrentTab() == i) {
  62. v.setBackgroundResource(R.drawable.renren_sdk_pay_repair_btn);
  63. }
  64. }
  65. }
  66. });
  67. }
  68. }

白線是怎麼被消掉的

關鍵只有三步。第一,取得 TabWidget 底下每個分頁 View,把背景改成白色,蓋掉系統預設那條分隔線;第二,TabHost 內的 TextView 預設字色是白色,在白色背景上會看不見,所以要抓出 android.R.id.title 改成黑色;第三,掛上 OnTabChangeListener,在切換時先把所有分頁背景重設為白,再把當前分頁設成高亮背景,避免選中態殘留。

要注意 setCurrentTab 必須在這段迴圈之前呼叫,否則「判斷當前分頁」的那個 if 永遠不會成立,初始高亮就會消失。

下圖為實機效果:

源碼下載
免費下載地址:https://linux.linuxidc.com/
帳號與密碼皆為 www.linuxidc.com
下載目錄:/2012年資料/8月/30日/Android TabHost解決下面白線

Copyright © Linux教程網 All Rights Reserved