2012-07-13 131 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”。