如何在android中设置具有涟漪效果的按钮背景可绘制图像
我的Xml代码:如何在android中设置具有涟漪效果的按钮背景可绘制图像,android,background,android-drawable,ripple,Android,Background,Android Drawable,Ripple,我的Xml代码: <Button android:id="@+id/link_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/google_btn" /> 我正在应用默认涟漪效应 <Button android:id="@+id/link_btn" andro
<Button
android:id="@+id/link_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/google_btn" />
我正在应用默认涟漪效应
<Button
android:id="@+id/link_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground" />
但是我需要按钮背景“@drawable/google\u btn”
和“?android:attr/selectableItemBackground”
。这意味着我需要自定义背景的涟漪效果
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight" >
<item
android:id="@android:id/mask"
android:drawable="@drawable/btn_rectangle"/>
<item android:drawable="@drawable/btn_rect_states"/>
</ripple>
在drawables中尝试上面的代码,根据需要提供自己的自定义可绘制背景 在您的
drawable-v21
文件夹中,您可以自己编写ripple effect代码。制作一个可绘制的xml文件,并通过ripple
设置起始标记。像这样:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="@color/colorAccentDark">
<item>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@color/button_accent_dark"
android:state_checked="false"/>
<item
android:drawable="@color/button_accent"
android:state_checked="true" />
<item
android:drawable="@color/button_accent_dark" />
</selector>
</item>
</ripple>
当按钮具有可绘制的背景时,我们可以向前景参数添加涟漪效果。。检查下面的代码它的工作为我的按钮与不同的背景
<Button
android:layout_width="wrap_content"
android:layout_height="40dp"
android:gravity="center"
android:layout_centerHorizontal="true"
android:background="@drawable/shape_login_button"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:text="@string/action_button_login"
/>
另一个选项是创建可绘制的图层列表,并将其设置为背景。例如:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_background"/>
<item android:drawable="?attr/selectableItemBackground"/>
</layer-list>
我该怎么做呢值得强调的是(正如@zahidul提到的),连锁反应只适用于Android v21及以上版本。谢谢@zahidul Islam。。这是可行的,还有一个疑问是,我如何在v21以下应用涟漪效应这在v21以下很好涟漪不起作用,因此您需要为正常和按下状态@PrithivDharmaraj.创建按钮状态的选择器文件。。如果我的回答对你有帮助,请接受或投票。。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_background"/>
<item android:drawable="?attr/selectableItemBackground"/>
</layer-list>