android 自定义 Dialog

在项目开发中经常用到 自定义 Dialog ,

      

先说一下实现过程:

    android 原生提供了  Dialog 、AlertDialog 、FragmentDialog ,官方推荐使用 FragmentDialg. 由于在本人项目中用到的这个

Dialog 比较简单,所以就了最为简单的 Dialog 方式去实现了,

   Dialog 类提供了一个方法 setContentView(View view);

  里面的参数 view 也就是所对应的 布局文件, view 的加载用 LayoutInflater 加载器将其加载进来。

  如果不想显示 默认的 title ,则在 setContentView(View view)方法前使用 requestWindowFeature(Window.FEATURE_NO_TITLE);


上代码了。

Dialog 所对应的布局文件

<?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:background="@drawable/shape_while_top_circular_rect"
              android:orientation="vertical">

  <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="65dp"
    >

    <TextView
      android:id="@+id/dxy_custom_dialog_title"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:layout_centerHorizontal="true"
      android:layout_marginBottom="15dp"
      android:gravity="center_horizontal"
      android:text="软件升级提醒!"
      android:textSize="18sp"
      />

  </RelativeLayout>

  <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
      android:id="@+id/dxy_custom_dialog_describe"
      android:text="检测到软件可能升级到体验更好版本,我准备好了,是否升级呢?"
      android:textSize="16sp"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:paddingTop="10dp"
      android:paddingLeft="30dp"
      android:paddingRight="30dp"
      android:paddingBottom="20dp"
      />

  </RelativeLayout>

  <View
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:background="#d8d4d3"
    />

<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/dxy_custom_dialog_cancel_down" android:layout_width="0dp" android:layout_height="60dp" android:layout_weight="1" android:text="取消" android:textSize="20sp" /> <View android:id="@+id/dxy_custom_dialog_vertical_line" android:layout_width="2dp" android:layout_height="60dp" android:background="#d8d4d3" /> <Button android:id="@+id/dxy_custom_dialog_confirm_down" android:layout_width="0dp" android:layout_height="60dp" android:layout_weight="1" android:text="确定" android:textSize="20sp" /> </LinearLayout> </LinearLayout>

Activity 中的代码 

private void initDialog(boolean dowload, String urls) {
  LayoutInflater inflater = this.getLayoutInflater();
  View dialogView = inflater.inflate(R.layout.dxy_custom_alter_dialog, null);   // 自定义的 Dialog 布局界面

  TextView customDialogTitle;                        // 升级的标题提示
  TextView customDialogDescribe;                     // 升级的描术符
  Button   customDialogCancelDown;                   // 取消升级
  Button   customDialogConfirmDown;                  // 确定升级
  View     customDialogVerticalLine;                 // 此处的view 是显示在 “取消” 与 “确定按扭”之间的竖线

  final Dialog dxyDialog = new Dialog(MainActivity.this);

  customDialogTitle = (TextView)dialogView.findViewById(R.id.dxy_custom_dialog_title);
  customDialogDescribe = (TextView)dialogView.findViewById(R.id.dxy_custom_dialog_describe);
  customDialogCancelDown = (Button)dialogView.findViewById(R.id.dxy_custom_dialog_cancel_down);
  customDialogConfirmDown = (Button)dialogView.findViewById(R.id.dxy_custom_dialog_confirm_down);
  customDialogVerticalLine = (View)dialogView.findViewById(R.id.dxy_custom_dialog_vertical_line);

  dxyDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
  dxyDialog.setContentView(dialogView);
  dxyDialog.setCanceledOnTouchOutside(false);
  dxyDialog.setCancelable(false);
  customDialogTitle.setText(R.string.soft_update);
  if(dowload){    // 一定得下载,否则不能使用软件
    customDialogCancelDown.setVisibility(View.GONE);
    customDialogVerticalLine.setVisibility(View.GONE);
    dxyDialog.show();
  }else{          // 在这里做判断,显示 Dialog 还是不显示 Dialog
    dxyDialog.show();
  }
  customDialogCancelDown.setOnClickListener(v -> {
    if(dxyDialog.isShowing()){
      dxyDialog.dismiss();
    }
  });
  customDialogConfirmDown.setOnClickListener(v ->{
    if(dxyDialog.isShowing()){
       dxyDialog.dismiss();
      new DownloadApk(urls,updateProgressHandle);
      dxyDialog.dismiss();
      alertDownLoadDialog();
    }
  });


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值