繁体   English   中英

在操作栏中更改选项卡指示器颜色

[英]Change tab indicator color in actionbar

我知道这个问题之前已被问过多次,但我尝试了所有解决方案,但没有任何工作。 所以我决定遵循官方文档: https//developer.android.com/training/basics/actionbar/styling.html

我做了文件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>

actionbar_background只是简单的形状,带有实心填充,并且正常工作。
但是指标仍为蓝色:
actionbar_tab_indicator - 这是从http://jgilfelt.github.io/android-actionbarstylegenerator/生成的文件。我还将生成的图像复制粘贴到可绘制文件夹。 第一个问题是9补丁文件不知何故错了,但我设法手动修复它们。

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

我也尝试过以编程方式更改颜色的解决方案,但它不能按预期工作。 而不是小矩形,它被拉伸以适应整个标签。

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

问候

我总是发现使用Android Asset Studio中的工具最简单,因为我没有特别的图形倾向。 特别是这个工具与你想要完成的工作非常相关: http//jgilfelt.github.io/android-actionbarstylegenerator/ 可以在http://romannurik.github.io/AndroidAssetStudio/找到全套工具。 只需选择所需的颜色/样式,下载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>

你能试一下吗:

<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" for CustomActionBarTheme

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

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

<item android:state_selected="true" android:drawable="@drawable/background_tab_selected" />

<item android:state_selected="false" android:drawable="@drawable/background_tab_unselected" />

</selector>

@ drawable / background_tab_selected和@ drawable / background_tab_unselected是九个补丁图像,分别表示选定和未选择的选项卡状态。

注意: background_tab_selected九补丁图像的可缩放区域应仅包含与正常背景匹配的像素。 像这样: 在此输入图像描述 (我的意思是左边和前九个补丁指标)

然后在你的活动的onCreate()中,找到TabWidget对象并设置它们的背景:

final TabWidget tabs = (TabWidget) findViewById(android.R.id.tabs);

tabs.getChildTabViewAt(0).setBackgroundResource(R.drawable.tab_indicator);

tabs.getChildTabViewAt(1).setBackgroundResource(R.drawable.tab_indicator);

tabs.getChildTabViewAt(2).setBackgroundResource(R.drawable.tab_indicator);

tabs.setDividerDrawable(R.drawable.tab_divider);

你也可以通过setDividerDrawable()方法设置divider drawable。

希望有所帮助。

更改actionBar选项卡指示器颜色很困难,您可以使用SlidingTabLayout来动态更改指示器颜色,也可以更改指示器的高度。

要获取SlidingTabLayout类(&widget),请在android studio中导入示例项目:

  1. SlidingTabBasic :用于指示器的恒定颜色。
  2. SlidingTabBasic :用于动态标签指示器颜色。

在slidingTabLayout中,有各种选项可以自定义选项卡,指示器及其文本。

在您的项目中使用它,享受!

在.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"`
    />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM