改变动作条的标签强调颜色编程(Changing ActionBar tabs underline c

2019-08-31 17:59发布

我已经创建了操作栏

ActionBar actionbar = getActionBar()

操作栏的背景下而改变

actionbar.setBackgroundDrawable(actionBarBackgroundImage);

现在,我需要改变操作栏标签强调色彩编程。 是否有任何改变动作栏标签强调颜色的方法?

Answer 1:

另外,您可以使用Android操作栏,样式发电机轻松的主题你的动作条和标签。



Answer 2:

这里有一个更简单的方法。 我知道你正在寻找一个纲领性的变化,但是这一次是真的很容易。

我一直在努力与这个好几天,最后还是找到了解决办法。 我使用的程序兼容性。 您可以设置colorAccent在你的主题,这将改变你的动作条高亮颜色。 像这样:

<item name="colorAccent">@color/highlightcolor</item>

这是在上下文中:

<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>

当我最初发布这个答案: Android的标签强调颜色不改变



Answer 3:

我会建议你使用ActionBarSherlock 。 有名为“风格动作条”图书馆提供一个样本。 (这是唯一可以改变动作条的标签强调色彩的方式)

如果您有自定义动作条,然后你必须在动作条样式添加这种风格

或者这里的方式如何做到这一点

创建样式像下面(这里我用ActionBarShareLock如果你不想使用,然后使用Android支持-v4.jar支持所有Android OS版)

<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>

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" />

applay这种风格在活动中的Android清单文件

<activity
            android:name="com.example.tabstyle.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/Theme.AndroidDevelopers" >

为更详细检查这个答案 ,这文章 。


编辑:29-09-2015

ActionBarSherlock不赞成这样或者你可以使用Android design支持库和Android应用appcompat库工具栏(行动酒吧不赞成这样..)和标签。

使用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" />

这里是标签Android设计支持库的样本



Answer 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>


Answer 5:

我试过很多的贴在这里的建议和其他地方,没有运气。 但我认为我设法拼凑出一个(虽然不完美)的解决方案。

所述TabWidget使用的选择器。 本质上,它是示出根据该标签的状态的不同9补丁图像(选择,压制,等等)。 我终于想通了,你可以通过编程生成一个选择。 我开始产生9个修补从http://android-holo-colors.com/ (颜色:#727272,TabWidget:是)。

最大的问题是将颜色设置。 设置彩色滤光片也没做。 所以,我最终改变每个循环中的9块图像的像素的颜色。

...    
/**
 * <code>NinePatchDrawableUtility</code> utility class for manipulating nine patch resources.
 * 
 * @author amossman
 *
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class NinePatchDrawableUtility {

    // Matches the colors in the supported drawables
    private static final int TAB_UNDERLINE_HIGHLIGHT_COLOR = 1417247097;
    private static final int TAB_UNDERLINE_COLOR = -8882056;
    private static final int TAB_PRESSED_COLOR = -2122745479;

    private Resources resources;

    public NinePatchDrawableUtility(Resources resources) {
        this.resources = resources;
    }

    /**
     * Create a <code>StateListDrawable</code> that can be used as a background for the {@link android.widget.TabWidget}</br></br>
     * 
     * <code>
     * FragmentTabHost tabHost = ...</br>
     * NinePatchUtility ninePatchUtility = new NinePatchUtility(getResources());</br>
     * TabWidget tabWidget =  tabHost.getTabWidget();</br>
     * for (int i = 0; i < tabWidget.getChildCount(); i++) {</br>
     * &nbsp;&nbsp;&nbsp;tabWidget.getChildAt(i).setBackground(ninePatchUtility.getTabStateListDrawable(titleColor));</br>
     * }
     * </code>
     * 
     * @param tintColor The color to tint the <code>StateListDrawable</code>
     * @return A new <code>StateListDrawable</code> that has been tinted to the given color
     */
    public StateListDrawable getTabStateListDrawable(int tintColor) {
        StateListDrawable states = new StateListDrawable();
        states.addState(new int[] {android.R.attr.state_pressed},
            changeTabNinePatchColor(resources, R.drawable.cc_tab_selected_pressed_holo, tintColor));
        states.addState(new int[] {android.R.attr.state_focused},
            changeTabNinePatchColor(resources, R.drawable.cc_tab_selected_focused_holo, tintColor));
        states.addState(new int[] {android.R.attr.state_selected},
            changeTabNinePatchColor(resources, R.drawable.cc_tab_selected_holo, tintColor));
        states.addState(new int[] { },
            changeTabNinePatchColor(resources, R.drawable.cc_tab_unselected_holo, tintColor));
        return states;
    }

    /**
     * Change the color of the tab indicator.</br></br>
     * 
     * Supports only the following drawables:</br></br>
     * 
     * R.drawable.cc_tab_selected_pressed_holo</br>
     * R.drawable.cc_tab_selected_focused_holo</br>
     * R.drawable.cc_tab_selected_holo</br>
     * R.drawable.cc_tab_unselected_holo</br></br>
     * 
     * Note: This method is not efficient for large <code>Drawable</code> sizes.
     * 
     * @param resources Contains display metrics and image data
     * @param drawable The nine patch <code>Drawable</code> for the tab
     * @param tintColor The color to tint the <code>Drawable</code>
     * @return A new <code>NinePatchDrawable</code> tinted to the given color
     */
    public NinePatchDrawable changeTabNinePatchColor(Resources resources, int drawable, int tintColor) {

        int a = Color.alpha(tintColor);
        int r = Color.red(tintColor);
        int g = Color.green(tintColor);
        int b = Color.blue(tintColor);
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inMutable = true;
        Bitmap bitmap = BitmapFactory.decodeResource(resources, drawable, opt);
        for (int x = 0; x < bitmap.getWidth(); x++) {
            for (int y = 0; y < bitmap.getHeight(); y++) {
                int color = bitmap.getPixel(x, y);
                if (color == TAB_PRESSED_COLOR) {
                    bitmap.setPixel(x, y, Color.argb((int)(a * 0.5), r, g, b));
                } else if (color == TAB_UNDERLINE_HIGHLIGHT_COLOR) {
                    bitmap.setPixel(x, y, Color.argb((int)(a * 0.9), r, g, b));
                } else if (color == TAB_UNDERLINE_COLOR) {
                    bitmap.setPixel(x, y, tintColor);
                }
            }
        }
        return new NinePatchDrawable(resources, bitmap, bitmap.getNinePatchChunk(), new Rect(), null);
    }

}

