You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何防止键盘弹出时MaterialAlertDialogBuilder高度自动缩小?

解决MaterialAlertDialogBuilder对话框键盘弹出时高度缩小的问题

问题描述

使用MaterialAlertDialogBuilder构建包含2个TextInputLayout的自定义对话框时,键盘弹出后对话框高度自动缩小,布局被异常压缩。

解决方案

步骤1:给对话框根布局设置最小高度

修改自定义对话框的XML布局,给根LinearLayout添加android:minHeight属性,限制布局最低高度,避免被键盘挤压:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
    android:minHeight="240dp" <!-- 添加最小高度限制,数值可按需调整 -->
    android:orientation="vertical"
    android:padding="16dp"
    tools:viewBindingIgnore="true">

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="দোয়া"
        app:endIconMode="clear_text"
        app:shapeAppearance="@style/ShapeAppearance.App.SmallComponent"
        tools:viewBindingIgnore="true">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/doarName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="11dp"
        android:hint="দৈনিক কতবার পাঠ করবেন?"
        app:endIconMode="clear_text"
        app:shapeAppearance="@style/ShapeAppearance.App.SmallComponent"
        tools:viewBindingIgnore="true">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/number"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="number" />

    </com.google.android.material.textfield.TextInputLayout>

</LinearLayout>

步骤2:单独设置对话框的软键盘模式

Activity的windowSoftInputMode设置不会作用于对话框,需单独给对话框的Window配置合适的模式,确保键盘弹出时布局不被压缩:

import android.view.WindowManager

// 先创建Dialog对象,而非直接show()
val dialog = MaterialAlertDialogBuilder(this@TosbiActivity)
    .setView(view)
    .setTitle("দোয়া যুক্ত করুন")
    .setPositiveButton("Apply") { dialog, _ ->
        // 业务逻辑处理
        dialog.dismiss()
    }
    .setNegativeButton("Cancel") { dialog, _ ->
        dialog.dismiss()
    }
    .create()

// 设置对话框Window的软键盘模式
dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
// 若需要完全禁止布局调整,可替换为:
// dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING)

dialog.show()

模式说明

  • SOFT_INPUT_ADJUST_PAN:键盘弹出时将对话框整体上移,不会压缩布局
  • SOFT_INPUT_ADJUST_NOTHING:键盘弹出时不调整对话框的位置与大小,需确保对话框不会被键盘遮挡(可结合布局微调)

内容的提问来源于stack exchange,提问作者AR Sharif Uddin JUMMAN

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 09:25:35