如何更改新材质主题中后退箭头的颜色?

Fer*_*nch 181 java user-interface android colors android-styles

我已将我的SDK更新为API 21,现在后退/上移图标是指向左侧的黑色箭头.

黑色后箭头

我希望它是灰色的.我怎样才能做到这一点?

例如,在Play商店中,箭头为白色.

我这样做是为了设置一些风格.我已经习惯@drawable/abc_ic_ab_back_mtrl_am_alphahomeAsUpIndicator.drawable是透明的(只有alpha),但箭头显示为黑色.我想知道我是否能像我一样设置颜色DrawerArrowStyle.或者,如果唯一的解决方案是创建我的@drawable/grey_arrow并使用它homeAsUpIndicator.

<!-- Base application theme -->
<style name="AppTheme" parent="Theme.AppCompat.Light">

    <item name="android:actionBarStyle" tools:ignore="NewApi">@style/MyActionBar</item>
    <item name="actionBarStyle">@style/MyActionBar</item>

    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>

    <item name="homeAsUpIndicator">@drawable/abc_ic_ab_back_mtrl_am_alpha</item>
    <item name="android:homeAsUpIndicator" tools:ignore="NewApi">@drawable/abc_ic_ab_back_mtrl_am_alpha</item>
</style>

<!-- ActionBar style -->
<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid">

    <item name="android:background">@color/actionbar_background</item>
    <!-- Support library compatibility -->
    <item name="background">@color/actionbar_background</item>
</style>

<!-- Style for the navigation drawer icon -->
<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
    <item name="spinBars">true</item>
    <item name="color">@color/actionbar_text</item>
</style>
Run Code Online (Sandbox Code Playgroud)

到目前为止,我的解决方案是采用@drawable/abc_ic_ab_back_mtrl_am_alpha看似白色的,并使用照片编辑器将其绘制成我想要的颜色.它的工作原理,但我更喜欢使用@color/actionbar_textDrawerArrowStyle.

Car*_*les 340

你可以通过代码实现它.获取后退箭头drawable,使用过滤器修改其颜色,并将其设置为后退按钮.

final Drawable upArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
upArrow.setColorFilter(getResources().getColor(R.color.grey), PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setHomeAsUpIndicator(upArrow);
Run Code Online (Sandbox Code Playgroud)

修订版1:

从API 23(Marshmallow)开始,可绘制资源abc_ic_ab_back_mtrl_am_alpha更改为abc_ic_ab_back_material.

编辑:

您可以使用此代码来实现所需的结果:

toolbar.getNavigationIcon().setColorFilter(getResources().getColor(R.color.blue_gray_15), PorterDuff.Mode.SRC_ATOP);
Run Code Online (Sandbox Code Playgroud)

  • 有用.请注意你应该使用`ContextCompat.getDrawable(this,R.drawable.abc_ic_ab_back_mtrl_am_alpha)`因为`getResources.getDrawable(int)`已被弃用 (23认同)
  • 请注意,abc_ic_ab_back_mtrl_am_alpha在23.2.0支持库中更改为abc_ic_ab_back_material (12认同)
  • 这是唯一适用于我而不用使用colorControlNormal改变所有其他小部件的色调的东西 (4认同)
  • 这个解决方案的问题是它会绘制所有其他后退箭头,如果您只想更改一个并将其余部分保留为白色 (3认同)
  • 另一种可能的解决方案是让您自己获取该资源并将其放在可绘制文件夹上:) (3认同)
  • 请注意,如果您使用的主题为".NoActionBar"工具栏,则为getSupportActionBar().setHomeAsUpIndicator(upArrow); 不行.使用toolbar.setNavigationIcon(upArrow); 然后 (3认同)
  • 最好更改箭头的颜色,而不要使用图像。属性`colorControlNormal`决定箭头的颜色。因此,在工具栏自定义主题中更改此属性即可享受:) (2认同)

roc*_*cko 196

查看ToolbarTintManager源,drawable/abc_ic_ab_back_mtrl_am_alpha使用style属性的值着色colorControlNormal.

我确实尝试在我的项目中设置这个(<item name="colorControlNormal">@color/my_awesome_color</item>在我的主题中),但它对我来说仍然是黑色的.

更新:

找到了.您需要设置actionBarTheme属性( actionBarStyle)colorControlNormal.

例如:

<style name="MyTheme" parent="Theme.AppCompat.Light">        
    <item name="actionBarTheme">@style/MyApp.ActionBarTheme</item>
    <item name="actionBarStyle">@style/MyApp.ActionBar</item>
    <!-- color for widget theming, eg EditText. Doesn't effect ActionBar. -->
    <item name="colorControlNormal">@color/my_awesome_color</item>
    <!-- The animated arrow style -->
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>

<style name="MyApp.ActionBarTheme" parent="@style/ThemeOverlay.AppCompat.ActionBar">       
    <!-- THIS is where you can color the arrow! -->
    <item name="colorControlNormal">@color/my_awesome_color</item>
