Android AlertDialog/AlertDialog.builder 以及自定义AlertDialog方法

未经本人授权,不得转载!否则必将维权到底

导语:

最近遇到了一个需要自定义AlertDialog的需求,一直对AlertDialog半知半解,花了点时间自己研究下,给大家分享下自己的研究心得把。
首先介绍下AlertDialog:AlertDialog也是Android系统当中最常用的对话框之一。 我们可以给AlertDialog对话框设置相应的信息。比如icon、title、message、setPositiveButton、setNegativeButton、setOnCancelListener等。

首先,我们先看下最简单运用系统布局创建的AlertDialog

图例1.png

代码如下:

/** * Created by KeithXiaoY on 2016/7/10. */
private void showOneDialog(String desc, final String downloadurl) {
    AlertDialog.Builder build = new AlertDialog.Builder(this);
    build.setTitle("提示");
    build.setMessage(desc);
    build.setOnCancelListener(new DialogInterface.OnCancelListener() {
      @Override    
      public void onCancel(DialogInterface dialog) {        
            loadMainActivity();    
      }
    });
    build.setPositiveButton("升级", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
               downloadApk(downloadurl);
               dialog.dismiss();
        }
      });
    build.setNegativeButton("取消", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {        
              dialog.dismiss();        
              loadMainActivity();    
        }
      });
    build.show();

这样就能实现了上面简单的需求。但是大家也肯定发现了,title和message都不居中,底下的两个button也没有按比例左右对齐,这样是很影响美观的。如果我们想实现一个title和message居中,button也对齐的效果,亦或是其他任何你想要的效果,那我们就需要自定义布局了。


在用自定义AlertDialog实现上述效果之前,不知道大家注意到没有,AlertDialog是无法通过其本身的构造函数来到的AlertDialog的实例的,必须借助AlertDialog.Builder!

通过查看AlertDialog的源码发现三个构造函数都是protected,所以不能用AlertDialog alertDialog = new AlertDialog();来得到。源码里面的注解@NonNull 是这里的参数不能为空。

图例2.png

我们再来看下AlertDialog的builder的源码,发现Builder是用public修饰的:

图例3.png

到这里大家应该明白,为什么我们需要用下面的代码来创建AlertDialog来创建它的实例了。

 AlertDialog.Builder build = new AlertDialog.Builder(this);

下面我们来讲下自定义AlertDialog的布局来实现我们自己的需求。

先看效果图:
图例4.png
代码实现:

/** * Created by KeithXiaoY on 2016/7/10. */
private void showOneDialog(String desc, final String downloadurl) {
    final AlertDialog build = new AlertDialog.Builder(this).create();
    //自定义布局
    View view = getLayoutInflater().inflate(R.layout.splash_dialog, null);
    //把自定义的布局设置到dialog中,注意,布局设置一定要在show之前。从第二个参数分别填充内容与边框之间左、上、右、下、的像素
    build.setView(view, 0, 0, 0, 0);
    //一定要先show出来再设置dialog的参数,不然就不会改变dialog的大小了
    build.show();
    //得到当前显示设备的宽度,单位是像素
    int width = getWindowManager().getDefaultDisplay().getWidth();
    //得到这个dialog界面的参数对象
    WindowManager.LayoutParams params = build.getWindow().getAttributes();
    //设置dialog的界面宽度
    params.width = width-(width/6);
    //设置dialog高度为包裹内容
    params.height =  WindowManager.LayoutParams.WRAP_CONTENT;
    //设置dialog的重心
    params.gravity = Gravity.CENTER;
    //dialog.getWindow().setLayout(width-(width/6), LayoutParams.WRAP_CONTENT);
    //用这个方法设置dialog大小也可以,但是这个方法不能设置重心之类的参数,推荐用Attributes设置
    //最后把这个参数对象设置进去,即与dialog绑定
    build.getWindow().setAttributes(params);
    Button leftButton = (Button) view.findViewById(R.id.splash_dialog_left);
    Button rightButton = (Button) view.findViewById(R.id.splash_dialog_right);
    TextView warnMessage = (TextView) view.findViewById(R.id.warnmessage);
    warnMessage.setText(desc);
    leftButton.setOnClickListener(new View.OnClickListener() {    
          @Override
          public void onClick(View v) {    
                downloadApk(downloadurl);    
                build.dismiss();
          }
     });
    rightButton.setOnClickListener(new View.OnClickListener() {    
          @Override    
          public void onClick(View v) {        
                build.dismiss();        
                loadMainActivity();
          }
     });

下面是xml布局文件代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
   xmlns:tools="http://schemas.android.com/tools"    
   android:layout_width="match_parent"    
   android:layout_height="match_parent"    
   android:orientation="vertical">
    <TextView    
      android:layout_width="wrap_content"    
      android:layout_height="wrap_content"    
      android:layout_gravity="center_horizontal"    
      android:layout_marginTop="15dp"    
      android:text="提示"    
      android:textColor="@android:color/black"    
      android:textSize="25sp" />
    <TextView    
       android:id="@+id/warnmessage"    
       android:layout_width="wrap_content"    
       android:layout_height="wrap_content"    
       android:layout_gravity="center_horizontal"    
       android:layout_marginTop="15dp"    
       android:textColor="#434343"    
       android:textSize="20sp"    
       tools:text="提醒信息" />
    <LinearLayout    
      android:layout_width="match_parent"    
      android:layout_height="wrap_content"    
      android:layout_margin="15dp"    
      android:orientation="horizontal">
      <Button    
         android:id="@+id/splash_dialog_left"    
         style="@android:style/Theme.Dialog"    
         android:layout_width="0dp"    
         android:layout_height="wrap_content"    
         android:layout_weight="1"    
         android:background="@null"    
         android:text="升级"    
         android:textColor="#45818e" />
      <Button    
         android:id="@+id/splash_dialog_right"    
         style="@android:style/Theme.Dialog"    
         android:layout_width="0dp"    
         android:layout_height="wrap_content"    
         android:layout_toRightOf="@+id/splash_dialog_left"    
         android:layout_weight="1"    
         android:background="@null"    
         android:text="取消"    
         android:textColor="#45818e" />

    </LinearLayout>
</LinearLayout>

结束语:

这样我们就通过自定义布局,实现了我们自己想要的效果。自定义布局可以实现各种各样的效果,这里就不再向大家阐述了,有兴趣的可以自己写个Demo试试。相信大家对AlertDialog和AlertDialog.builder有了些认识。学无止境,一起努力吧。


本文原创发布于微信公众号「keithxiaoy」,编程、思维、成长、正能量,关注并回复「编程」、「阅读」、「Java」、「Python」等关键字获取免费学习资料

不要给自己的人生设限

猜你喜欢

转载自blog.csdn.net/xiaoy_yan/article/details/80965717
今日推荐