一定要实现onclick事件,才能看到效果,否则无效!
点击前
点击中
点击中
最终效果是,水波纹延伸至按钮边界,看看详细编码吧。。。
- colors.xml:
<!--红底 点击时水波纹颜色-->
<color name="color_dd4646">#dd4646</color>
<color name="colorPress_dd4646">#dd4646</color>
<color name="color_ff5151">#ff5151</color>
<!--白底 点击时水波纹颜色-->
<color name="colorRipple">#e0e0e0</color>
- drawable下的btn_bg.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/colorPress_dd4646" android:state_pressed="true" />
<item android:drawable="@color/color_dd4646" />
</selector>
- drawable-v21下的btn_bg.xml:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/colorRipple">
<item>
<shape android:shape="rectangle">
<solid android:color="@color/color_ff5151" />
</shape>
</item>
</ripple>
- activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal|center_vertical">
<Button
android:id="@+id/ll_btn"
android:layout_width="300dp"
android:layout_height="150dp"
android:background="@drawable/btn_bg"
android:text="点击我出现水波纹效果"
android:padding="10dp" />
</LinearLayout>
- MainActivity.java实现点击事件:
Button llb=findViewById(R.id.ll_btn);
llb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"123",Toast.LENGTH_LONG).show();
}
});
代码量不多,都在这里了~~效果杠杆的!
网友评论