【问题标题】:How to change TabWidget color?如何更改 TabWidget 颜色?
【发布时间】:2015-12-29 11:03:35
【问题描述】:


我是 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 可以做我需要的事情吗?

【问题讨论】:

    标签: android android-tabhost tabwidget android-style-tabhost


    【解决方案1】:

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

    【讨论】:

    • 它正在工作,但我可以使用 tabHost 实现相同的效果吗?
    • 谢谢。我想没有一些编码是不可能的。
    【解决方案2】:

    将这些行放入您的 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"/>
    

    【讨论】:

    • 感谢它的工作。但是我可以对 tabHost 做同样的事情吗?不是 TabLayout。
    • 谢谢。我试图在不编码的情况下找到解决方案,但也许这是不可能的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-16
    • 2016-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多