我有一个安卓按钮,我想给它一个背景颜色和波纹效果
我的波纹 xml 如下。
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="#BDA0CB"
tools:targetApi="lollipop">
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#BDA0CB" />
</shape>
</item>
</ripple>
我的按钮是
<Button android:id="@+id/Button_activity_login_ViewProfile" style="?android:textAppearanceSmall"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginTop="16dp" android:text="Save"
android:textStyle="bold"
android:layout_marginLeft="@dimen/margin_left"
android:layout_marginRight="@dimen/margin_right"
android:textColor="#ffffffff"
android:fontFamily="sans-serif"
android:background="#ff7e51c2" />
为了产生涟漪效果,我必须删除背景颜色...有没有办法可以同时保留这两种颜色。
最佳答案
这可以通过在 ripple.xml 中添加 android:drawable 来实现。
首先将其添加到您的 color.xml 中。假设 #ff7e51c2 是您想要的按钮背景色。
...
<color name="btn_bg_color">#ff7e51c2</color>
....
然后为按钮背景创建一个drawable(/drawable/btn_bg.xml):
<?xml version="1.0" encoding="utf-8"?><shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="2dp" />
<solid
android:color="@color/btn_bg_color" />
</shape>
然后在你的 ripple.xml 中使用 drawable:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<item android:drawable="@drawable/btn_bg"/>
</ripple>
关于具有背景颜色和波纹效果的 Android 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37203335/