今天,我就直奔主题,动手撸一个自定义的dialog,而且点击按钮能够展示水波纹特效(能够兼容5.0以下的版本哦)的控件。记录下来,以便日后能在项目中直接使用!
首先看效果图:
可以看到,点击按钮,显示了一个自定义的dialog,该dialog的样式是目前十分流行的,各大app上几乎都在使用这种风格的dialog。而且,我给dialog的button添加了点击时的水波纹特效,尽管当前我使用的是android 6.0 版本,但为了兼容5.0以下的版本,我也对该效果做了兼容处理,代码其实很简单,不过要注意一些细节。
1,项目的结构:
由项目结构可以看到,代码并不多,主要是一些drawable文件的处理。
2,先看看我们的xml布局文件:
①self_dialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">
<LinearLayout
android:layout_width="260dp"
android:layout_height="140dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/self_dialog_bg"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:gravity="center"
android:text="提示"
android:textColor="#C71585"
android:textSize="25dp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="15dp"
android:background="#3000" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/no"
android:layout_width="0dp"