【问题标题】:Android tabhost change text color styleAndroid tabhost更改文本颜色样式
【发布时间】:2014-03-20 12:28:56
【问题描述】:

尝试更改 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 文本颜色?

【问题讨论】:

标签: android android-tabhost


【解决方案1】:

您可以如下更改 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 时才能工作
  • 也许你听不懂我的意思。当我第一次启动程序 tabhost 的文本颜色是默认的(黑色)然后如果我点击 tabhost 然后颜色会改变完美。我想默认颜色应该是我自己的颜色,而不是默认颜色
  • @user3345767 "#000000" 的颜色值为black,因此最初的文本颜色为黑色。您可以更改颜色,即"#0000ff"Blue。等等,您可以将默认颜色tv.setTextColor(Color.parseColor("#0000ff")) 设置为蓝色。正如提到的答案的编辑部分。
【解决方案2】:

这实际上可以使用 XML 主题来完成。 TabWidgetandroid: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”
  • 使所选标签字体加粗的类似属性?
【解决方案3】:

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

编辑:

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

FragmentTabHost tabHost;

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

//为每个标签创建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;
    }

你的 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 hi.. 您可以将自定义 tabHost 添加到您的应用程序。完全根据您的需求定制。您可以在 xml 或 .java 文件中提供 textColor。代码如上编辑。
  • @AnkitDhadse 我试图通过在运行时创建新的 TextView 来更改标签栏字体颜色和大小,但出现必须从父级删除 View 的错误
  • @ErumHannan 你能告诉我你的代码吗?!你在哪里得到你的错误,你的日志是什么!否则你可以发布一个新问题。
  • @AnkitDhadse 这是我的代码,它的错误你可以检查一下pastie.org/9557782
【解决方案4】:

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

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 是最干净的方式!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    相关资源
    最近更新 更多