一. AlertDialog
对话提示框,通常是在某项操作触发下显示的控件,需要在代码中调用show方法进行展现。AlertDialog的构造方法是protected,所以不能直接创建,有以下两种方式来创建:
创建方式
- 通过AlertDialog内部类Build来实现操作
//参数为该dialog要显示在哪个上下文中的对象
AlertDialog.Bulider builder = new AlertDialog.Build(this);
build.setTitle("提示");
build.setMessage("你是帅哥还是大帅哥?");
build.setPositiveButton("帅哥", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
);
builder.setNegativeButton("大帅哥",null);
builder.show();
- 通过Build来创建AlertDialog对象,但是自己进行控件设置
AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.setTitle("提示");
dialog.setMessage("您确定退出程序吗?");
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish()
}
})
dialog.show();
自定义Dialog
- 设置布局
dialog_demo.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="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:background="@mipmap/dialog_bg"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="您确定要退出吗?"
android:textFontWeight="2"
android:textSize="36sp"
android:textColor="#FF3300"
android:textStyle="bold"
android:layout_gravity="center"
android:layout_marginTop="256dp"
android:layout_marginBottom="20dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center"
>
<Button
android<

最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



