1


我是 Android 新手,需要您的帮助!
我喜欢默认的 tabWidget 样式,但我需要在标签标题中使用白色。
这就是我现在所拥有的, 我想要白色的“TAB1”和“TAB2”。 我尝试了自己的 tabwidget 风格。这是代码标签小部件

<!-- styles.xml -->

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:tabWidgetStyle">@style/LightTabWidget</item>
</style>
<style name="LightTabWidget" parent="@android:style/Widget.TabWidget">
    <item name="android:textColor">@color/tabTitle</item>
</style>

这将更改标题文本颜色,但会更改所有选项卡样式 在此处输入图像描述

如何仅更改文本颜色?不改变其余部分?
谢谢您的帮助!

更新 我正在使用 tabHost。不使用 TabLayout 可以做我需要的事情吗?

4

2 回答 2

3

在你的 onCreate 或 onCreateView 里面试试这个对我有用!

TabLayout tabLayout = (TabLayout) rootView.findViewById(R.id.tab_bhishilayout);
tabLayout.addTab(tabLayout.newTab().setText("OPEN"));
tabLayout.setSelectedTabIndicatorColor(Color.RED);//set tab indicator color
tabLayout.setTabTextColors(ColorStateList.valueOf(Color.BLACK));//set tab text color

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

    }
});
于 2015-12-29T11:29:17.177 回答
1

将这些行放入您的 xml-

app:tabSelectedTextColor="@android:color/white"
app:tabTextColor="@android:color/black"

<android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="fill"
        app:tabMode="fixed"
        app:tabTextAppearance="@style/MyTabLayoutTextAppearance"
        app:tabIndicatorColor="@color/black"
        app:tabSelectedTextColor="@android:color/white"
        app:tabTextColor="@android:color/black"/>
于 2015-12-29T11:35:03.633 回答