简体   繁体   English

使用 appcompat-v7 在 Actionmode 中自定义“后退”箭头颜色

[英]Customizing the 'back' arrow color in Actionmode with appcompat-v7

My main theme is based on Theme.AppCompat.Light as follows:我的主题是基于Theme.AppCompat.Light如下:

<style name="core" parent="Theme.AppCompat.Light" >
    <!-- Material, Yo!-->
    <item name="colorPrimary">@color/theme_main_color</item>
    <item name="colorPrimaryDark">@color/deep_purple</item>
    <item name="colorAccent">@color/theme_accent_color</item>
    <item name="android:navigationBarColor" tools:ignore="NewApi">?attr/colorPrimary</item>
    <!-- Toolbar -->
    <item name="theme">@style/my_toolbar_theme</item>
    <item name="drawerArrowStyle">@style/my_drawer_arrow</item>
    <!-- Actionbar -->
    <item name="android:actionBarDivider">@null</item>
    <item name="android:actionBarTabStyle">@null</item>
    <item name="android:actionBarTabBarStyle">@null</item>
    <!-- Contextual Actionbar -->
    <item name="windowActionModeOverlay">true</item>
    <item name="actionModeBackground">?attr/colorAccent</item>
    <item name="actionModeStyle">@style/my_actionmode_style</item>
</style>

<style name="my_drawer_arrow" parent="Widget.AppCompat.DrawerArrowToggle" >
    <item name="spinBars">true</item>
    <item name="color">@color/theme_accent_color</item>
</style>

<style name="my_toolbar_style">
    <item name="android:id">@id/toolbar</item>
    <item name="android:minHeight">?attr/actionBarSize</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_width">match_parent</item>
    <item name="android:background">?attr/colorPrimary</item>
    <item name="android:elevation" tools:ignore="NewApi">5dp</item>
</style>

<style name="my_actionmode_style" parent="Widget.AppCompat.Light.ActionMode.Inverse" >
    <item name="titleTextStyle">@style/my_actionmode_title_style</item>
    <item name="subtitleTextStyle">@style/my_actionmode_subtitle_style</item>
</style>

<style name="my_toolbar_style.gradient">
    <item name="android:background">@drawable/ab_gradient_bg</item>
</style>

<style name="my_toolbar_theme" parent="ThemeOverlay.AppCompat.Dark.ActionBar" >
    <!-- Customize the toolbar here -->
</style>

<style name="my_actionmode_title_style" parent="TextAppearance.AppCompat.Widget.ActionMode.Title">
    <item name="android:textColor">@color/std_white</item>
</style>

<style name="my_actionmode_subtitle_style" parent="TextAppearance.AppCompat.Widget.ActionMode.Subtitle">
    <item name="android:textColor">@android:color/white</item>
</style>

In actionmode I'd like to see white title/subtitles with a white back arrow.动作模式中,我希望看到带有白色后退箭头的白色标题/字幕。 I've been able to set the title and subtitle text colors to white, but the back arrow remains black.我已经能够将标题和副标题文本颜色设置为白色,但后退箭头仍然是黑色。 This issue only occurs in places where I'm using the SupportActionbar and not the toolbar (the back arrow in toolbar is white).这个问题只发生在我使用 SupportActionbar 而不是工具栏的地方(工具栏中的后退箭头是白色的)。

This is my toolbar in ActionMode.这是我在 ActionMode 中的工具栏。

这是我在 ActionMode 中的工具栏

This is the actionbar in ActionMode.这是 ActionMode 中的操作栏。 Same phone, same app, same themes, just a different activity.相同的手机,相同的应用程序,相同的主题,只是不同的活动。

这是 ActionMode 中的操作栏

I can't find anything in the themes that would determine this color.我在主题中找不到任何可以确定此颜色的内容。 According to the source, the icon is @drawable/abc_ic_ab_back_mtrl_am_alpha , which is white, so something must be tinting it.根据消息来源,图标是@drawable/abc_ic_ab_back_mtrl_am_alpha ,它是白色的,所以一定有什么东西给它着色了。 Where does this black-ish color come from?这种黑乎乎的颜色从何而来?

To customize this using AppCompat:要使用 AppCompat 对其进行自定义:

In your app level theme:在您的应用级别主题中:

<style name="MyTheme" parent="Theme.AppCompat.Light">
    <item name="actionModeCloseButtonStyle">@style/myclosebutton</item>
</style>

In the CloseButtonStyle:在 CloseButtonStyle 中:

<style name="myclosebutton" parent="Widget.AppCompat.Light.ActionButton.CloseMode">
    <item name="android:tint">#ff0</item> <!-- whatever color -->
</style> 

I had the same problem and I have solved it by explicitly declaring the icon I want to use in the style:我遇到了同样的问题,我通过明确声明我想在样式中使用的图标来解决它:

<item name="android:actionModeCloseDrawable">@drawable/ab_back</item>

Where @drawable/ab_back is provided by me. @drawable/ab_back 由我提供。

This is not ideal, as I would like to correctly set the style and let it work inheriting the right colours from the parent theme, without the need of declaring so specific details.这并不理想,因为我想正确设置样式并让它从父主题继承正确的颜色,而无需声明如此具体的细节。 But, I feel everything around the ActionBar, SupportActionBar, SherlockActionBar, ActionBarActivity, FragmentActivityWithASherlockSupportActionToolbar is SO broken and bad engineered that I'm happy to solve it with a workaround.但是,我觉得ActionBar、SupportActionBar、SherlockActionBar、ActionBarActivity、FragmentActivityWithASherlockSupportActionToolbar周围的一切都太坏了,而且设计得很糟糕,我很高兴通过一种变通方法来解决它。

Try "colorControlNormal" Property Like given Below in your "NoActionBar Theme"尝试像下面在“NoActionBar 主题”中给出的“colorControlNormal”属性

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="colorControlNormal">@android:color/white</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

I have best answer for this我对此有最佳答案

 Spannable text = new SpannableString( mode.getTitle());
                    text.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorPrimaryDark)),
                            0, text.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                    mode.setTitle(text);

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

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