2012-07-13 148 views
0

嗨,我想改變我的標籤小部件的顏色,但是當我運行應用程序它沒有改變沒有人知道爲什麼嗎?Android標籤顏色不變

繼承人我的代碼,我試圖改變我的tabhost的顏色都選擇,而選擇

public static void setTabColor(TabHost tabhost) { 
     for(int i=0;i<tabhost.getTabWidget().getChildCount();i++) 
     { 
      tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#666666")); //unselected 
     } 
     tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.parseColor("#FFFFFF")); // selected 
    } 



    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.tablayout); 
      checkInternet(); 
      checkPreferences(); 

     TabHost tabHost = getTabHost(); 
//  setTabColor(tabHost); 


     // Tab for Photos 
     TabSpec resultsspec = tabHost.newTabSpec("Results"); 
     // setting Title and Icon for the Tab 
     resultsspec.setIndicator("Results", getResources().getDrawable(R.drawable.miresults_tab)); 
     Intent photosIntent = new Intent(this, MIResults.class); 
     resultsspec.setContent(photosIntent); 
//  setTabColor(tabHost); 



     // Tab for Songs 
     TabSpec Fixturesspec = tabHost.newTabSpec("Fixtures"); 
     Fixturesspec.setIndicator("Fixtures", getResources().getDrawable(R.drawable.mifixtures_tab)); 
     Intent songsIntent = new Intent(this, MIFixtures.class); 
     Fixturesspec.setContent(songsIntent); 

     TabSpec tablespec = tabHost.newTabSpec("Table"); 
     tablespec.setIndicator("Table", getResources().getDrawable(R.drawable.mitable_tab)); 
     Intent videosIntent = new Intent(this, MITable.class); 
     tablespec.setContent(videosIntent); 

     // Adding all TabSpec to TabHost 
     tabHost.addTab(resultsspec); 
     tabHost.addTab(Fixturesspec); 
     tabHost.addTab(tablespec); 
    } 

回答

1

你需要創建一個可繪製的選擇,類似於:

mytab.xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- Non focused states --> 
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected_v4" /> 
    <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected_v4" /> 

    <!-- Focused states --> 
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_focus" /> 
    <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_focus" /> 

    <!-- Pressed --> 
    <item android:state_pressed="true" android:drawable="@drawable/tab_press" /> 
</selector> 

處理選項卡的每個狀態。每個drawable,如tab_focus,是一個9-patch的png。

您還需要重寫選項卡布局以引用可繪製資源。我會使用一個引用android默認的父類的樣式,然後覆蓋背景屬性以引用您的「drawable/mytab」。