使用示例:

/**
 * Theme the tab widget with the defined background color and title color set
 * in the TabManager
 * @param tabWidget
 */
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
public void theme(TabWidget tabWidget) {
    ColorDrawable backgroundDrawable = new ColorDrawable(backgroundColor);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        tabWidget.setBackground(backgroundDrawable);
        tabWidget.setAlpha(0.95f);
    } else {
        backgroundDrawable.setAlpha(242);
        tabWidget.setBackgroundDrawable(backgroundDrawable);
    }
    NinePatchDrawableUtility ninePatchUtility = new NinePatchDrawableUtility(resources);
    for (int i = 0; i < tabWidget.getChildCount(); i++) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            tabWidget.getChildAt(i).setBackground(ninePatchUtility.getTabStateListDrawable(titleColor));
        } else {
            tabWidget.getChildAt(i).setBackgroundDrawable(ninePatchUtility.getTabStateListDrawable(titleColor));
        }
        View tabView = tabWidget.getChildTabViewAt(i);
        tabView.setPadding(0, 0, 0, 0);
        TextView tv = (TextView) tabView.findViewById(android.R.id.title);
        tv.setSingleLine(); // set the texts on the tabs to be single line
        tv.setTextColor(titleColor);
    }
}


Answer 6:

得到了后1长search.Just 2行代码日新月异的标签荧光笔颜色的解决方案,使这项工作完美!

转到价值观/ styles.xml并添加以下动作条主题代码

<item name="colorAccent">@color/Tab_Highlighter</item>

现在请颜色Tab_Highlighter在colors.xml

<color name="Tab_Highlighter">#ffffff</color>


Answer 7:

尝试以下。

写RES /绘制tabs_selector_green.xml。

    <!-- Non focused states -->
<item android:drawable="@android:color/transparent" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="@drawable/layer_bg_selected_tabs_green" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/>

<!-- Focused states -->
<item android:drawable="@android:color/transparent" android:state_focused="true" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="@drawable/layer_bg_selected_tabs_green" android:state_focused="true" android:state_pressed="false" android:state_selected="true"/>

<!-- Pressed -->
<!-- Non focused states -->
<item android:drawable="@android:color/transparent" android:state_focused="false" android:state_pressed="true" android:state_selected="false"/>
<item android:drawable="@drawable/layer_bg_selected_tabs_green" android:state_focused="false" android:state_pressed="true" android:state_selected="true"/>

<!-- Focused states -->
<item android:drawable="@android:color/transparent" android:state_focused="true" android:state_pressed="true" android:state_selected="false"/>
<item android:drawable="@drawable/layer_bg_selected_tabs_green" android:state_focused="true" android:state_pressed="true" android:state_selected="true"/>

写RES /绘制的文件夹layer_bg_selected_tabs_green.xml。

<item>
    <shape android:shape="rectangle" >
        <solid android:color="@color/tab_green" />

        <padding android:bottom="5dp" />
    </shape>
</item>
<item>
    <shape android:shape="rectangle" >
        <solid android:color="#FFFFFF" />
    </shape>
</item>

在java代码写这个。

private static final int[] TABS_BACKGROUND = {
        R.drawable.tabs_selector_orange, R.drawable.tabs_selector_green,
        R.drawable.tabs_selector_red, R.drawable.tabs_selector_blue,
        R.drawable.tabs_selector_yellow };
/*
BLA BLA BLA
*/
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub
    RelativeLayout tabLayout = (RelativeLayout) tab.getCustomView();
    tabLayout.setBackgroundResource(TABS_BACKGROUND[tab.getPosition()]);
    tab.setCustomView(tabLayout);
/* ... */
}


Answer 8:

您可以使用此代码:

actionBar.setStackedBackgroundDrawable(new ColorDrawable(yourColor));



文章来源: Changing ActionBar tabs underline color programmatically