自定义Dialog

自定义AlertDialog

我们都知道Android自定的AlertDialog看起来比较丑,有时候我们需要自定义成自己需要的AlertDialog
我总结了有两种方法,话不多说,直接上代码

  1. 方式一
        AlterDialog dialog = new AlertDialog.Builder(MainActivity.this).create();
        dialog.show();
        dialog.setCancelable(false);//设置对话框之外点击无效
        //获取window对象
        Window window = dialog.getWindow();
        //给Window对象设置自定义样式
        window.setContentView(R.layout.dialog_update_version);
        //获取屏幕宽度
        int width = UIUtils.getScreenWidth(this);
        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        //设置宽度为屏幕的5/6,高包裹内容
        params.width = width-(width/6);
        params.height = LayoutParams.WRAP_CONTENT;
        //中心位置
        params.gravity = Gravity.CENTER;
        dialog.getWindow().setAttributes(params);

        TextView tvDialogOk = (TextView) window.findViewById(R.id.tv_dialog_ok);
        TextView tvDialogCancle = (TextView) window.findViewById(R.id.tv_dialog_cancle);
        tvDialogCancle.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                //对话框消失
                dialog.dismiss();
            }
        });

        tvDialogOk.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //执行更新操作
            }
        });

接下来看一下布局文件dialog_update_version.xml

     
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#70C555" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:padding="10dp"
            android:text="提示"
            android:textColor="#fff"
            android:textSize="15sp" />
    RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#fff" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:padding="10dp"
            android:text="检测到有新版本"
            android:textColor="#000"
            android:textSize="15sp" />
    RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="#E5E5E5"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tv_dialog_ok"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_weight="1"
            android:clickable="true"
            android:gravity="center"
            android:padding="10dp"
            android:text="立即下载"
            android:textColor="#70C555"
            android:textSize="14sp" />

        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="#BDBDBD" />

        <TextView
            android:id="@+id/tv_dialog_cancle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_weight="1"
            android:clickable="true"
            android:gravity="center"
            android:padding="10dp"
            android:text="取消"
            android:textColor="#70C555"
            android:textSize="14sp" />
    LinearLayout>

LinearLayout>

布局很简单,就四个TextView,后面两个TextView设置clicable设置成可点状态,(根据个人喜好,也可以换成两个Button),效果如图所示
自定义Dialog_第1张图片

  1. 方式二
        AlertDialog dialog = new AlertDialog.Builder(mActivity).create();
        View dialogView = UIUtils.inflate(R.layout.dialog_update_version);
        TextView tvDialogOk = (TextView) window.findViewById(R.id.tv_dialog_ok);
        TextView tvDialogCancle = (TextView) window.findViewById(R.id.tv_dialog_cancle);
        tvDialogCancle.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        tvDialogOk.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent= new Intent();        
                intent.setAction("android.intent.action.VIEW");    
                Uri content_url = Uri.parse(versionUrl);   
                intent.setData(content_url);  
                startActivity(intent);
                dialog.dismiss();
            }
        });

        dialog.setView(dialogView,0,0,0,0);//和方式一不同之处就在这
        dialog.show();
        //获取屏幕宽度
        int width = UIUtils.getScreenWidth(mActivity);
        LayoutParams params = dialog.getWindow().getAttributes();
        params.width = width-(width/6);
        params.height = LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.CENTER;
        dialog.getWindow().setAttributes(params);

效果和方式一样,我就不上图了
注意:方式一自定义样式中如果有EditText,点击之后,不会跳出软键盘,方式二可以弹出软键盘

你可能感兴趣的:(安卓入门)