如何更改Selected Tab的颜色

Son*_*ali 20 android android-actionbar

如何在选中时更改标签的颜色,请参见下面的屏幕截图:

我在ActionBar中显示橙色,同样我想用橙色代替浅蓝色.

要在ActionBar背景中显示橙色,我使用下面的代码:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="Theme.MyAppTheme" parent="android:style/Theme.Holo.Light">
         <item name="android:actionBarStyle">@style/Theme.MyAppTheme.ActionBar</item>
    </style>

   <style name="Theme.MyAppTheme.ActionBar" parent="android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">#FF4444</item>
    </style>

</resources>
Run Code Online (Sandbox Code Playgroud)

小智 16

我真的建议你使用Actionbar Style Generator.

使用该工具,您可以轻松地在工具栏中设置图形元素的主题.

  • 现在这已经过时了. (2认同)

小智 9

只需在选项卡布局中添加以下2个属性即可.

app:tabSelectedTextColor="@color/color_primary_text"
app:tabTextColor="@color/color_secondary_text"
Run Code Online (Sandbox Code Playgroud)

xml中的整个tablayout如下所示

<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_gravity="bottom"
android:background="@color/button_background"
android:fillViewport="true"
app:tabBackground="@drawable/fixed_bottom_button"
app:tabIndicatorColor="@color/color_primary_text"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/color_primary_text"
app:tabTextColor="@color/color_secondary_text" />
Run Code Online (Sandbox Code Playgroud)


小智 6

把这个函数调用到你的Activity并传​​递tabhost作为参数

public static void setTabColor(TabHost tabhost) {

            for (int i = 0; i < tabhost.getTabWidget().getChildCount(); i++) {
                tabhost.getTabWidget().getChildAt(i)
                        .setBackgroundResource(R.drawable.header_blank); // unselected
            }
            tabhost.getTabWidget().setCurrentTab(0);
            tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab())
                    .setBackgroundResource(R.drawable.tab_selected_new); // selected
                                                                            // //have
                                                                            // to
                                                                            // change
        }
Run Code Online (Sandbox Code Playgroud)

按以下方式调用此方法

        setTabColor(tabHost);
        tabHost.setOnTabChangedListener(new OnTabChangeListener() {

            @Override
            public void onTabChanged(String arg0) {

                setTabColor(tabHost);
            }
             });
Run Code Online (Sandbox Code Playgroud)

希望这对你有用