Lollipop RippleDrawable vs Pre-Lollipop的选择器

Neo*_*eoh 38 android material ripple rippledrawable android-5.0-lollipop

我有不同的draw9patch按钮png作为背景.目前按钮的控制方式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>
Run Code Online (Sandbox Code Playgroud)

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

<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="?android:colorControlHighlight">
    <item>
    ...
    </item>
</ripple>
Run Code Online (Sandbox Code Playgroud)

关于新的触摸波纹效果:

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

2:我如何容纳以上两种不同的xml我想遵循材料设计?我是否必须为新的文件夹/布局xml分叉RippleDrawable

ala*_*anv 78

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>
Run Code Online (Sandbox Code Playgroud)

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

<?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>
Run Code Online (Sandbox Code Playgroud)

2)是的,你应该将你的纹波XML放在drawable-v21中.

  • 不,你只想要一个涟漪元素.通常,您将其保留为默认的colorControlHighlight值,但如果您必须在按下并聚焦时必须具有不同的纹波颜色,则还可以将颜色设置为颜色状态列表.请记住,当项目启用,按下或聚焦时,您才会看到涟漪. (2认同)