更改工具栏溢出图标颜色

时间:2017-04-21 08:43:49

标签: java android android-layout android-view android-toolbar

我的Android应用中有一个android.support.v7.widget工具栏。它的背景颜色是亮橙色,最好看的颜色是白色而不是黑色。

我的默认颜色是黑色而不是白色。由于它会与其他东西冲突,这几乎是不可能的。 我无法将主要文字颜色更改为白色!

我设法改变了标题颜色。 我现在正在寻找的是如何更改动作按钮颜色(白色)。

enter image description here

我的代码:

主要活动:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".UI.activities.MainActivity">

<android.support.v7.widget.Toolbar
    android:id="@+id/r2_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    app:titleTextColor="@color/primary_text_material_light"
    app:subtitleTextColor="@color/primary_text_material_light"
    android:theme="@style/R2Theme.Toolbar"/>

<fragment android:name="com.r2retail.r2retailapp.UI.fragments.SetupFragment"
    android:layout_below="@+id/r2_toolbar"
    android:id="@+id/list"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />


</RelativeLayout>

菜单栏:

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

<item android:id="@+id/about"
    android:icon="@drawable/ic_menu"
    android:title="About"
    app:showAsAction="never"/>

</menu>

样式:

<resources>

<style name="R2Theme" parent="Theme.AppCompat.Light.NoActionBar">=
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorPrimary</item>
    <item name="android:textColorPrimary">@color/secondary_text_material_dark</item>
    <item name="android:textColorSecondaryInverse">@color/primary_text_material_light</item>
</style>

<style name="R2Theme.Toolbar" parent="R2Theme">
    <item name="actionMenuTextColor">@color/primary_text_material_light</item>
</style>

</resources>

6 个答案:

答案 0 :(得分:45)

风格:

<DataGrid Name="Person" ItemsSource="{Binding PersonList}" Sorting="Person_Sorting" ...>

结果:

enter image description here

答案 1 :(得分:2)

解决方案是替换图标本身。

<强>第一

转到值/样式并在styles.xml文件中添加:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="android:actionOverflowButtonStyle">@style/MyActionButtonOverflow</item>
</style>

<style name="MyActionButtonOverflow" parent="android:style/Widget.Holo.Light.ActionButton.Overflow">
    <!--Here you need to put name of drawable you will create during the next step-->
    <item name="android:src">@drawable/your_white_icon</item> 
</style>

<强>第二

然后转到 drawable 文件夹。鼠标右键单击 - &gt;新的 - &gt;矢量资产。 然后按图标图像,从名为 ic_more_vert_black_24dp 的建议图标中选择。

自定义 - &gt;按下 - &gt;光洁度。

然后打开新创建的图标文件。代码看起来像这样。

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24.0"
        android:viewportHeight="24.0">
    <path
        android:fillColor="#FFFFFFFF" <!-- Here u can change color-->
        android:pathData="M12,8c1.1,0 2,-0.9 2,-2s-0.9,-2 -2,-2 -2,0.9 -2,2 0.9,2 2,2zM12,10c-1.1,0 -2,0.9 -2,2s0.9,2 2,2 2,-0.9 2,-2 -0.9,-2 -2,-2zM12,16c-1.1,0 -2,0.9 -2,2s0.9,2 2,2 2,-0.9 2,-2 -0.9,-2 -2,-2z"/>
</vector>

fillColor属性更改为您需要的颜色。将此文件放入第1步中描述的样式。

