1 创建drawable文件 all_radius.xml
圆角值为20dp,背景颜色为box,边框颜色为border,可以自己定义一个颜色
android:topLeftRadius="20dp"
android:topRightRadius="20dp"
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp">
2 创建对话框custom_dialog.xml
提示框的大小为300x200,
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
android:layout_width="300dp"
android:background="@drawable/all_radius"
android:layout_height="200dp">
3 Java代码弹出对话框
主界面添加一个按钮,并是现行点击事件,执行如下函数
protected void creatCustomDialog() {
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.show();
Window window = alertDialog.getWindow();
window.setContentView(R.layout.custom_dialog);
window.setWindowAnimations(R.style.dialog_style_scale);
}
4 效果图
真机测试达到效果,模拟器有矩形
5 为对话框添加动画
可以参考博客,包含6中动画: 博客地址
1 添加anim资源文件animate_alpha.xml
android:fromAlpha="0"
android:toAlpha="1"
android:duration = "1000">
2 添加anim资源文件animate_scale.xml
android:fromXScale="0"
android:toXScale="1"
android:fromYScale="0"
android:toYScale="1"
android:duration = "1000"
android:pivotX="50%"
android:pivotY="50%">
3 在values里的styles.xml文件中添加内容
@anim/animate_alpha
@anim/animate_scale
4 调用方法
添加一个就行了
//添加缩放效果
window.setWindowAnimations(R.style.dialog_style_scale);
//添加渐变效果
window.setWindowAnimations(R.style.dialog_style_alpha);
5 效果图
为了演示效果,只好在模拟器上运行了,真机运行不会有矩形框的,请见谅!