如何将对话框的按钮居中? (Kotlin,Android Studio)

时间:2018-10-10 10:07:16

标签: android android-studio button layout kotlin

默认情况下,对话框的按钮像这样向右对齐:

enter image description here

不知道这是否是Android推荐的标准,但对我来说看起来很奇怪。我想将按钮居中。

我从这里尝试了不同的配置,但没有结果:align AlertDialog buttons to center

也许当我尝试用java编写它时,它的kotlin令我感到困惑,或者该帖子已有3.5年的历史了。无论如何,在不借助xmlinflating对话框的情况下,有人对如何以编程方式解决此问题有任何建议吗?

1 个答案:

答案 0 :(得分:0)

如果您不喜欢“警报对话框”构建器方法,这里是一个简单的自定义警报对话框

此btnDeleteDept称为doCustom()乐趣

        btnDeleteDept.setOnClickListener {
        if (etDeptData.text.toString().equals("")) {
            message("No Match Found")
            return@setOnClickListener
        }
        doCustom()
    }

这是doCustom()乐趣的代码,它使用了您所看到的另一种乐趣

    fun doCustom() {
    /* This method uses the custom_dialog.xml file created for greater control over
   the styling of the Custom Alert Dialog for various screen sizes and to be
   able to set the text size of the dialog message text
   */
    val makeDialog = LayoutInflater.from(this).inflate(R.layout.custom_dialog,null)
    val mBuilder = AlertDialog.Builder(this).setView(makeDialog)
    val mAlertDialog = mBuilder.show()

    val btnYES = makeDialog.findViewById<Button>(R.id.btnYES)
    val btnNO = makeDialog.findViewById<Button>(R.id.btnNO)
    mAlertDialog.setCancelable(false)

    btnYES.setOnClickListener {
        removeDEPT()
        mAlertDialog.dismiss()
    }

    btnNO.setOnClickListener {
        message("Record NOT Deleted")
        etDeptData.setText("")
        //Timer().schedule(800){
            thisACTIVITY()
        //}
        mAlertDialog.dismiss()
    }
    mAlertDialog.show()
}

private fun removeDEPT() {

    val dbHandler = DBHelper(this)

    val result = dbHandler.deleteDEPT(etDeptData.text.toString())

    if (result) {
        etDeptData.setText("")
        message("Record Removed")
        //Timer().schedule(1000){
            thisACTIVITY()
        //}
    }else{
        etDeptData.setText("NO MATCH -> click View Dept List")
        btnViewDeptList.visibility = View.VISIBLE
        btnEditDept.visibility = View.INVISIBLE
        btnDeleteDept.visibility =View.INVISIBLE
        btnSaveDeptData.visibility = View.INVISIBLE
        message("NO Match Found")
    }
}

好吧,没有XML文件,这一切都不是

<?xml version="1.0" encoding="utf-8"?>

<ImageView
    android:id="@+id/imgDI"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:src="@drawable/delete48" />

<TextView
    android:id="@+id/tvDAT"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="80dp"
    android:layout_marginTop="30dp"
    android:text="Delete Data"
    android:textColor="@color/color_Black"
    android:textSize="20sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/tvDAC"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="28dp"
    android:layout_marginTop="80dp"
    android:gravity="center"
    android:text="Click DELETE to Remove Data"
    android:textColor="@color/color_Black"
    android:textSize="18sp"
    android:textStyle="bold" />

<Button
    android:id="@+id/btnYES"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="240dp"
    android:layout_marginTop="110dp"
    android:background="@color/color_Transparent"
    android:text="DELETE"
    android:textColor="@color/color_deepBlue"
    android:textSize="18sp"
    android:textStyle="bold" />

<Button
    android:id="@+id/btnNO"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="110dp"
    android:background="@color/color_Transparent"
    android:text="CANCEL"
    android:textColor="@color/color_deepBlue"
    android:textSize="18sp"
    android:textStyle="bold" />