Android 在actionbar中更改选项卡指示器颜色

Android 在actionbar中更改选项卡指示器颜色,android,Android,我知道这个问题以前被问过很多次,但我尝试了所有的解决方案,但没有任何效果。因此,我决定遵循官方文件: 我创建了文件themes.xml: <?xml version="1.0" encoding="utf-8"?> <resources> <!-- the theme applied to the application or activity --> <style name="CustomActionBarTheme"

我知道这个问题以前被问过很多次,但我尝试了所有的解决方案,但没有任何效果。因此,我决定遵循官方文件:

我创建了文件themes.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme"
        parent="@android:style/Theme.Holo.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <item name="android:actionBarTabStyle">@style/MyActionBarTabs</item>
    </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar"
        parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@drawable/actionbar_background</item>
    </style>

    <!-- ActionBar tabs styles -->
    <style name="MyActionBarTabs"
        parent="@android:style/Widget.Holo.ActionBar.TabView">
        <!-- tab indicator -->
        <item name="android:background">@drawable/actionbar_tab_indicator</item>
    </style>
</resources>

@样式/MyActionBar
@风格/我的行动
@可绘制/操作栏\u背景
@可绘图/操作栏选项卡指示器
actionbar_背景只是一个简单的形状,带有实心填充,并且正在工作。
但指示灯仍为蓝色:
actionbar_选项卡_指示器-这是从 我还将粘贴生成的图像复制到drawable文件夹。第一个问题是9-patch文件有点错误,但我已经设法手动修复了它们。

我正在AndroidManifest.xml中设置主题:android:theme=“@style/CustomActionBarTheme”

我也尝试过通过编程改变颜色的解决方案,但效果并不理想。它不是一个小矩形,而是被拉伸以适应整个标签

在最新的android studio中工作的任何解决方案(编程或xml)都将非常好。但它必须看起来像原始标签,但与其他颜色-在我的情况下红色

关于

你能试试吗:

<style name="MyActionBarTabs"  
              parent="@android:style/Widget.Holo.Light.ActionBar.TabView">
    <item name="android:background">@drawable/actionbar_tab_indicator</item>

</style>

@可绘图/操作栏选项卡指示器

由于您的
parent=“@android:style/Theme.Holo.Light.DarkActionBar”
对于
CustomActionBarTheme

首先,在drawable文件夹(tab_indicator.xml)中定义一个选择器

也可以通过setDividerDrawable()方法设置分隔符drawable


希望有帮助。

更改操作栏选项卡指示器颜色很困难,相反,您可以使用SlidingTabLayout来动态更改指示器颜色,也可以更改指示器的高度

要获取SlidingTableOut类(&小部件),请在android studio中导入示例项目:

  • :用于整个指示器的恒定颜色
  • :用于动态选项卡指示器颜色 在SlidingTableOut中,有各种选项可用于自定义选项卡、指示器及其文本


    在你的项目中使用它,并享受它

    我一直觉得使用安卓资产工作室的工具最简单,因为我不是特别喜欢图形。此工具尤其与您要完成的目标非常相关: . 全套工具可在以下网址找到: . 只需选择所需的颜色/样式,下载zip文件,然后将文件复制/粘贴到项目中。然后在您的AndroidManifest.xml文件中,在包含ActionBar的活动中,引用您在生成器(您在网站上填写的第一个文本框)中选择的任何“样式名称”。因此,例如,如果您在生成器中调用样式“Custom_action_bar”,您将按如下方式将其添加到活动中:

    <activity
        android:name="com.company.appname.ActivityName"
        android:label="@string/somename"
        android:theme="@style/Theme.Custom_action_bar">
        <!--the above line is all you need to add-->
    </activity>
    

    在.xml文件中,可以使用以下代码更改tabIndicator的高度

    <android.support.design.widget.TabLayout`
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="62dp"
        android:background="#001e8b"
        android:elevation="6dp"
        app:tabSelectedTextColor="@android:color/white"
        app:tabIndicatorHeight="5dp"
        app:tabIndicatorColor="@android:color/white"
        app:tabTextColor="@android:color/darker_gray"`
        />
    
    
    
    你的minSDK版本是什么?@MounirElfassi,现在是15。(安卓4.0.3)你在使用任何用于指示器的库吗?不,我只想使用普通的TabHost(带片段)并且只更改指示器的颜色。刚刚检查过。没有区别。因此,可能您的选择器有问题,您应该点击选择器xml。这会改变整个选项卡的背景。如何单独改变指示器的颜色?我已经尝试过相同的解决方案。在我的例子中,setBackgroundResource将9-patch扩展到一个完整的选项卡,而不是绘制小的指示器(如蓝色的指示器)。这段代码对我来说很好。您可以在上查看应用程序,并查看其外观。注意:背景的可缩放区域\u选项卡\u选定的九块图像应仅包含与正常背景匹配的像素。请查看我答案中的嵌入图像(顶部和左侧的九个补丁指示器)我尝试了你的图像,结果如下(第四个选项卡,无挫折资源显示差异):这节省了我的时间,谢谢!虽然FAQ并没有明确说明这一点,但我将其与@alimephour-answer结合在一起,使其发挥了作用!大量的文件,但它的工作。
    <activity
        android:name="com.company.appname.ActivityName"
        android:label="@string/somename"
        android:theme="@style/Theme.Custom_action_bar">
        <!--the above line is all you need to add-->
    </activity>
    
    <android.support.design.widget.TabLayout`
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="62dp"
        android:background="#001e8b"
        android:elevation="6dp"
        app:tabSelectedTextColor="@android:color/white"
        app:tabIndicatorHeight="5dp"
        app:tabIndicatorColor="@android:color/white"
        app:tabTextColor="@android:color/darker_gray"`
        />