Android TabHost 更改文本颜色样式

16
尝试更改tabhost文本颜色,在此代码中,我可以更改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

        }
});

我该如何更改 TabHost 的文本颜色?


点击这里:http://stackoverflow.com/questions/11192863/the-text-color-does-not-change-tabwidget - M D
https://dev59.com/9mrXa4cB1Zd3GeqPAprf#13288683 - Sree
4个回答

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

    }
});

编辑:

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

谢谢,当我点击TabHost时它可以工作,但第一次颜色也是默认的。你的代码只有在我点击某个TabHost后才能工作。 - user3345767
也许你没有理解我的意思。当我第一次启动程序时,TabHost的文本颜色是默认的(黑色),然后如果我点击TabHost,颜色就会完美地改变。我想要的是默认颜色应该是我自己定义的颜色,而不是默认颜色。 - user3345767
@user3345767,"#000000"的颜色值是黑色,因此文本的初始颜色为黑色。您可以更改颜色,例如"#0000ff"表示蓝色。您可以将默认颜色设置为tv.setTextColor(Color.parseColor("#0000ff")),以便使用蓝色。如答案所述,已编辑部分。 - Mukesh Kumar Singh

12

这实际上可以使用XML主题来完成。 TabWidget使用android:textColorPrimary选择选项卡和android: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>
在您的布局中:
<TabHost
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:theme="@style/TabWidgetTheme"/>

请注意,android:theme 不应该直接放在 TabWidget 中,而是应该放在包含的 TabHost 或类似的容器中。


这对我有用,但它是“android:textColorPrimary”。 - andrei
如何使选定的选项卡字体加粗? - coding_idiot

4
要更改选项卡文本的颜色,您需要获取视图,即 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"));
    } 

编辑:

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

FragmentTabHost tabHost;

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

// 为每个选项卡创建自定义视图 View tabViewHome = createTabView(tabHost.getContext(), "主页", R.drawable.ic_home);

注:createTabView() 是一个自定义函数,此处仅翻译参数和常量。
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;
    }

你的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>

希望这有所帮助。

谢谢,当我点击TabHost时它可以工作,但第一次颜色也是默认的(黑色)。你的代码只有在我点击某个TabHost后才能工作。 - user3345767
@user3345767 你好,你可以在你的应用程序中添加自定义的TabHost。根据你的需求进行完全定制。你可以在XML或.java文件中设置文本颜色。代码如上所编辑。 - Ankit Dhadse
@AnkitDhadse 我正在尝试通过在运行时创建新的TextView来更改选项卡栏字体颜色和大小,但是出现了错误,提示View必须从父级中移除。 - Erum
@ErumHannan,你能给我展示一下你的代码吗?你在哪里遇到了错误,日志又是什么呢?否则,你可以发布一个新的问题。 - Ankit Dhadse
@AnkitDhadse 这是我的代码和它的错误,你能帮我检查一下吗?http://pastie.org/9557782 - Erum

0

嘿,伙计,我用了这个解决方案:

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

重写setSelected是最简洁的方式!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接