Android tabhost 更改文本颜色样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22533061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:08:54  来源:igfitidea点击:

Android tabhost change text color style

androidandroid-tabhost

提问by user3345767

Trying to change tabhost text color, in this code I can change tabhost background color(not text color)

尝试更改 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

        }
});

how can I change tabhost text color?

如何更改 tabhost 文本颜色?

回答by Mukesh Kumar Singh

You may change color of Tabhost text as follow.

您可以按如下方式更改 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"))

    }
});

EDIT:

编辑:

To set text color initially in your activity, you can use this code in onResume()function

要在您的活动中最初设置文本颜色,您可以在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"));
    } 

回答by Will Molter

This can actually be done using XML themes. The TabWidgetuses android:textColorPrimaryfor the selected tab and android:textColorSecondaryfor the unselected ones. Thus, you can achieve a text color change like this:

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

In styles.xml:

在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>

In your layout:

在您的布局中:

<TabHost
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:theme="@style/TabWidgetTheme"/>

Note that the android:themeshould not be directly in the TabWidgetitself, but rather the containing TabHostor similar.

请注意, theandroid:theme不应该直接在其TabWidget本身中,而应该是包含TabHost或类似的。

回答by Ankit Dhadse

To change the text color of tabs, you need to get the view i.e TextView which is set as title of tabs and you can change it like this:

要更改选项卡的文本颜色,您需要获取设置为选项卡标题的视图即 TextView,您可以像这样更改它:

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"));
    } 

EDIT :

编辑 :

Another way is to create a custom view for your tabs. when you add tabs to your tabHost

另一种方法是为您的选项卡创建自定义视图。当您向 tabHost 添加标签时

FragmentTabHost tabHost;

tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        tabHost.setup(this, getSupportFragmentManager(), R.id.frame);

// create customView for each tabs View tabViewHome = createTabView(tabHost.getContext(), "Home", R.drawable.ic_home);

// 为每个选项卡视图创建 customView tabViewHome = createTabView(tabHost.getContext(), "Home", R.drawable.ic_home);

tabHost.addTab(tabHost.newTabSpec("Home").setIndicator(tabViewHome), HomeActivity.class, null);


private static View createTabView(final Context context, final String text, int iconId)
    {
            // inflate your layout here
        View view = LayoutInflater.from(context).inflate(R.layout.tab_layout, null);
        TextView tv = (TextView) view.findViewById(R.id.tab_tv_title);
        tv.setText(text);
            tv.setTextColor(Color.RED);
        ImageView iv = (ImageView) view.findViewById(R.id.tab_background_iv_icon);
        iv.setImageResource(iconId);
        return view;
    }

and your tab_layout.xml will be like this:

你的 tab_layout.xml 将是这样的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabsLayout"
    android:layout_width="fill_parent"
    android:layout_height="40dip"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="5dip" 
    android:background="#AAE1E1E1">

     <ImageView
        android:id="@+id/tab_background_iv_icon"
        android:layout_width="30dip"
        android:layout_height="30dip"
        android:contentDescription="@string/imgDesc"
        />

    <TextView
        android:id="@+id/tab_tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        //android:textColor="@drawable/tab_text_selector"
        android:textSize="8sp"
        android:textStyle="bold" />

</LinearLayout>

Hope this helps.

希望这可以帮助。

回答by user3024665

Ehi man, I used this solution for:

嗨,伙计,我将此解决方案用于:

private void setNewTab(final String tag, final String title, final Class<?> clazz, final Bundle bundle) {
    TabHost.TabSpec tabSpec = tabHost.newTabSpec(tag);
    tabSpec.setIndicator(InfoTabView_.build(getActivity()).bind(title, false));
    tabHost.addTab(tabSpec, clazz, bundle);
}

...

bundle = new Bundle();
bundle.putSerializable(FaqFragment.ARG_FAQS, infos.getFaq());
setNewTab("faq", "Faq", FaqFragment_.class, bundle);

...


@EViewGroup(R.layout.view_info_tab)
public class InfoTabView extends RelativeLayout {

    ....

    @Override
    public void setSelected(final boolean selected) {
        if (selected)
            titleTextView.setTextColor(selectedColor);
        else
            titleTextView.setTextColor(unselectedColor);
    }
}

override setSelected is most clean way!

覆盖 setSelected 是最干净的方法!