经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android自定义样式圆角dialog对话框
来源:jb51  时间:2021/11/15 17:22:15  对本文有异议

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

效果如上,圆角对话框,标题和正文都可以自己设定

做法:

1.在res文件的layout文件夹创建自己的对话框布局,命名为my_dialog.xml
2.在res文件的drawable文件夹创建自己的对话框样式(圆角),命名为my_dialog_shape.xml
3.写一个方法调用对话框布局,触发条件自定义,这里我是写了一个按钮,在按钮的点击事件里调用方法,弹出对话框。在这个方法里可以定义对话框的标题、正文、点击确定或取消时触发的事件等,还可以设定对话框在屏幕上的显示位置
4.在需要弹出对话框的地方调用方法

上代码:

1.在res文件的layout文件夹创建自己的对话框布局,命名为my_dialog.xml

对话框内部控件的显示位置都可以在这里自己调整

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content"
  6. android:layout_marginHorizontal="16dp">
  7.  
  8. <TextView
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:textSize="18sp"
  12. android:textColor="@color/black"
  13. android:textStyle="bold"
  14. android:layout_marginTop="14dp"
  15. android:gravity="center"
  16. android:layout_gravity="center"
  17. android:id="@+id/title"/>
  18. <TextView
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content"
  21. android:textSize="16sp"
  22. android:textColor="@color/black"
  23. android:layout_marginTop="16dp"
  24. android:layout_marginHorizontal="16dp"
  25. android:gravity="center"
  26. android:layout_gravity="center"
  27. android:id="@+id/message"/>
  28. <LinearLayout
  29. android:layout_width="match_parent"
  30. android:layout_height="wrap_content"
  31. android:layout_marginHorizontal="20dp"
  32. android:layout_marginTop="16dp">
  33. <Button
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:layout_weight="1"
  37. android:text="取消"
  38. android:textSize="16sp"
  39. android:textColor="@color/white"
  40. android:background="@null"
  41. android:layout_marginRight="14dp"
  42. android:id="@+id/btn_cancel"/>
  43. <Button
  44. android:layout_width="wrap_content"
  45. android:layout_height="wrap_content"
  46. android:layout_weight="1"
  47. android:text="确定"
  48. android:textSize="16sp"
  49. android:textColor="@color/white"
  50. android:id="@+id/btn_confirm"/>
  51. </LinearLayout>
  52.  
  53. </LinearLayout>

2.在res文件的drawable文件夹创建自己的对话框样式(圆角),命名为my_dialog_shape.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:shape="rectangle">
  5. <solid android:color="@color/white" />
  6. <corners android:radius="20dp"/>
  7. </shape>

3.写一个方法调用对话框布局,触发条件自定义,这里我是写了一个按钮,在按钮的点击事件里调用方法,弹出对话框。在这个方法里可以定义对话框的标题、正文、点击确定或取消时触发的事件等,还可以设定对话框在屏幕上的显示位置

  1. public void my_dialog(Context context) {
  2. View inflateLayout = LayoutInflater.from(context).inflate(R.layout.my_dialog,null);
  3. TextView unbind_title = (TextView) inflateLayout.findViewById(R.id.title);
  4. unbind_title.setText("标题");
  5. TextView unbind_message = (TextView) inflateLayout.findViewById(R.id.message);
  6. unbind_message.setText("正文");
  7. AlertDialog builderDialog = new AlertDialog.Builder(context)
  8. .setView(inflateLayout)
  9. .setCancelable(false) //使用户只能通过点击对话框的确定或取消关闭对话框
  10. .create();
  11.  
  12. inflateLayout.findViewById(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
  13. @Override
  14. public void onClick(View view) {
  15. Toast.makeText(context, "你点击了确定", Toast.LENGTH_SHORT).show();
  16. builderDialog.dismiss();
  17. }
  18. });
  19.  
  20. inflateLayout.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
  21. @Override
  22. public void onClick(View v) {
  23. Toast.makeText(context, "你点击了取消", Toast.LENGTH_SHORT).show();
  24. builderDialog.dismiss();
  25. }
  26. });
  27. builderDialog.getWindow().setBackgroundDrawableResource(R.drawable.my_dialog_shape); //设置对话框的样式
  28. WindowManager.LayoutParams params = builderDialog.getWindow().getAttributes();
  29. params.y = 1000;
  30. builderDialog.getWindow().setAttributes(params);
  31. builderDialog.show();
  32. builderDialog.getWindow().setGravity(Gravity.TOP); //设置对话框展示在距离屏幕顶部1000的位置
  33. }

4.在需要弹出对话框的地方调用方法

例如:我在MainActivity里点击了一下button,触发了弹出对话框的方法

  1. Button pops_up = (Button) findViewById(R.id.pops_up);
  2. pops_up.setOnClickListener(new View.OnClickListener() {
  3. @Override
  4. public void onClick(View view) {
  5. my_dialog(MainActivity.this);
  6. }
  7. });

代码完整,欢迎指正

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号