Android标签颜色不变

时间:2012-07-13 15:26:52

标签: android android-tabhost

您好我正在尝试更改我的标签小部件的颜色但是当我运行应用程序时它没有变化有人知道为什么吗?

继承我的代码,我试图改变我的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 个答案:

答案 0 :(得分: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默认值的父级样式,然后覆盖background属性以引用“drawable / mytab”。