当软键盘弹出时,DialogFragment未上移

3

我在应用程序的其他地方有对话框,它们工作得非常好(键盘不会隐藏视图),但是在我编写的这个特定的新对话框中,当键盘打开时,它并不会将视图向上移动。 我想不出为什么...

我的对话框布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:id="@+id/MyLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="15dp"
    android:background="@drawable/dialog_background"
    android:clickable="true"
    android:orientation="vertical"
    custom:maxHeight="@dimen/dialog_max_height"
    custom:maxWidth="@dimen/dialog_max_width" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="10dp"
        android:gravity="center"
        android:text="something" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/SomeEditText"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:minHeight="100dp"
            android:layout_margin="15dp"
            android:background="#ffffff"
            android:gravity="right|top"
            android:inputType="textMultiLine"
            android:singleLine="false"
            android:textColor="@color/edit_text_text_color"
            android:textSize="16dp" />

    </LinearLayout>

    <Button
        android:id="@+id/SomeButton"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dialog_button_height"
        android:layout_margin="15dp"
        android:text="text" />

</LinearLayout>

I have

android:windowSoftInputMode="adjustPan" > 

在我的清单文件中设置。


展示你的清单文件。 - K Neeraj Lal
我有 android:windowSoftInputMode="adjustPan"。但是在应用程序中的其他对话框中它可以正常工作,所以我认为这与此无关。我也尝试了adjustResize,但没有帮助。 - BVtp
在你的清单文件中的活动中尝试使用属性 android:windowSoftInputMode="adjustResize" - Sohail Zahid
我写了我尝试过了,但不幸的是它没有帮助。 - BVtp
3个回答

4
我找到了解决方法:
我将以下代码放在onCreateDialog回调函数中:getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

我认为应该使用“WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN”来防止UI被挤压。 - Mohammad Javadian

1

由于 SOFT_INPUT_ADJUST_RESIZE 在 Android 30 开始已被弃用,您应该使用 Window.setDecorFitsSystemWindows(boolean decorFitsSystemWindows)

以下是 DialogFragment 的示例:

class MyDialog extends DialogFragment {

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        getDialog().getWindow().setDecorFitsSystemWindows(true);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.my_dialog, container, false);

        v.setOnApplyWindowInsetsListener((v1, insets) -> {
            Insets imeInsets = insets.getInsets(WindowInsets.Type.ime());
            v1.setPadding(0, 0, 0, imeInsets.bottom);
            return insets;
        });
  
        getDialog().getWindow().setDecorFitsSystemWindows(false);    
        return v;
    }
}

0

我也遇到了同样的问题,并通过在oncreateview中保留getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)来解决了它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接