0

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

这是我的代码,我尝试在其中更改未选中和选中的 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); 
    }
4

1 回答 1

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>

处理选项卡的每个状态。每个可绘制对象,如 tab_focus,都是一个 9-patch png。

您还需要覆盖选项卡布局以引用您的可绘制资源。我会使用引用android默认值的父样式,然后覆盖背景属性以引用您的“drawable/mytab”。

于 2012-07-13T16:24:54.920 回答