使用具有涟漪效应的 android:itemBackground [英] Using android:itemBackground with a ripple effect

查看:39
本文介绍了使用具有涟漪效应的 android:itemBackground的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 android:itemBackground XML 属性来更改我的溢出菜单(三点菜单)中项目的背景颜色.我的styles.xml 如下所示:

I'm using the android:itemBackground XML attribute to change the background color of the items in my overflow menu (three dots menu). My styles.xml looks as follows:

<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="android:itemBackground">@color/colorItemBg</item>
</style>

这有效,但会使物品上的涟漪效应完全消失.这种效果对我的用户体验至关重要.我尝试将 itemBackground 设置为 @drawable/custom_background,这是我定义的 XML 可绘制对象:

This works, but makes the ripple effect on the items completely disappear. This effect is critical for my user experience. I tried setting the itemBackground to @drawable/custom_background, which is an XML drawable I defined like so:

<?xml version="1.0" encoding="utf-8" ?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/ripple_material_dark">
  <item android:id="@android:id/mask"
    android:drawable="@color/colorItemBg" />
</ripple>

但这完全忽略了我的自定义背景并使用默认背景.我还尝试了这个问题的所有答案无济于事.

But this ignores my custom background altogether and uses the default one. I also tried all answers to this question to no avail.

如何更改菜单项的背景颜色并保持涟漪效果?

How can I change the background color of my menu items and keep the ripple effect?

推荐答案

您可以尝试设置 actionOverflowMenuStyle 如下:

You could try to set actionOverflowMenuStyle like 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="actionOverflowMenuStyle">@style/CMOptionsMenu</item>
</style>


<style name="CMOptionsMenu" parent="Widget.AppCompat.PopupMenu.Overflow">
   <item name="android:popupBackground">@color/colorItemBg</item>
</style>

这篇关于使用具有涟漪效应的 android:itemBackground的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