选定的弹出菜单项背景颜色设置

时间:2021-05-27 13:49:22

标签: android xml android-studio kotlin popup

我只是想问有没有人可以更改所选菜单项的背景颜色?我在这个主题上搜索了很多,但我还没有找到任何可行的解决方案:(

所以我有一个带有 3dot imageview 的 recyclerview,当我单击此图像时,会显示一个弹出菜单。在我的列表中,我有项目并且所有项目都有一个状态,我使用这个弹出菜单来显示状态选项,但这些项目已经具有默认状态,我希望当弹出菜单出现时,manu 的默认项目具有不同的背景, 比其他人。

I found one solution, that is with radiobuttons, so when menu item is selected, i set the item checket, so with the radiobutton is workign well, but that's not that I really want :/

我尝试过样式,如下所示:

@style/PopupMenu

@颜色:黑色 @drawable/selector_popup_menu_background @drawable/selector_popup_menu_dropdown @drawable/selector_menu_item @color/light_blue

(对不起,我把代码贴在这里,但在我发布问题后,它没有很好地显示出来)

在可绘制文件中,我尝试了以下操作:

<item android:drawable="@color/light_blue" android:state_selected="true"/>
<item android:drawable="@color/light_blue" android:state_checked="true"/>
<item android:drawable="@color/transparent"/>

但对我来说似乎状态检查在背景中根本不起作用,只有单选按钮:(

我还在 popup.setOnMenuItemClickListener 中尝试了以下内容:

val view = popup.menu.findItem(item.itemId).actionView as TextView view.background = context.resources.getDrawable(R.drawable.custom_spinner_item_background_shape)

但是什么都没发生...

我也可以在单击项目时更改选择的突出显示颜色,但关闭菜单后此背景颜色消失。

有没有人有解决这个问题的可行解决方案?

1 个答案:

答案 0 :(得分:0)

名为 PopUp 的样式

 <selector>
    <?xml version="1.0" encoding="utf-8"?>
    <item android:drawable="@color/light_blue" android:state_selected="true"/>
    <item android:drawable="@color/light_blue" android:state_checked="true"/>
    <item android:drawable="@color/transparent"/>
  </selector>

申请android:background="@drawable/popup"

 <RadioButton
            android:id="@+id/rbID"
            android:text="check"
            android:background="@drawable/popup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:checked="false"
            />

输出ss

未选中

uncheck

已检查

checked

@style/PopupMenuandroid:background="@drawable/popup"

希望对你有帮助