Android TabHost解决下面白线


我之前做分页都是用GridView和ActivityGroup实现的.因为觉得TabHost不好用,还有就是自己水平差的原因吧.如果帮的话,重写view任何问题都可以解决,呵呵,下面请看实现过程,其实很简单.

针对TabHost的运用,我就不多讲解了,网上例子好多,或者你也可以下载我的Demo查看,不过先声明,写的不好.

代码片段:

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

其实就是那几行加注释部分,相信你看一下就明白.

解决Tab下面白线方法:

首先我们要获取Tab的分页view和textview控件,因为textview默认字体是白色.我们要进行处理.获取后进行相应处理(也就是该加背景的加背景,该改变字体颜色的改变字体颜色).最后我们要对TabHost进行监听(OnTabChangedListener);也不是什么监听,。就是用他的点击执行事件.

下面是视图:

源码下载

免费下载地址在 http://linux.bkjia.com/

用户名与密码都是www.bkjia.com

具体下载目录在 /2012年资料/8月/30日/Android TabHost解决下面白线

相关内容