歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android TabHost解決下面白線

Android TabHost解決下面白線

日期:2017/3/1 10:11:35   编辑:Linux編程

我之前做分頁都是用GridView和ActivityGroup實現的.因為覺得TabHost不好用,還有就是自己水平差的原因吧.如果幫的話,重寫view任何問題都可以解決,呵呵,下面請看實現過程,其實很簡單.

針對TabHost的運用,我就不多講解了,網上例子好多,或者你也可以下載我的Demo查看,不過先聲明,寫的不好.

代碼片段:

  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. }

其實就是那幾行加注釋部分,相信你看一下就明白.

解決Tab下面白線方法:

首先我們要獲取Tab的分頁view和textview控件,因為textview默認字體是白色.我們要進行處理.獲取後進行相應處理(也就是該加背景的加背景,該改變字體顏色的改變字體顏色).最後我們要對TabHost進行監聽(OnTabChangedListener);也不是什麼監聽,。就是用他的點擊執行事件.

下面是視圖:

源碼下載

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

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

具體下載目錄在 /2012年資料/8月/30日/Android TabHost解決下面白線

Copyright © Linux教程網 All Rights Reserved