android - 如何使对话框的按钮居中?(科特林,Android 工作室)
问题描述
默认情况下,对话框的按钮向右对齐,如下所示:
不确定这是否是 Android 推荐的标准,但对我来说它看起来很奇怪。我想将按钮居中。
我从这里尝试了不同的配置,但没有结果:将 AlertDialog 按钮对齐到中心
java
当我尝试将其写入kotlin
或该帖子已有 3.5 年历史时,也许它让我感到困惑。无论如何,有人对如何在不诉诸对话的xml
情况下以编程方式解决这个问题有什么建议吗?inflating
解决方案
如果您不喜欢警报对话框构建器方法,这里有一个简单的自定义警报对话框
这个 btnDeleteDept 调用 doCustom() fun
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" />
推荐阅读
- java - Java 堆空间不足,无法在 AWS S3 上上传文件
- android - 将字符串附加到 Firebase 实时数据库上的列表
- angular - 在行标题列标题中带有树的角度表
- angular7 - 如何在 Angular7 中的 HttpUrl 中传递字典对象?
- algorithm - Optimal solution for clustering of rectangles
- java - ImageView onClick 动画在资源更改后不起作用
- php - 如何完全(我的意思是完全)销毁所有会话数据并防止缓存访问?
- file - 如何使用 corona sdk 读取特定文件
- javascript - 允许javascript控制一些环境的东西
- wpf - WPF Datagrid:如何通过鼠标单击列标题来清除列排序?