【问题标题】:How to force the soft input to overlap bottom-anchored View?如何强制软输入与底部锚定视图重叠?
【发布时间】:2020-01-02 10:19:36
【问题描述】:

TL;DR软键盘应该与底部锚定视图重叠,而不是向上推。 Gitlab linkmcve

总结:

我有一个AppCompatDialogFragment (androidx),它在手机上出现全屏,在平板电脑上固定尺寸​​(使用dialog?.window?.setLayout(width, height),以防万一)。对话框的布局有一些内容放置在ScrollView 中,底部有一个类似按钮的布局(完整的 XML 结构见下文)。

旁注:有问题的AppCompatDialogFragment 的超类在其Window 上调用setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)

问题:

当软键盘由于某些文本输入接收焦点而出现时,它会将整个布局向上推,包括我希望软键盘重叠的底部锚定视图。出于某种原因,此问题仅影响手机,在平板电脑上,软键盘正确地覆盖了所有内容,包括底部锚定视图,无需任何额外调整或标记。

我尝试过的(没有任何成功):

  • 为有问题的Activity 设置android:windowSoftInputMode="adjustNothing"(也尝试了其他标志“以防万一”),还尝试在代码中应用它们
  • ScrollView 及其父对象上设置android:isScrollContainer="false"
  • 以上组合
  • 寻找类似的问题,如 this one 并确认建议的解决方案不起作用

这是有问题的布局(注意:我省略了许多不相关的属性以保持 sn-p 的合理大小;所有内容都垂直放置以匹配元素的顺序。<include> 元素包含文本输入):

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.Toolbar         
        android:layout_width="match_parent"
        android:id="@+id/toolbar"
        android:layout_height="?attr/actionBarSize"/>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@id/bottom_layout"
        app:layout_constraintTop_toBottomOf="@id/toolbar"
        android:isScrollContainer="false"
        android:scrollbarStyle="outsideOverlay">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <include
                layout="@layout/some_layout_1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

            <include
                layout="@layout/some_layout_2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

        </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>

  <!-- I want this one to be overlapped by the soft input, but it's just pushed up -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/bottom_layout"
        app:layout_constraintBottom_toBottomOf="parent">

        <View
            android:layout_width="match_parent"
            android:layout_height="@dimen/divider"
            android:background="@color/dark_grey" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="@dimen/some_dimen"
            android:foreground="?attr/selectableItemBackground"
            android:text="@string/text" />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

问题:如何强制软键盘覆盖所有设备底部的布局?

如果您需要任何其他详细信息,请发表评论。

编辑:这是一个重现问题的最小演示应用程序:https://gitlab.com/Droidman/soft-keyboard-issue-demo

【问题讨论】:

    标签: android android-layout android-softkeyboard appcompatdialogfragment


    【解决方案1】:

    尝试将以下行添加到DemoDialog 中的onCreateView()

    dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING)

    重要的是将此更改应用到对话框的窗口而不是活动的窗口。

    有了这个,这就是我在使用 MCVE 运行 API 29 的 Nexus 6 模拟器上看到的:

    您可能需要稍微处理一下展示位置,但这应该会有所帮助。


    让我们深入了解一下。对于手机和平板电脑,onCreateView() 中的软输入模式为零。此值对应于SOFT_INPUT_ADJUST_UNSPECIFIEDSOFT_INPUT_ADJUST_PAN 的文档解释了未指定软输入模式时会发生什么(重点是我的。)

    softInputMode的调整选项:设置为显示输入法时有一个窗口平移,所以它不需要处理调整大小而只是由框架平移以确保当前输入焦点可见。这不能与 SOFT_INPUT_ADJUST_RESIZE 结合使用; 如果这两个都没有设置,那么系统将根据窗口的内容尝试选择一个或另一个

    查看onStart()dialog?.window?.attributes?.softInputMode)中软输入模式的值,系统为手机选择的值为SOFT_INPUT_ADJUST_RESIZE),为平板电脑选择的值为SOFT_INPUT_ADJUST_PAN。这解释了手机和平板电脑之间的差异。

    因此,始终将软输入模式设置为 SOFT_INPUT_ADJUST_PAN 看起来是最好的解决方案,尽管 SOFT_INPUT_ADJUST_NOTHING 也可以工作,并且对于某些布局可能更可取。

    【讨论】:

    • 感谢您指出这一点,它有效。我想我明显的错误是我试图通过代码清单在活动级别设置SOFT_INPUT_ADJUST_NOTHING(和其他人),而忽略了我实际上需要将它们应用到对话框的窗口 代替。
    【解决方案2】:

    将此添加到您的班级中 AndroidManifest 中的标签内:

    android:windowSoftInputMode="stateHidden|adjustResize"
    

    【讨论】:

    • 很遗憾这不起作用,底部的布局仍然被推到软输入之上。 “隐藏状态”已经在代码中设置,我已经更新了问题以反映这一点,尽管我怀疑它很重要。
    • 可以分享代码吗?所以我可以正确检查问题
    • 我创建了一个简单的演示应用程序,它演示了问题gitlab.com/Droidman/soft-keyboard-issue-demo,正如您在清单中看到的那样,您的建议已被应用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-04
    • 2021-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多