去掉TabActivity底部默认白线

经过一翻百度,google终于实现了TabActivity设置tab自定义背景颜色,设置tab中标题文字的颜色。并将TabWidget置于底部 

 

TabManager: 
Java代码  收藏代码
  1. import android.app.TabActivity;  
  2. import android.content.Intent;  
  3. import android.content.res.ColorStateList;  
  4. import android.content.res.XmlResourceParser;  
  5. import android.os.Bundle;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.widget.TabHost;  
  9. import android.widget.TabWidget;  
  10. import android.widget.TextView;  
  11.   
  12. public class TabManager extends TabActivity{  
  13.     private TabManager th;  
  14.     private TabHost tabHost;  
  15.     private TabWidget tabWidget;  
  16.       
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.tab_content);  
  20.         th = this;  
  21.          tabHost = (TabHost) findViewById(android.R.id.tabhost);  
  22.          LayoutInflater.from(this).inflate(R.layout.main,tabHost.getTabContentView(),true);  
  23.          tabWidget = tabHost.getTabWidget();  
  24.          tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab1",th.getResources().getDrawable(R.drawable.ic_menu_home_tab))  
  25.                   .setContent(new Intent(this, Tab1.class)));  
  26.          tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Tab2",th.getResources().getDrawable(R.drawable.ic_menu_camera_tab))  
  27.                  .setContent(new Intent(this, Tab2.class)));  
  28.          tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("Tab3",th.getResources().getDrawable(R.drawable.ic_menu_search_tab))  
  29.                  .setContent(new Intent(this, Tab3.class)));  
  30.          tabHost.addTab(tabHost.newTabSpec("tab4").setIndicator("Tab4",th.getResources().getDrawable(R.drawable.ic_menu_upload_tab))  
  31.                  .setContent(new Intent(this, Tab4.class)));  
  32.          tabHost.addTab(tabHost.newTabSpec("tab5").setIndicator("Tab5",th.getResources().getDrawable(R.drawable.ic_menu_preferences_tab))  
  33.                  .setContent(new Intent(this, Tab5.class)));  
  34.           tabHost.setCurrentTab(0);  
  35.   
  36.         for (int i = 0; i < tabWidget.getChildCount(); i++) {  
  37.             /** 
  38.              * 此方法是为了去掉系统默认的色白的底角 
  39.              */  
  40.             tabHost.setPadding(tabHost.getPaddingLeft(), tabHost.getPaddingTop(), tabHost.getPaddingRight(), tabHost.getPaddingBottom() - 5);  
  41.             View v = tabWidget.getChildAt(i);  
  42.             //设置tab背景颜色  
  43.             v.setBackgroundResource(R.drawable.tab_indicator);  
  44.               
  45.             //设置tab中标题文字的颜色,不然默认为黑色  
  46.             XmlResourceParser xpp = getResources().getXml(R.color.tab_font_selector);   
  47.             try {  
  48.                 ColorStateList csl= ColorStateList.createFromXml(getResources(),xpp);  
  49.                 TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);  
  50.                 tv.setTextColor(csl);  
  51.             } catch (Exception e){  
  52.                 e.printStackTrace();  
  53.             }  
  54.         }  
  55.     }  
  56. }  


tab_indicator.xml: 
Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <!-- Non focused states -->  
  4.     <item android:state_focused="false" android:state_selected="false"  
  5.         android:state_pressed="false" android:drawable="@color/tab_unselected" />  
  6.     <item android:state_focused="false" android:state_selected="true"  
  7.         android:state_pressed="false" android:drawable="@color/tab_selected" />  
  8.   
  9.     <!-- Focused states -->  
  10.     <item android:state_focused="true" android:state_selected="false"  
  11.         android:state_pressed="false" android:drawable="@color/tab_focus" />  
  12.     <item android:state_focused="true" android:state_selected="true"  
  13.         android:state_pressed="false" android:drawable="@color/tab_focus" />  
  14.   
  15.     <!-- Pressed -->  
  16.     <item android:state_pressed="true" android:drawable="@color/tab_press" />  
  17. </selector>  


tab_font_selector.xml: 
Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <!-- Non focused states -->  
  4.     <item android:state_focused="false" android:state_selected="false"  
  5.         android:state_pressed="false" android:color="@color/tab_font_unselected" />  
  6.     <item android:state_focused="false" android:state_selected="true"  
  7.         android:state_pressed="false" android:color="@color/tab_font_selected" />  
  8.   
  9.     <!-- Focused states -->  
  10.     <item android:state_focused="true" android:state_selected="false"  
  11.         android:state_pressed="false" android:color="@color/tab_font_focus" />  
  12.     <item android:state_focused="true" android:state_selected="true"  
  13.         android:state_pressed="false" android:color="@color/tab_font_focus" />  
  14.   
  15.     <!-- Pressed -->  
  16.     <item android:state_pressed="true" android:color="@color/tab_font_press" />  
  17. </selector>  


colors.xml: 
Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <color name="tab_unselected">#ff000000</color>  
  5.     <color name="tab_selected">#ff383838</color>  
  6.     <color name="tab_focus">#ffff8000</color>  
  7.     <color name="tab_press">#ffe9a700</color>  
  8.   
  9.     <color name="tab_font_unselected">#ff888888</color>  
  10.     <color name="tab_font_selected">#ffffffff</color>  
  11.     <color name="tab_font_focus">#ffffffff</color>  
  12.     <color name="tab_font_press">#ff888888</color>  
  13.   
  14.     <color name="black">#FF000000</color>  
  15.     <color name="dkgray">#FF444444</color>  
  16.     <color name="gray">#FF888888</color>  
  17.     <color name="ltgray">#FFCCCCCC</color>  
  18.     <color name="white">#FFFFFFFF</color>  
  19.     <color name="red">#FFFF0000</color>  
  20.     <color name="green">#FF00FF00</color>  
  21.     <color name="blue">#FF0000FF</color>  
  22.     <color name="yellow">#FFFFFF00</color>  
  23.     <color name="cyan">#FF00FFFF</color>  
  24.     <color name="magenta">#FFFF00FF</color>  
  25.     <color name="transparent">#00000000</color>  
  26. </resources>  


转自:http://www.cmd100.com/bbs/thread-156307-1-1.html
posted on 2012-09-12 20:06  Code大蛇丸  阅读(1513)  评论(0编辑  收藏  举报