• Android 弹出自定义对话框


    Android在任意Activity界面弹出一个自定义的对话框,效果如下图所示:

    准备一张小图片,右上角的小X图标64*64,close_icon.png,随便找个小图片代替;

    第一步:样式添加,注意:默认在values->thems下,如果版本较高,请至values->style.xml内定义,将以下代码添加在之前

    1. <style name="CustomDialog" parent="android:style/Theme.Dialog">
    2. <item name="android:windowBackground">@android:color/transparentitem>
    3. <item name="android:windowNoTitle">trueitem>
    4. <item name="android:windowFrame">@nullitem>
    5. <item name="android:windowIsFloating">trueitem>
    6. <item name="android:backgroundDimEnabled">trueitem>
    7. style>
    8. <style name="mydialog" parent="android:style/Theme.Dialog">
    9. <item name="android:windowBackground">@android:color/transparentitem>
    10. <item name="android:windowNoTitle">trueitem>
    11. <item name="android:backgroundDimEnabled">trueitem>
    12. style>

    第二步:专门为它创建两个类:DialogView + DialogManager  

    1. //DialogView.java
    2. package com.example....//my package
    3. import android.app.Dialog;
    4. import android.content.Context;
    5. import android.view.Window;
    6. import androidx.annotation.NonNull;
    7. public class DialogView extends Dialog {
    8. public DialogView(@NonNull Context context, int layout, int style, int gravity) {
    9. super(context, style);
    10. setContentView(layout);
    11. Window mWindow = getWindow();
    12. }
    13. }
    1. //DialogManager.java
    2. package com.example....//my package
    3. import android.content.Context;
    4. import android.view.Gravity;
    5. public class DialogManager {
    6. private static volatile DialogManager mInstance = null;
    7. private DialogManager() { }
    8. public static DialogManager getInstance() {
    9. if (mInstance == null) {
    10. synchronized (DialogManager.class) {
    11. if (mInstance == null) {
    12. mInstance = new DialogManager();
    13. }
    14. }
    15. }
    16. return mInstance;
    17. }
    18. public DialogView initView(Context context, int layout) {
    19. return new DialogView(context,layout, R.style.CustomDialog, Gravity.CENTER);
    20. }
    21. public DialogView initView(Context context,int layout,int gravity) {
    22. return new DialogView(context,layout, R.style.mydialog, gravity);
    23. }
    24. public void show(DialogView view) {//Show
    25. if (view != null) {
    26. if (!view.isShowing()) {
    27. view.show();
    28. }
    29. }
    30. }
    31. public void hide(DialogView view) {//Hide
    32. if (view != null) {
    33. if (view.isShowing()) {
    34. view.dismiss();
    35. }
    36. }
    37. }
    38. }

    第三步:给它创建样式布局xml   my_dlg_layout.xml

    1. "1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. android:layout_width="match_parent"
    4. android:background="@color/white"
    5. android:layout_height="match_parent">
    6. <LinearLayout
    7. android:layout_width="match_parent"
    8. android:orientation="vertical"
    9. android:layout_marginTop="5dp"
    10. android:layout_height="wrap_content">
    11. <RelativeLayout
    12. android:layout_width="match_parent"
    13. android:orientation="horizontal"
    14. android:layout_height="34dp">
    15. <RelativeLayout
    16. android:layout_width="match_parent"
    17. android:layout_height="match_parent"
    18. android:gravity="center"
    19. android:orientation="horizontal">
    20. <TextView
    21. android:layout_width="wrap_content"
    22. android:layout_height="25dp"
    23. android:text="MyDialog"
    24. android:textColor="@color/red"
    25. android:textSize="16sp"
    26. android:textStyle="bold" />
    27. RelativeLayout>
    28. <RelativeLayout
    29. android:layout_alignParentRight="true"
    30. android:layout_width="wrap_content"
    31. android:orientation="horizontal"
    32. android:layout_gravity="right"
    33. android:layout_marginRight="10dp"
    34. android:layout_height="wrap_content">
    35. <ImageView
    36. android:id="@+id/btn_cancel"
    37. android:layout_width="25dp"
    38. android:src="@drawable/close_icon"
    39. android:layout_margin="5dp"
    40. android:layout_height="25dp"/>
    41. RelativeLayout>
    42. RelativeLayout>
    43. <View
    44. android:layout_width="match_parent"
    45. android:background="@color/gray"
    46. android:layout_height="1dp"/>
    47. <LinearLayout
    48. android:layout_width="match_parent"
    49. android:layout_height="wrap_content"
    50. android:orientation="vertical">
    51. <EditText
    52. android:layout_width="match_parent"
    53. android:layout_height="wrap_content"
    54. android:layout_margin="10dp"
    55. android:hint="名称:华山一区..."
    56. android:textSize="12sp"
    57. >EditText>
    58. <EditText
    59. android:layout_width="match_parent"
    60. android:layout_height="wrap_content"
    61. android:layout_margin="10dp"
    62. android:textSize="12sp"
    63. android:hint="备注..."
    64. >EditText>
    65. <Button
    66. android:layout_width="match_parent"
    67. android:layout_height="wrap_content"
    68. android:id="@+id/MY_Test_Add"
    69. android:background="@color/red"
    70. android:textColor="@color/white"
    71. android:layout_margin="10dp"
    72. android:paddingTop="10dp"
    73. android:paddingBottom="10dp"
    74. android:text="添加">Button>
    75. LinearLayout>
    76. LinearLayout>
    77. LinearLayout>

    //这里用到了刚才提到的close_icon,随便替换为你的一个小图标

    第四步:优化-圆角(可有可无)

    1. "1.0" encoding="utf-8"?>
    2. <shape xmlns:android="http://schemas.android.com/apk/res/android">
    3. <corners android:radius="10dp"/>
    4. <solid android:color="#FFEEEE" />
    5. shape>

    //注意文件路径res/drawable/shapes.xml,添加进去别和你的东西冲突了,注意着点,边框颜色随便调整

    第五步:已经完成了,分两步显示它:初始化+显示

    1. import android.view.Gravity;//needed
    2. //myActivity(){.....
    3. private DialogView mDlgView;//公共变量
    4. private ImageView btnCancel;//公共变量
    5. //protected void onCreate(Bundle savedInstanceState) {//my onCreate
    6. //super.onCreate(savedInstanceState);
    7. //setContentView(R.layout.activity_main);
    8. mDlgView= DialogManager.getInstance().initView(this, R.layout.my_dlg_layout, Gravity.BOTTOM);//这里要注意,这个对话框的View要单独绑定自己的布局
    9. mDlgView.setCanceledOnTouchOutside(false);//这是设置区域外点击是否取消显示
    10. btnCancel = mDlgView.findViewById(R.id.btn_cancel);//注意这个关闭图片X,在对话框布局里了,而不是在当前页面布局,不可用this.findViewBy...
    11. btnCancel.setOnClickListener(new OnClickListener() {//给返回按纽添加点击隐藏事件
    12. @Override
    13. public void onClick(View view) {
    14. DialogManager.getInstance().hide(mDlgView);
    15. }
    16. });

    初始化完毕,在需要的地方进行调用,比如你的按钮被点击了,直接在里调用这一句即可;

    DialogManager.getInstance().show(mDlgView);

    更多操作提示:

    1. //mDlgView.dismiss(); //取消
    2. //mDlgView.setCanceledOnTouchOutside(true);//允许区域外点击关闭
    3. //mDlgView.setCanceledOnTouchOutside(false);//禁止区域外点击关闭
    4. //每次显示的时候其实应该清空Edittext里面的内容,返回关闭X的图标的ID都能绑定了,相同的方法上面的任何子控件绑定都是小菜一碟,给个ID,用mDialogView.findViewById(R.....)就出来了
    5. //my_dlg_layout.xml 样式随便调 padding是内部边距,margin是外边距
    6. //那一根线条的颜色也是可调的,高度为1的View,android:background="@color/gray",你甚至可以改为:android:background="#AAAAAA"
    7. 举一反三,祝你成功!

  • 相关阅读:
    openssl-1.0.2k版本升级openssl-1.1.1p
    企业信息化安全方案设计实战参考
    【 C++ 】list的模拟实现
    最优化方法——Matlab实现黄金分割法一维搜索
    如何使用搜索功能精确筛选数据?
    笔试强训48天——day23
    微信小程序 | 微信公众平台SpringBoot开发实例 │ 模板消息的应用开发
    XMl发展前景及相关领域
    《合成孔径雷达成像算法与实现》Figure5.3
    Java多线程(5):CAS
  • 原文地址:https://blog.csdn.net/franzhong/article/details/134485075