瞧!我们的三个点的颜色改变不依赖于基本的应用程序样式(#FF2012颜色的结果)。

enter image description here

答案 2 :(得分:2)

另一种方式,代码而不是XML:

public static boolean colorizeToolbarOverflowButton(@NonNull Toolbar toolbar, @ColorInt int color) {
    final Drawable overflowIcon = toolbar.getOverflowIcon();
    if (overflowIcon == null)
        return false;
    toolbar.setOverflowIcon(getTintedDrawable(toolbar.getContext(), overflowIcon, toolbarIconsColor));
    return true;
}

public static Drawable getTintedDrawable(@NonNull Context context, @NonNull Drawable inputDrawable, @ColorInt int color) {
    Drawable wrapDrawable = DrawableCompat.wrap(inputDrawable);
    DrawableCompat.setTint(wrapDrawable, color);
    DrawableCompat.setTintMode(wrapDrawable, Mode.SRC_IN);
    return wrapDrawable;
}

如果成功着色溢出图标,该函数将返回true。

另一种选择,如果你不想使用有色的drawable:

public static boolean colorizeToolbarOverflowButton(@NonNull Toolbar toolbar, @ColorInt Integer color) {
    final Drawable overflowIcon = toolbar.getOverflowIcon();
    if (overflowIcon == null)
        return false;
    final PorterDuffColorFilter colorFilter = toolbarIconsColor == null ? null : new PorterDuffColorFilter(toolbarIconsColor, PorterDuff.Mode.MULTIPLY);
    overflowIcon.setColorFilter(colorFilter);
    return true;
}

答案 3 :(得分:2)

对于AndroidX用户(真的不知道它是否可以使用旧的支持库工作):

TL; DR:

<style name="MyToolbarTheme">
  <item name="colorControlNormal">@color/white</item>
</style>

MyToolbarTheme应用于您的Toolbar视图。


详细解释:

Widget.AppCompat.ActionButton.Overflow扩展了Base.Widget.AppCompat.ActionButton.Overflow。我们将讨论后者:

关于默认实现:

<style name="Base.Widget.AppCompat.ActionButton.Overflow" parent="RtlUnderlay.Widget.AppCompat.ActionButton.Overflow">
  <item name="srcCompat">@drawable/abc_ic_menu_overflow_material</item>
  ...
</style>

关于API 21的实现:

<style name="Base.Widget.AppCompat.ActionButton.Overflow" parent="android:Widget.Material.ActionButton.Overflow">
  <item name="android:src">@null</item>
  <item name="srcCompat">@drawable/abc_ic_menu_overflow_material</item>
</style>

关于API 23和更高版本的实现:

它扩展了android:Widget.Material.ActionButton.Overflow

<style name="Widget.Material.ActionButton.Overflow">
  <item name="src">@drawable/ic_menu_moreoverflow_material</item>
  ...
</style>

我们可以注意到,每个实现都使用@drawable/ic_menu_moreoverflow_material

在此drawable的实现中,您可以看到以下内容:

android:tint="?attr/colorControlNormal"

答案 4 :(得分:1)

如果要更改工具栏中图标(导航图标,菜单项图标)的颜色,只需使用以下代码即可。我已经保存了问题并使用此解决了。

<!--Light Theme-->
<style name="AppThemeLight" parent="Theme.MaterialComponents.Light.NoActionBar">
    <!--other colors and properties-->
    <item name="iconTint">@color/colorBlack</item>
</style>

<!-- Dark/Night theme. -->
<style name="AppThemeDark" parent="Theme.MaterialComponents.NoActionBar">
    <!--other colors and properties-->
    <item name="iconTint">@color/colorWhite</item>
</style>

答案 5 :(得分:0)

如果有人希望以编程方式更改它,但以下方法无效:

mBinding.toolbar.overflowIcon?.setTint(Color.WHITE)

mBinding.toolbar.overflowIcon?.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP)

mBinding.toolbar.overflowIcon?.colorFilter = BlendModeColorFilterCompat.createBlendModeColorFilterCompat(Color.WHITE, BlendModeCompat.SRC_ATOP)

val overflowIcon = ContextCompat.getDrawable(this, R.drawable.dots_vertical_black)
overflowIcon?.setTint(Color.WHITE)
mBinding.toolbar.overflowIcon = overflowIcon

试试这个。

最后,下面的行对我有用(经过 2 天的反复试验-_-)

mBinding.toolbar.menu?.findItem(R.id.menu)?.icon?.setTint(Color.WHITE)
相关问题