如何隐藏或更改 Kotlin 对话框后面的页面背景?
how to hide or change the background of the page behind the dialog in Kotlin?
我正在尝试删除或更改此对话框出现时的颜色或背景:
是否可以从对话框中完成?
对话函数:
fun showAddNoteDialog() {
val dialog = Dialog(requireActivity())
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog.setCancelable(true)
dialog.setContentView(R.layout.ilmnotes_dialog)
btn_add = dialog.findViewById(R.id.btn_add)
titleEditText = dialog.findViewById(R.id.title)
noteDescText = dialog.findViewById(R.id.noteDesc)
date_container = dialog.findViewById(R.id.date_container)
date_container.applyClickShrink()
date_container.setOnClickListener {
val datePicker: DialogFragment = DatePickerFragment()
// Get Current Date
val c = Calendar.getInstance()
mYear = c[Calendar.YEAR]
mMonth = c[Calendar.MONTH]
mDay = c[Calendar.DAY_OF_MONTH]
val datePickerDialog = DatePickerDialog(
it.context,
DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
Toast.makeText(
it.context,
"$year-$monthOfYear-$dayOfMonth",
Toast.LENGTH_LONG
).show()
}, mYear, mMonth, mDay
)
datePickerDialog.show()
}
btn_add.setOnClickListener {
postNote(dialog)
}
dialog.show()
}
所以我试图隐藏对话框后面的页面而不是对话框背景本身
实现此目标的正确方法是什么?
创建全屏布局
ilmnotes_dialog.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"
android:layout_width="match_parent"
android:layout_height="match_parent">
//Container here
</androidx.constraintlayout.widget.ConstraintLayout>
Kotlin 代码
val dialog = Dialog(this, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen)
dialog.setContentView(R.layout.ilmnotes_dialog)
val window = dialog.window
window!!.setBackgroundDrawableResource(R.color.dialog_color)
dialog.show()
在 color.xml
中创建颜色变量
<color name="dialog_color">#80BB86FC</color>
注意:这里BB86FC是颜色,所以根据你的UI改变,80是alpha,FF是完全不透明的,00是透明的
检查这个旧的 post:How to change the background color around a DialogFragment?
(考虑使用 DialogFragment)
我正在尝试删除或更改此对话框出现时的颜色或背景:
是否可以从对话框中完成?
对话函数:
fun showAddNoteDialog() {
val dialog = Dialog(requireActivity())
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog.setCancelable(true)
dialog.setContentView(R.layout.ilmnotes_dialog)
btn_add = dialog.findViewById(R.id.btn_add)
titleEditText = dialog.findViewById(R.id.title)
noteDescText = dialog.findViewById(R.id.noteDesc)
date_container = dialog.findViewById(R.id.date_container)
date_container.applyClickShrink()
date_container.setOnClickListener {
val datePicker: DialogFragment = DatePickerFragment()
// Get Current Date
val c = Calendar.getInstance()
mYear = c[Calendar.YEAR]
mMonth = c[Calendar.MONTH]
mDay = c[Calendar.DAY_OF_MONTH]
val datePickerDialog = DatePickerDialog(
it.context,
DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
Toast.makeText(
it.context,
"$year-$monthOfYear-$dayOfMonth",
Toast.LENGTH_LONG
).show()
}, mYear, mMonth, mDay
)
datePickerDialog.show()
}
btn_add.setOnClickListener {
postNote(dialog)
}
dialog.show()
}
所以我试图隐藏对话框后面的页面而不是对话框背景本身
实现此目标的正确方法是什么?
创建全屏布局
ilmnotes_dialog.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"
android:layout_width="match_parent"
android:layout_height="match_parent">
//Container here
</androidx.constraintlayout.widget.ConstraintLayout>
Kotlin 代码
val dialog = Dialog(this, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen)
dialog.setContentView(R.layout.ilmnotes_dialog)
val window = dialog.window
window!!.setBackgroundDrawableResource(R.color.dialog_color)
dialog.show()
在 color.xml
中创建颜色变量<color name="dialog_color">#80BB86FC</color>
注意:这里BB86FC是颜色,所以根据你的UI改变,80是alpha,FF是完全不透明的,00是透明的
检查这个旧的 post:How to change the background color around a DialogFragment?
(考虑使用 DialogFragment)