</style>

<style name="MyApp.ActionBarStyle" parent="@style/Widget.AppCompat.Light.ActionBar">
    <item name="elevation">0dp</item>      
    <!-- style for actionBar title -->  
    <item name="titleTextStyle">@style/ActionBarTitleText</item>
    <!-- style for actionBar subtitle -->  
    <item name="subtitleTextStyle">@style/ActionBarSubtitleText</item>

    <!-- 
    the actionBarTheme doesn't use the colorControlNormal attribute
    <item name="colorControlNormal">@color/my_awesome_color</item>
     -->
</style>
Run Code Online (Sandbox Code Playgroud)

  • 如果你的父主题派生自一个"NoActionBar"主题,那么在根主题而不是在`actionBarTheme`中设置`colorControlNormal`就是要走的路. (10认同)
  • 不幸的是,这是一个21级API,如果你想支持下面的任何东西它将无法正常工作. (6认同)
  • 我需要添加<item name ="android:colorControlNormal"> ... </ item>和android:前缀 (5认同)
  • 这应该是公认的答案.这次真是万分感谢.问题,我无法找到展开SearchView时显示的箭头着色的样式.任何线索?它显然不受这个答案的影响. (4认同)
  • 它只花了我2个小时的搜索和失败,再次搜索终于到达这一点.有时Android中的风格'系统'让我疯了!当您有一个活动不使用与其他活动相同的样式时,此方法尤为重要. (3认同)

Har*_*ung 115

试过上面的所有建议.我设法更改工具栏中导航图标默认后退按钮箭头的颜色的唯一方法是在这样的基本主题中设置colorControlNormal.可能是由于父级使用的是Theme.AppCompat.Light.NoActionBar

<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
  <item name="colorControlNormal">@color/white</item> 
  //the rest of your codes...
</style>
Run Code Online (Sandbox Code Playgroud)

  • 最简单的答案,而不是摆弄其他代码,只需在`style.xml`中添加这一行 (5认同)
  • @AdnanAli如果你正在使用AppCompat它也可以在<API 21上工作.显然,AppCompat _is_在这里被使用,因为`colorControlNormal`是_not_前缀为`android:`. (3认同)
  • 但这也会改变其他控件的颜色,例如单选按钮和文本编辑下的行。 (3认同)
  • 请记住,colorControlNormal需要API 21 (2认同)
  • 但这也改变了所有其他小部件的色彩..任何不影响其他小部件的方式,如复选框和单选按钮? (2认同)

Neo*_*Neo 61

需要在工具栏主题中添加单个属性 -

<style name="toolbar_theme" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="colorControlNormal">@color/arrow_color</item>
</style>
Run Code Online (Sandbox Code Playgroud)

将此toolbar_theme应用于工具栏.

要么

你可以直接申请你的主题 -

<style name="CustomTheme" parent="Theme.AppCompat.Light.NoActionBar">
  <item name="colorControlNormal">@color/arrow_color</item> 
  //your code ....
</style>
Run Code Online (Sandbox Code Playgroud)

  • 尝试接受的答案(也许是在网上找到的最流行的解决方案)对我来说不起作用,但***这个工作***:)在使用Android编程时非常疯狂. (4认同)
  • 最简单的答案.这不会改变其他控件主题.您只需将自定义工具栏主题设置为工具栏即可.:) (4认同)

S b*_*uce 44

只需添加

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

到你当前的应用主题.

  • 这也改变了 android.support.v7.widget.AppCompatCheckBox 颜色 (2认同)

小智 32

如果使用Toolbar,你可以试试这个

Drawable drawable = toolbar.getNavigationIcon();
drawable.setColorFilter(ContextCompat.getColor(appCompatActivity, colorId), PorterDuff.Mode.SRC_ATOP);
Run Code Online (Sandbox Code Playgroud)

  • 请注意,在尝试调用getNavigationIcon()之前,可能需要调用此方法:getSupportActionBar()。setDisplayHomeAsUpEnabled(true); (2认同)

小智 31

正如大多数之前的评论所说,解决方案是添加

<item name="colorControlNormal">@color/white</item> 到您的应用主题.但请确认您的布局上的工具栏元素中没有定义其他主题.

     <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
Run Code Online (Sandbox Code Playgroud)

  • 很棒......我尝试了所有关于在主题中添加它的答案,但它没有用......正如你所提到的,我的工具栏中有android:theme ="@ style/ThemeOverlay.AppCompat.ActionBar"属性.一旦我删除它,它就开始工作了. (3认同)

cap*_*wag 10

Carles的答案是正确的答案,但是在我写这个答案的时候,很少有像getDrawable(),getColor()这样的方法被弃用了.所以更新的答案是

