本文实例为大家分享了Android创建自定义样式圆角dialog对话框的具体代码,供大家参考,具体内容如下

效果如上,圆角对话框,标题和正文都可以自己设定
做法:
1.在res文件的layout文件夹创建自己的对话框布局,命名为my_dialog.xml
2.在res文件的drawable文件夹创建自己的对话框样式(圆角),命名为my_dialog_shape.xml
3.写一个方法调用对话框布局,触发条件自定义,这里我是写了一个按钮,在按钮的点击事件里调用方法,弹出对话框。在这个方法里可以定义对话框的标题、正文、点击确定或取消时触发的事件等,还可以设定对话框在屏幕上的显示位置
4.在需要弹出对话框的地方调用方法
上代码:
1.在res文件的layout文件夹创建自己的对话框布局,命名为my_dialog.xml
对话框内部控件的显示位置都可以在这里自己调整
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginHorizontal="16dp">
-
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textSize="18sp"
- android:textColor="@color/black"
- android:textStyle="bold"
- android:layout_marginTop="14dp"
- android:gravity="center"
- android:layout_gravity="center"
- android:id="@+id/title"/>
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textSize="16sp"
- android:textColor="@color/black"
- android:layout_marginTop="16dp"
- android:layout_marginHorizontal="16dp"
- android:gravity="center"
- android:layout_gravity="center"
- android:id="@+id/message"/>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginHorizontal="20dp"
- android:layout_marginTop="16dp">
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="取消"
- android:textSize="16sp"
- android:textColor="@color/white"
- android:background="@null"
- android:layout_marginRight="14dp"
- android:id="@+id/btn_cancel"/>
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="确定"
- android:textSize="16sp"
- android:textColor="@color/white"
- android:id="@+id/btn_confirm"/>
- </LinearLayout>
-
- </LinearLayout>
2.在res文件的drawable文件夹创建自己的对话框样式(圆角),命名为my_dialog_shape.xml
- <?xml version="1.0" encoding="utf-8"?>
- <shape
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:shape="rectangle">
- <solid android:color="@color/white" />
- <corners android:radius="20dp"/>
- </shape>
3.写一个方法调用对话框布局,触发条件自定义,这里我是写了一个按钮,在按钮的点击事件里调用方法,弹出对话框。在这个方法里可以定义对话框的标题、正文、点击确定或取消时触发的事件等,还可以设定对话框在屏幕上的显示位置
- public void my_dialog(Context context) {
- View inflateLayout = LayoutInflater.from(context).inflate(R.layout.my_dialog,null);
- TextView unbind_title = (TextView) inflateLayout.findViewById(R.id.title);
- unbind_title.setText("标题");
- TextView unbind_message = (TextView) inflateLayout.findViewById(R.id.message);
- unbind_message.setText("正文");
- AlertDialog builderDialog = new AlertDialog.Builder(context)
- .setView(inflateLayout)
- .setCancelable(false) //使用户只能通过点击对话框的确定或取消关闭对话框
- .create();
-
- inflateLayout.findViewById(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Toast.makeText(context, "你点击了确定", Toast.LENGTH_SHORT).show();
- builderDialog.dismiss();
- }
- });
-
- inflateLayout.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast.makeText(context, "你点击了取消", Toast.LENGTH_SHORT).show();
- builderDialog.dismiss();
- }
- });
- builderDialog.getWindow().setBackgroundDrawableResource(R.drawable.my_dialog_shape); //设置对话框的样式
- WindowManager.LayoutParams params = builderDialog.getWindow().getAttributes();
- params.y = 1000;
- builderDialog.getWindow().setAttributes(params);
- builderDialog.show();
- builderDialog.getWindow().setGravity(Gravity.TOP); //设置对话框展示在距离屏幕顶部1000的位置
- }
4.在需要弹出对话框的地方调用方法
例如:我在MainActivity里点击了一下button,触发了弹出对话框的方法
- Button pops_up = (Button) findViewById(R.id.pops_up);
- pops_up.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- my_dialog(MainActivity.this);
- }
- });
代码完整,欢迎指正
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。