记DialogFragment中软键盘遮挡问题

很多时候,由于业务需求需要在DialogFragment弹窗中输入内容,但点击输入框的时候却发现,内容无论如何都不会被顶上去,软键盘遮挡了弹窗。但这并不是我们想要的效果。

一般弹窗都是自适应高度,如下设置:

弹窗高度自适应:
window.setLayout(
    ViewGroup.LayoutParams.MATCH_PARENT,
    ViewGroup.LayoutParams.WRAP_CONTENT
)

弹窗布局自适应:
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
</LinearLayout>

这样设置时有问题的,软键盘不能顶起父布局。需要修改设置为:

父布局撑满,给软键盘顶上去的空间:
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"/>
</FrameLayout>

弹窗布局也需要撑满:
window.setLayout(
    ViewGroup.LayoutParams.MATCH_PARENT,
    ViewGroup.LayoutParams.MATCH_PARENT
)

此时不出意外就能达到软键盘顶起内容的效果了