Drawable upArrow = ContextCompat.getDrawable(context, R.drawable.abc_ic_ab_back_mtrl_am_alpha);
upArrow.setColorFilter(ContextCompat.getColor(context, R.color.white), PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setHomeAsUpIndicator(upArrow);
Run Code Online (Sandbox Code Playgroud)

在其他一些stackoverflow查询之后,我发现调用ContextCompat.getDrawable()类似于

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    return resources.getDrawable(id, context.getTheme());
} else {
    return resources.getDrawable(id);
}
Run Code Online (Sandbox Code Playgroud)

ContextCompat.getColor()类似

public static final int getColor(Context context, int id) {
    final int version = Build.VERSION.SDK_INT;
    if (version >= 23) {
        return ContextCompatApi23.getColor(context, id);
    } else {
        return context.getResources().getColor(id);
    }
}
Run Code Online (Sandbox Code Playgroud)

链接1:ContextCompat.getDrawable()

链接2:ContextCompat.getColor()


GFr*_*red 7

我找到了一个适用于Lollipop之前的解决方案.在"actionBarWidgetTheme"中设置"colorControlNormal"以更改homeAsUpIndicator颜色.从上面修改rockgecko的答案看起来像这样:

<style name="MyTheme" parent="Theme.AppCompat.Light">        
    <item name="actionBarTheme">@style/MyApp.ActionBarTheme</item>
    <item name="actionBarStyle">@style/MyApp.ActionBar</item>
    <!-- color for widget theming, eg EditText. Doesn't effect ActionBar. -->
    <item name="colorControlNormal">@color/my_awesome_color</item>
    <!-- The animated arrow style -->
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
    <!-- The style for the widgets on the ActionBar. -->
    <item name="actionBarWidgetTheme">@style/WidgetStyle</item>
</style>

<style name="WidgetStyle" parent="style/ThemeOverlay.AppCompat.Light">
    <item name="colorControlNormal">@color/my_awesome_color</item>
</style>
Run Code Online (Sandbox Code Playgroud)


nut*_*les 7

在compileSdkVersoin 25上,你可以这样做:

styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light">
     <item name="android:textColorSecondary">@color/your_color</item>
</style>
Run Code Online (Sandbox Code Playgroud)


Sum*_*ena 6

在此输入图像描述

更改菜单导航图标颜色

最终转换菜单图标颜色

在style.xml中:

<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
    <item name="android:textColor">@color/colorAccent</item>


</style>

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







In Mainfests.xml :

<activity android:name=".MainActivity"
        android:theme="@style/MyMaterialTheme.Base"></activity>
Run Code Online (Sandbox Code Playgroud)


Jum*_*mpa 5

使用以下方法:

private Drawable getColoredArrow() {
    Drawable arrowDrawable = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
    Drawable wrapped = DrawableCompat.wrap(arrowDrawable);

    if (arrowDrawable != null && wrapped != null) {
        // This should avoid tinting all the arrows
        arrowDrawable.mutate();
        DrawableCompat.setTint(wrapped, Color.GRAY);
    }

    return wrapped;
}
Run Code Online (Sandbox Code Playgroud)

现在您可以使用以下方法设置drawable:

getSupportActionBar().setHomeAsUpIndicator(getColoredArrow());
Run Code Online (Sandbox Code Playgroud)


Vin*_*hwa 5

另一个可能对您有用的解决方案是不将您的工具栏声明为应用程序的操作栏( bysetActionBarsetSupportActionBar),并使用本页另一个答案中提到的代码在您的 onActivityCreated 中设置后退图标

final Drawable upArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
upArrow.setColorFilter(getResources().getColor(R.color.grey), PorterDuff.Mode.SRC_ATOP);
toolbar.setNavigationIcon(upArrow);
Run Code Online (Sandbox Code Playgroud)

现在,onOptionItemSelected当您按下后退按钮时,您将不会收到回调。但是,您可以使用setNavigationOnClickListener. 这就是我所做的:

toolbar.setNavigationOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        getActivity().onBackPressed(); //or whatever you used to do on your onOptionItemSelected's android.R.id.home callback
    }
});
Run Code Online (Sandbox Code Playgroud)

如果您使用菜单项,我不确定它是否会起作用。


Sam*_*Sam 5

以下是我在材质组件中的做法:

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <item name="drawerArrowStyle">@style/AppTheme.DrawerArrowToggle</item>
</style>

<style name="AppTheme.DrawerArrowToggle" parent="Widget.AppCompat.DrawerArrowToggle">
    <item name="color">@android:color/white</item>
</style>
Run Code Online (Sandbox Code Playgroud)


Suy*_*yog 5

解决方法很简单

只需将以下行放入名为AppTheme 的样式中

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

现在您的整个 xml 代码将如下所示(默认样式)。

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

        <item name="colorPrimary">#0F0F0F</item>
        <item name="android:statusBarColor">#EEEEF0</item>
        <item name="android:windowLightStatusBar">true</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowActivityTransitions">true</item>

        <item name="colorControlNormal">@color/white</item>
</style>
Run Code Online (Sandbox Code Playgroud)