如何更改TabHost的颜色?

时间:2016-08-29 17:58:38

标签: java android xml android-studio android-tabhost

我想更改标签的颜色(已激活的标签和已停用的标签)。我知道,我如何更改背景图片,但不是,我如何更改标签的颜色: - (

我的Java代码:

public class Inventar extends TabActivity {

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

    TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
    TabHost.TabSpec spec;
    Intent intent;

    spec = tabHost.newTabSpec("1");
    spec.setIndicator("Inventar 1");
    intent = new Intent(this, Spielregeln.class);
    spec.setContent(intent);
    tabHost.addTab(spec.setIndicator("",getResources().getDrawable(R.drawable.button)));

    spec = tabHost.newTabSpec("2");
    spec.setIndicator("Inventar 2");
    intent = new Intent(this, Login.class);
    spec.setContent(intent);
    tabHost.addTab(spec.setIndicator("",getResources().getDrawable(R.drawable.button)));

    spec = tabHost.newTabSpec("3");
    spec.setIndicator("Inventar 3");
    intent = new Intent(this, Registrieren.class);
    spec.setContent(intent);
    tabHost.addTab(spec.setIndicator("", getResources().getDrawable(R.drawable.tab_spec)));


    tabHost.setCurrentTab(0);
    tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
        @Override
        public void onTabChanged(String tabId) {

        }
    });


}

}

我的XML代码(tab_spec.xml):

<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_selected="true"
        android:drawable="@drawable/inventar" />
    <item
        android:state_selected="false"
        android:drawable="@drawable/freunde" />
</selector>

我的XML代码(主要)

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="0dp">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:padding="5dp">

        </FrameLayout>

    </LinearLayout>
</TabHost>
enter code here

1 个答案:

答案 0 :(得分:0)

您可以使用onTabChangedListener

进行设置
tabHost.setOnTabChangedListener(new OnTabChangeListener() {

        public void onTabChanged(String arg0) {
            for (int i = 0; i < tab.getTabWidget().getChildCount(); i++) {
                tabHost.getTabWidget().getChildAt(i)
                        .setBackgroundResource(R.drawable.tab_selected); // unselected
            }
            tabHost.getTabWidget().getChildAt(tab.getCurrentTab())
                    .setBackgroundResource(R.drawable.tab_unselected); // selected

        }
    });