Android Lollipop RippleDrawable vs Selector for Pre-Lollipop

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24607339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:29:19  来源:igfitidea点击:

Lollipop RippleDrawable vs Selector for Pre-Lollipop

androidmaterial-designandroid-5.0-lollipopripplerippledrawable

提问by Neoh

I have buttons with different draw9patch pngas background. Currently the buttons are controlled by selectorwhich look something like this:

我有不同 draw9patchpng作为背景的按钮。目前,按钮由selector它控制,看起来像这样:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/pressed" android:state_pressed="true"/>
  <item android:drawable="@drawable/disabled" android:state_enabled="false"/>
  <item android:drawable="@drawable/focused" android:state_focused="true"/>
  <item android:drawable="@drawable/default"/>
</selector>

For the Android Lollipopthey have a RippleDrawablefor the touch effect, which goes something like this:

对于Android Lollipop,它们具有RippleDrawable触摸效果,如下所示:

<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="?android:colorControlHighlight">
    <item>
    ...
    </item>
</ripple>

Regarding the new touch ripple effect:

关于新的触摸涟漪效果:

1:Can I set draw9patch as background for RippleDrawable?

1:我可以将 draw9patch 设置为背景RippleDrawable吗?

2:How do I accomodate the above two different xml's I want to follow Material design? Do I have to fork out a new folder/layout xml for the new RippleDrawable?

2:如何适应以上两种不同的xml我想遵循Material design?我是否必须为新的文件夹/布局 xml 分叉RippleDrawable

回答by alanv

1) Yes. See the documentation for RippleDrawable for more details on how layers are composited, but basically you want:

1) 是的。有关如何合成图层的更多详细信息,请参阅 RippleDrawable 的文档,但基本上您需要:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:attr/colorControlHighlight">
    <item android:drawable="@drawable/yourninepatch" />
</ripple>

Or to also handle the disabled state in a clean way, you might want:

或者也以干净的方式处理禁用状态,您可能需要:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:attr/colorControlHighlight">
    <item>
        <selector>
            <item android:state_enabled="false">
                <nine-patch
                    android:src="@drawable/yourninepatch"
                    android:alpha="?android:attr/disabledAlpha" />
            </item>
            <item>
                <nine-patch android:src="@drawable/yourninepatch" />
            </item>
        </selector>
    </item>
</ripple>

2) Yes, you should place your ripple XML in drawable-v21.

2) 是的,您应该将您的涟漪 XML 放在 drawable-v21 中。