简体   繁体   English

Android:如何更改工具栏中菜单项的背景

[英]Android: How to change the background of the menu item in the toolbar

You need to change the background of the ~ showAsAction = always element as in the example.您需要像示例中一样更改 ~ showAsAction = always元素的背景。

在此处输入图像描述

I tried to make custom xml through a layer-list with a background and an icon, but as a result, the background was only inside the icon, and not around the entire perimeter of the item in the menu.我尝试通过带有背景和图标的图层列表来自定义 xml,但结果是,背景仅位于图标内部,而不是菜单中项目的整个周边。 Also tried android(app):background , android(app):itemBackground , but it`s dont work还尝试了android(app):background , android(app):itemBackground ,但它不起作用

My menu xml我的菜单 xml

<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"
    android:layout_width="wrap_content"
    android:icon="@drawable/ic_search"
    android:title="Search"
    app:actionViewClass="androidx.appcompat.widget.SearchView"
    app:showAsAction="collapseActionView|ifRoom" />
<item
    android:id="@+id/action_filter"
    android:layout_width="wrap_content"
    android:icon="@drawable/ic_slider"
    app:iconTint="@color/green"
    android:iconTint="@color/green"
    android:title="filters"
    app:showAsAction="collapseActionView|ifRoom"
    />

My adding menu in fragment我在片段中添加菜单

val menuHost: MenuHost = binding.searchAnimeToolbar
    menuHost.addMenuProvider(object : MenuProvider {
        override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
            menuInflater.inflate(R.menu.menu, menu)
            val menuFilterButton = menu.findItem(R.id.action_filter)


        }

        override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
            if(menuItem.itemId == R.id.action_filter) {
                Toast.makeText(context, "coming soon", Toast.LENGTH_LONG).show()
            }

            return true
        }
    }, viewLifecycleOwner)

Update: This method with the addition of a separate drawable is not suitable, because it is necessary to set the background for the menu item, and not for the icon.更新:这种添加单独drawable的方法不适合,因为需要为菜单项设置背景,而不是图标。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <layer-list>
        <item>
            <shape android:shape="rectangle">
                <solid android:color="@color/opacity_green"/>
                <corners android:radius="2dp"/>
            </shape>
        </item>
        <item android:drawable="@drawable/ic_slider">
        </item>
    </layer-list>
</item>
</selector>

First create a drawable for your menu item.首先为您的菜单项创建一个可绘制对象。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/your_drawable" />
    <item android:state_selected="true" android:drawable="@drawable/your_drawable" />
    <item android:state_focused="true" android:drawable="@drawable/your_drawable" />
    <item android:drawable="@drawable/your_drawable" />
</selector>

Then put it as icon for Menu Item.然后把它作为菜单项的图标。

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    <item android:id="@+id/itemTodo"
        android:icon="@drawable/item_selector"
        android:title="TODO"
        app:showAsAction="always"/>
</menu>

Hope it helps you.希望对你有帮助。 Happy coding!编码愉快!

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

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