更改工具栏未隐藏的菜单图标颜色

时间:2016-10-20 07:40:29

标签: android

如何更改工具栏搜索图标颜色?我希望它是白色的。我用自己的白色画笔改变了,但结果仍然是黑色的。我已阅读this post,但帖子问题是文字颜色而非图标颜色。

截图

enter image description here

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.zihadrizkyef.project_katalog_grosirmukenalukis_admin.MainActivity"

    android:id="@+id/activity_home"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/myToolbar"
        android:layout_height="?android:attr/actionBarSize"
        android:layout_width="match_parent"
        android:background="@color/colorPrimary"
        android:theme="@style/myToolbarTheme"/>

    <ListView
        android:id="@+id/lvProduct"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

style.xml

<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <item name="android:buttonStyle">@style/MyButtonStyle</item>
    </style>

    <style name="MyButtonStyle" parent="Base.Widget.AppCompat.Button">
        <item name="android:textAllCaps">false</item>
        <item name="android:background">@drawable/mybutton</item>
        <item name="android:textColor">@color/myWidgetContentColor</item>
    </style>

    <style name="myToolbarTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:textColorPrimary">@color/myWidgetContentColor</item>
    </style>
</resources>

menu_home.xml

<?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/action_search"
        app:showAsAction="always"
        app:actionViewClass="android.support.v7.widget.SearchView"
        android:title="Search"
        android:icon="@drawable/ic_action_search"/>
</menu>

6 个答案:

答案 0 :(得分:1)

首先,您需要创建自己的菜单,然后在菜单项中放置自己的图标。

<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="com.bruce.example.search.SearchActivity">
<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    app:showAsAction="never" />
<item
    android:id="@+id/action_search"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    android:icon="@drawable/ic_search"
    app:showAsAction="always" />

然后只需覆盖onCreateOptionsMenu以扩充自定义菜单。

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_search, menu);
    return true;
}

如果您有任何问题,请告诉我

答案 1 :(得分:1)

将以下代码放入您的活动onCreateOptionsMenu()。

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_home, menu);
        MenuItem favoriteItem = menu.findItem(R.id.action_search);
        Drawable newIcon = (Drawable)favoriteItem.getIcon();
        newIcon.mutate().setColorFilter(Color.argb(255, 200, 200, 200), PorterDuff.Mode.SRC_IN);
        favoriteItem.setIcon(newIcon);
        return true;
    }

您可以在{Color.argb(255,200,200,200)}中提供自己的颜色

答案 2 :(得分:1)

我得到了答案:

<强> main_activity.xml

<android.support.v7.widget.Toolbar
    android:id="@+id/myToolbar"
    android:layout_height="?android:attr/actionBarSize"
    android:layout_width="match_parent"
    android:background="@color/colorPrimary"
    android:theme="@style/myToolbarTheme"/>

<强> style.xml

<style name="myToolbarTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorControlNormal">#fff</item>
</style>

答案 3 :(得分:0)

创建一个XML文件,您可以将其称为ic_action_search_white,使用色调来更改图标颜色。

<?xml version="1.0" encoding="utf-8"?>
<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/ic_action_search"
    android:tint="@color/white"/>

然后在菜单项集android:icon="@drawable/ic_action_search_white"中,drawable是刚刚创建的XML文件。或者,您可以通过编程方式设置图标yourSearchMenuItem.setIcon(R.drawable.ic_action_search_white)

让我知道它是否有效

答案 4 :(得分:0)

Change your AppTheme to below:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>

    <item name="android:buttonStyle">@style/MyButtonStyle</item>
</style>

Making it dark actionBar will solve your problem of menu icon color.

Update: Added windowNoTitle set as true.

答案 5 :(得分:0)

在声明searchView的地方添加以下方法。

public Drawable changeDrawableTint(Drawable drawable, int color){
    final Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
    DrawableCompat.setTintList(wrappedDrawable, ColorStateList
            .valueOf(color));
    return wrappedDrawable;
}

现在这两行你已经写过了

MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);

只需添加此行

即可
// To change the color of searchView
searchItem.setIcon(changeDrawableTint(searchItem.getIcon(), Color.WHITE));