替换导航查看项目的波纹效果 [英] Replace Navigation View Item Ripple Effect

查看:88
本文介绍了替换导航查看项目的波纹效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试替换我认为是NavigationView的默认项目背景的内容.因此,我创建了一个可绘制的背景来替换背景,并使用

时,选择的项目时,项目可绘制背景看起来大(选择控制"是下面的图像中所选择的项目).但是,当单击该项目时(下面的进度指示器"),似乎同时使用了默认的涟漪可绘制对象 background_navigation_view_item.xml可绘制对象(仅应显示一个圆角矩形,而不是矩形)包围它.)

我也将可绘制对象作为可检查视图的背景进行了测试,它以我期望的方式工作.因此,如果我的drawable代码不是错误,为什么不替换原始背景drawable?

解决方案

我已经在此处回答了这个问题>

注意: 这段代码只是消除了背景默认波纹的不透明性

无论如何,我会在这里再次回答.

假设您的NavigationView代码为:

<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/your_menu"
app:itemBackground="@drawable/background_navigation_view_item.xml"/>

我要做的是,向NavigationView中添加如下主题属性:

android:theme="@style/NavigationItemNoRipple"

styles.xml中:

<style name="NavigationItemNoRipple">
    <item name="android:colorControlHighlight">@android:color/transparent</item>
</style>

I am attempting to replace what I thought was the default item background of the NavigationView. So I created a drawable to replace the background and applied it using the itemBackground attribute.

background_navigation_view_item.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/navigation_item_background_tint">
    <item android:id="@android:id/mask" android:top="4dp" android:bottom="4dp" android:left="8dp" android:right="8dp">
        <shape android:shape="rectangle">
            <solid android:color="@android:color/white"/>
            <corners android:radius="3dp"/>
        </shape>
    </item>
    <item android:top="4dp" android:bottom="4dp" android:left="8dp" android:right="8dp">
        <selector>
            <item android:state_checked="true">
                <shape android:shape="rectangle">
                    <solid android:color="@color/navigation_item_background_tint"/>
                    <corners android:radius="3dp"/>
                </shape>
            </item>
            <item>
                <shape android:shape="rectangle">
                    <solid android:color="@android:color/transparent"/>
                    <corners android:radius="3dp"/>
                </shape>
            </item>
        </selector>
    </item>
</ripple>

The item background drawable looks great when the item is selected ("Selection Controls" is the selected item in the image below). But when the item is being clicked ("Progress Indicators" below) it appears that both the default ripple drawable and the background_navigation_view_item.xml drawable are being used (only a rounded rectangle should be shown, not the rectangle surrounding it).

I also tested the drawable as the background of a checkable view, and it works the way I would expect. So if it isn't an error in the code for my drawable, why isn't the original background drawable being replaced?

解决方案

I have already answered this question here

Note: This code just removes the opacity of background default ripple

Anyways, I'll answer it again here.

Assuming your NavigationView code is:

<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/your_menu"
app:itemBackground="@drawable/background_navigation_view_item.xml"/>

What I've done is, added a theme attribute as follows to the NavigationView:

android:theme="@style/NavigationItemNoRipple"

In styles.xml:

<style name="NavigationItemNoRipple">
    <item name="android:colorControlHighlight">@android:color/transparent</item>
</style>

这篇关于替换导航查看项目的波纹效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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