更改ActionBar选项卡以编程方式为颜色加下划线

Kar*_*ick 23 android android-actionbar android-tabs

我创建了动作栏

ActionBar actionbar = getActionBar()
Run Code Online (Sandbox Code Playgroud)

操作栏的背景更改为

actionbar.setBackgroundDrawable(actionBarBackgroundImage);
Run Code Online (Sandbox Code Playgroud)

现在我需要以编程方式更改操作栏选项卡下划线颜色.有没有任何方法可以更改操作栏标签下划线颜色?

小智 10

或者,您可以使用Android操作栏样式生成器轻松设置操作栏和选项卡的主题.


Ken*_*and 9

这是一个更容易的方法.我知道你正在寻找一个程序化的改变,但这个真的很容易.

几天来我一直在努力,但终于找到了解决方案.我正在使用AppCompat.您可以colorAccent在主题中进行设置,这将更改ActionBar上的突出显示颜色.像这样:

<item name="colorAccent">@color/highlightcolor</item>
Run Code Online (Sandbox Code Playgroud)

这是在上下文中:

<style name="LightTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/darkgrey</item>
    <item name="colorPrimaryDark">@color/black</item>
    <item name="colorAccent">@color/highlightcolor</item>
</style>
Run Code Online (Sandbox Code Playgroud)

我最初发布此答案的地方:Android标签下划线颜色不变


Dha*_*mar 6

我建议你使用ActionBarSherlock.库中有一个名为"Style ActionBar"的样本.(这是你可以改变ActionBar标签下划线颜色的唯一方法)

如果你有自定义的ActionBar,那么你必须在ActionBar Style中添加这个样式

或者这是如何做到这一点

在此输入图像描述

创建如下的样式(这里我已经使用了ActionBarShareLock,如果你不想使用那么使用android-support-v4.jar来支持所有Android操作系统版本)

<style name="Theme.AndroidDevelopers" parent="Theme.Sherlock.Light">
        <item name="android:actionBarTabStyle">@style/MyActionBarTabStyle</item>
        <item name="actionBarTabStyle">@style/MyActionBarTabStyle</item>
    </style>

    <!-- style for the tabs -->
    <style name="MyActionBarTabStyle" parent="Widget.Sherlock.Light.ActionBar.TabBar">
        <item name="android:background">@drawable/actionbar_tab_bg</item>
        <item name="android:paddingLeft">32dp</item>
        <item name="android:paddingRight">32dp</item>
Run Code Online (Sandbox Code Playgroud)

actionbar_tab_bg.xml

<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/ad_tab_unselected_holo" />
<item android:state_focused="false" android:state_selected="true"  android:state_pressed="false" android:drawable="@drawable/ad_tab_selected_holo" />
<item android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/ad_tab_selected_pressed_holo" />
<item android:state_selected="true"  android:state_pressed="true" android:drawable="@drawable/ad_tab_selected_pressed_holo" />
Run Code Online (Sandbox Code Playgroud)

在Android清单文件中的活动中应用此样式

<activity
            android:name="com.example.tabstyle.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/Theme.AndroidDevelopers" >
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息,请查看此答案本文.


编辑:29-09-2015

不推荐使用ActionBarSherlock,因此您可以使用Android design支持库和Android应用程序appcompat库来TOOLBAR(Action-Bar不推荐使用)和TABS.

使用TabLayout如下

<android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabGravity="center"
            app:tabMode="scrollable"
            app:tabSelectedTextColor="@color/white"
            app:tabIndicatorColor="@color/colorPrimary"
            app:tabIndicatorHeight="2dip"
            app:tabTextAppearance="?android:attr/textAppearanceMedium"
            app:tabTextColor="@color/colorAccent" />
Run Code Online (Sandbox Code Playgroud)

这里是带有选项卡的android设计支持库的示例


Sin*_*Raj 4

请参阅,用于自定义操作栏,

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActivityTheme" parent="@android:style/Theme.Holo">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <!-- other activity and action bar styles here -->
    </style>

    <!-- style for the action bar backgrounds -->
    <style name="MyActionBar" parent="@android:style/Widget.Holo.ActionBar">
        <item name="android:background">@drawable/ab_background</item>
        <item name="android:backgroundStacked">@drawable/ab_background</item>
        <item name="android:backgroundSplit">@drawable/ab_split_background</item>
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

  • 感谢西诺的回复。我知道这种自定义操作栏的方法。但我需要通过代码自定义操作栏选项卡。 (4认同)