AlertDialog和Dialog的使用差别

359 阅读1分钟

1.不说定义,只说坑,AlertDialog用build的方式创建,如果你是自定义View设置到Dialog中,且想要宽高设置成占手机屏幕百分比的效果,则布局不要用LinearLayout,不要用ConstraintLayout,用RelativeLayout,此外,设置宽高的代码必须放在dialog.show()之后,下面这个是一个可以设置宽高百分比的代码demo:

image.png

/**
     * 展示蓝牙绑定弹窗,这是在Fragment里面调用的
     */
    private fun showBluetoothBindDialog() {
        val dialogView = LayoutInflater.from(context).inflate(R.layout.alertdialog_test_relativelayout, null)
        val builder = AlertDialog.Builder(context)
        builder.setView(dialogView).setCancelable(false)
        var dialogBluetooth = builder.create()
        dialogBluetooth.setCanceledOnTouchOutside(false)
        dialogBluetooth.show() // 注意,这里要先调用,否则下面的宽高设置无效。
        dialogBluetooth.window?.setBackgroundDrawableResource(android.R.color.transparent);

        val display = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            activity?.display
        } else {
            activity?.windowManager?.defaultDisplay
        }

        var params = dialogBluetooth.window?.attributes
        params?.width =  (display?.width!! * 0.8).toInt()
        params?.height = (display?.height!! * 0.8).toInt()

        dialogBluetooth.window?.attributes = params

        dialogView.findViewById<TextView>(R.id.tvYes).setOnClickListener{
            Toast.makeText(context,"yes",Toast.LENGTH_SHORT).show()
        }
        dialogView.findViewById<TextView>(R.id.tvNo).setOnClickListener{
            dialogBluetooth.dismiss()
        }
    }

alertdialog_test_relativelayout.xml布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/dialog_bg"
    tools:context=".SysWifiFragment">

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="match_parent"
        android:layout_height="36dp"
        android:text="PIN Codec"
        android:textColor="@color/white"
        android:textSize="20sp"
        android:gravity="center"
         />

    <TextView
        android:id="@+id/tvSubtitle"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_marginTop="8dp"
        android:text="668888"
        android:gravity="center"
        android:textColor="@color/white"
        android:textSize="34sp"
        android:layout_below="@+id/tvTitle"
         />

    <TextView
        android:id="@+id/tvContent"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="Does the number match the one shown on your device?"
        android:textColor="@color/white"
        android:background="@color/teal_700"
        android:textSize="20sp"
        android:layout_below="@+id/tvSubtitle"
        android:layout_marginBottom="60dp"
        />

    <androidx.appcompat.widget.LinearLayoutCompat
        android:id="@+id/linearlayoutBottom"
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_height="60dp"
        android:background="@drawable/dialog_button_bg"

        android:layout_alignParentBottom="true"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tvYes"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Yes"
            android:textColor="@color/white" />

        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@color/white_transparent" />

        <TextView
            android:id="@+id/tvNo"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="No"
            android:textColor="@color/white" />
    </androidx.appcompat.widget.LinearLayoutCompat>

</RelativeLayout>

2.Dialog没这么多门道,随便用,show()在前后都没影响。

private fun showBluetoothBindDialog2() {
        val dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_test_constraintlayout, null)
        if(null == mDialog) {
            mDialog = Dialog(context!!)
        }
        mDialog?.show() // 顺序在前后都可
        mDialog?.let {
            it.setContentView(dialogView)
            it.setCancelable(true)
            it.setCanceledOnTouchOutside(true)
            it.window?.apply { setBackgroundDrawableResource(android.R.color.transparent); }?.attributes?.let { lp->
                lp.width = getScreenWidth(context!!)/10*8
                lp.height = getScreenHeight(context!!)/10*8
                lp.gravity = Gravity.CENTER
                it.window?.attributes = lp
            }
        }
        //mDialog?.show()  // 顺序在前后都可
    }

dialog_test_constraintlayout.xml布局代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/dialog_bg"
    android:paddingTop="32dp"
    tools:context=".SysWifiFragment">

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="PIN Codec"
        android:textColor="@color/white"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tvSubtitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="668888"
        android:textColor="@color/white"
        android:textSize="34sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvTitle" />

    <TextView
        android:id="@+id/tvContent"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:gravity="center"
        android:text="Does the number match the one shown on your device?"
        android:textColor="@color/white"
        android:background="@color/teal_700"
        android:textSize="20sp"
        app:layout_constraintBottom_toTopOf="@+id/linearlayoutBottom"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvSubtitle"
        app:layout_constraintVertical_bias="0.09"
        app:layout_constraintVertical_weight="1"/>

    <androidx.appcompat.widget.LinearLayoutCompat
        android:id="@+id/linearlayoutBottom"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@drawable/dialog_button_bg"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <TextView
            android:id="@+id/tvYes"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Yes"
            android:textColor="@color/white" />

        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@color/white_transparent" />

        <TextView
            android:id="@+id/tvNo"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="No"
            android:textColor="@color/white" />
    </androidx.appcompat.widget.LinearLayoutCompat>

</androidx.constraintlayout.widget.ConstraintLayout>