Android tabhost更改文本颜色样式

use*_*767 14 android android-tabhost

试图更改tabhost文本颜色,在此代码中我可以更改tabhost背景颜色(不是文本颜色)

tabHost.setOnTabChangedListener(new OnTabChangeListener() {
        @Override
        public void onTabChanged(String tabId) {
          for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
            tabHost.getTabWidget().getChildAt(i)
                            .setBackgroundColor(Color.parseColor("#FF0000")); // unselected
          }

          tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
                        .setBackgroundColor(Color.parseColor("#0000FF")); // selected

        }
});
Run Code Online (Sandbox Code Playgroud)

如何更改tabhost文本颜色?

Muk*_*ngh 54

您可以更改Tabhost文本的颜色,如下所示.

tabHost.setOnTabChangedListener(new OnTabChangeListener() {

    @Override
    public void onTabChanged(String tabId) {

        for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
            tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#FF0000")); // unselected
            TextView tv = (TextView) tabhost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); //Unselected Tabs
            tv.setTextColor(Color.parseColor("#ffffff"));
        }

        tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#0000FF")); // selected
        TextView tv = (TextView) tabhost.getCurrentTabView().findViewById(android.R.id.title); //for Selected Tab
        tv.setTextColor(Color.parseColor("#000000"))

    }
});
Run Code Online (Sandbox Code Playgroud)

编辑:

要在活动中初始设置文本颜色,可以在onResume()函数中使用此代码

TabHost tabhost = getTabHost();
    for(int i=0;i<tabhost.getTabWidget().getChildCount();i++) 
    {
        TextView tv = (TextView) tabhost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
        tv.setTextColor(Color.parseColor("#000000"));
    } 
Run Code Online (Sandbox Code Playgroud)


Wil*_*ter 10

实际上,这可以使用XML主题来完成.选定选项卡和未选定选项卡的TabWidget用途.因此,您可以实现如下文本颜色更改:android:textColorPrimaryandroid:textColorSecondary

在styles.xml中:

<style name="TabWidgetTheme" parent="AppTheme">
    <item name="android:textColorPrimary">@color/your_primary_color</item>
    <item name="android:textColorSecondary">@color/your_secondary_color</item>
</style>
Run Code Online (Sandbox Code Playgroud)

在你的布局中:

<TabHost
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:theme="@style/TabWidgetTheme"/>
Run Code Online (Sandbox Code Playgroud)

注意,android:theme不应该直接在TabWidget自身,而是包含TabHost或类似.