准备工作
1.调用参数
<!--颜色-->
<color name="transparent">#00000000</color>
<color name="black">#000000</color>
<color name="white">#FFFFFF</color>
<!--字体大小-->
<dimen name="text_size_16sp">16sp</dimen>
<!--文字-->
<string name="close_after__seconds">%s秒后关闭</string>
<!--样式-->
<style name="sp_dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item> <!-- 边框 -->
<item name="android:windowIsFloating">true</item> <!-- 是否浮现在activity之上 -->
<item name="android:windowIsTranslucent">false</item> <!-- 半透明 -->
<item name="android:windowNoTitle">true</item> <!-- 无标题 -->
<item name="android:windowBackground">@android:color/transparent</item> <!-- 背景透明 -->
<item name="android:backgroundDimEnabled">true</item> <!-- 模糊 -->
</style>
2.背景
dialog_common_bg
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="20dp" />
<solid android:color="@color/white" />
<stroke
android:width="1dp"
android:color="@color/white" />
</shape>
3.图片
主代码
public class WaitTimeDialog extends Dialog {
public WaitTimeDialog(@NonNull Context context, int themeResId) {
super(context, themeResId);
}
public static void create(Context context, int millisInFuture, int countDownInterval) {
WaitTimeDialog dialog = new WaitTimeDialog(context, R.style.sp_dialog);
dialog.setContentView(R.layout.dialog_wait_time);
dialog.setCancelable(false);
dialog.show();
TextView tip = dialog.findViewById(R.id.tip);
tip.setText(String.format(context.getString(R.string.close_after__seconds), millisInFuture + ""));
CountDownTimer countDownTimer = new CountDownTimer((millisInFuture + countDownInterval) * 1000, countDownInterval * 1000) {
@Override
public void onTick(long millisUntilFinished) {
tip.setText(String.format(context.getString(R.string.close_after__seconds), millisUntilFinished / 1000 + ""));
}
@Override
public void onFinish() {
dialog.dismiss();
}
};
countDownTimer.start();
}
}
dialog_wait_time
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@color/transparent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/dialog_common_bg"
android:orientation="vertical">
<ImageView
android:id="@+id/img"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:src="@drawable/icon_confirm" />
<TextView
android:id="@+id/tip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/img"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:textColor="@color/black"
android:textSize="@dimen/text_size_16sp" />
</RelativeLayout>
</LinearLayout>