[英]How to set button background drawable image with ripple effect in android
My Xml code:我的 XML 代码:
<Button
android:id="@+id/link_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/google_btn" />
I am applying Default ripple effect我正在应用默认涟漪效应
<Button
android:id="@+id/link_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground" />
but I need button background "@drawable/google_btn"
with但我需要按钮背景
"@drawable/google_btn"
"?android:attr/selectableItemBackground"
. "?android:attr/selectableItemBackground"
。 it's means i need ripple effect with custom background.这意味着我需要自定义背景的涟漪效果。
In your drawable-v21
folder you can write code for ripple effect by your own.在
drawable-v21
文件夹中,您可以自己编写涟漪效应代码。 Make a drawable xml file and set starting tag by ripple
.制作一个可绘制的 xml 文件并通过
ripple
设置起始标签。 Like this :像这样:
<?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>
When the button has a background from the drawable, we can add ripple effect to the foreground parameter.. Check below code its working for my button with a different background当按钮具有可绘制的背景时,我们可以向前景参数添加涟漪效果..检查下面的代码是否适用于我的具有不同背景的按钮
<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"
/>
Add below parameter for ripple effect添加以下参数以获得涟漪效果
android:foreground="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:focusable="true"
<?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>
Try the above code here in drawables give your own custom drawable backgrounds as you need it.在 drawables 中尝试上面的代码,根据需要提供您自己的自定义可绘制背景。
Another option is to create a layer list drawable and set that as a background.另一种选择是创建一个可绘制的图层列表并将其设置为背景。 For example:
例如:
<?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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.