在菜单中更改drawale的颜色

时间:2016-08-23 09:58:55

标签: android xml menu background-color menuitem

我的应用程序中有一个菜单:

<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
    android:id="@+id/action_search"
    android:orderInCategory="200"
    android:title="Search"
    android:icon="@drawable/ic_ok"
    app:showAsAction="ifRoom"/>  </menu>

我想知道我是否可以改变ic_ok drawable的颜色。因为默认它是黑色但我需要一个白色。我知道我可以从互联网上下载一个新的白色并采取它,但我想知道我是否可以改变xml代码中的颜色。

我尝试了所有我已知的命令,如:

android:tint="@color/white" 
android:textColor="@color/white"
android:backgroundColor="@color/white"
android:color="@color/white"

但没有任何作用。

感谢您的时间 丹尼尔

2 个答案:

答案 0 :(得分:0)

以编程方式,您可以使用白色获取可绘制的颜色:)

// I suppose your drawable object variable it named drawable
drawable.getConstantState().newDrawable().mutate(); //so we are sure we are using a copy of the original drawable
drawable.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
// now you can use the drawable in your source code

此解决方案基于one of the answers I already made

答案 1 :(得分:0)

这是另一种方式..

  

右键点击drawable目录 - &gt;新的 - &gt;选择矢量资产 - &gt;   点击选择按钮 - &gt;选择您的特定图标 - &gt;按好。   现在图标出现在drawable中..选择你的图标的xml文件..和   在这里你可以改变你的图标的fillColor,以及其他东西,如不透明度,wight