Android 键盘打开时警报对话框不居中
Android alertdialog not centered when keyboard opens
我有一个带有包含两个 EditText 字段的自定义视图的 AlertDialog。在某些时候,使用以下代码显示对话框:
val builder = AlertDialog.Builder(this)
builder
.setView(R.layout.dialog)
.setMessage("message")
.setPositiveButton("Oke"){ dialog, id -> println("oke")}
.setNegativeButton("Cancel"){ dialog, id -> println("cancel")}
val dialog = builder.create()
dialog.show()
当用户按下 EditText 时,SoftKeyboard 出现,但 Dialog 没有向上移动。我试过像这样将 dialog.window 的 softinputmode 设置为 SOFT_INPUT_ADJUST_PAN 或 SOFT_INPUT_ADJUST_RESIZE。
dialog.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
但这似乎没有任何作用。
试试这个:
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
显示 AlertDialog 的 Activity 有一个半透明的状态栏主题。
因此,您可以使用没有半透明状态栏的对话框主题,而不是使用使用上下文主题的经典 AlertDialog.Builder(this) 来初始化构建器:
val builder = AlertDialog.Builder(this, android.R.style.Theme_DeviceDefault_Light_Dialog_NoActionBar)
这个主题也确保了与棒棒糖之前的设备的兼容性,但 android.R.style.Theme_Material_Dialog
等其他设备也应该可以做到这一点。
我有一个带有包含两个 EditText 字段的自定义视图的 AlertDialog。在某些时候,使用以下代码显示对话框:
val builder = AlertDialog.Builder(this)
builder
.setView(R.layout.dialog)
.setMessage("message")
.setPositiveButton("Oke"){ dialog, id -> println("oke")}
.setNegativeButton("Cancel"){ dialog, id -> println("cancel")}
val dialog = builder.create()
dialog.show()
当用户按下 EditText 时,SoftKeyboard 出现,但 Dialog 没有向上移动。我试过像这样将 dialog.window 的 softinputmode 设置为 SOFT_INPUT_ADJUST_PAN 或 SOFT_INPUT_ADJUST_RESIZE。
dialog.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
但这似乎没有任何作用。
试试这个:
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
显示 AlertDialog 的 Activity 有一个半透明的状态栏主题。
因此,您可以使用没有半透明状态栏的对话框主题,而不是使用使用上下文主题的经典 AlertDialog.Builder(this) 来初始化构建器:
val builder = AlertDialog.Builder(this, android.R.style.Theme_DeviceDefault_Light_Dialog_NoActionBar)
这个主题也确保了与棒棒糖之前的设备的兼容性,但 android.R.style.Theme_Material_Dialog
等其他设备也应该可以做到这一点。