Android 中 Fragment 中的布局未浮动在键盘上方

2024-04-29

我有一个布局,其中有两个编辑文本和水平滚动条,片段中带有一些图标。

使用相对布局约束,水平滚动视图永久固定到父级的底部。

单击编辑时,默认情况下会出现软键盘。 当发生这种情况时,我需要水平滚动视图浮动在软键盘上方,以便每个人都可以使用它。

我已将以下行添加到 AndroidManifest.xml 文件中包含该片段的活动

android:windowSoftInputMode="adjustResize"

该视图仍然没有按预期工作。

我该如何解决?

当前屏幕:

当前屏幕(无键盘) [当前屏幕(无键盘)] https://i.stack.imgur.com/WhcIo.png1 https://i.stack.imgur.com/WhcIo.png

带键盘的当前屏幕 [当前屏幕与键盘] https://i.stack.imgur.com/sFR9T.png2 https://i.stack.imgur.com/sFR9T.png

预期屏幕

没有键盘的预期屏幕 [不带键盘的预期屏幕] https://i.stack.imgur.com/Zf0jN.png3 https://i.stack.imgur.com/Zf0jN.png

预期的屏幕和键盘 [带键盘的预期屏幕] https://i.stack.imgur.com/hGj05.png4 https://i.stack.imgur.com/hGj05.png

这是片段 XML file.

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <io.github.mthli.knife.KnifeText
        android:id="@+id/edt_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/title_notes_add"
        android:background="@android:color/transparent"
        android:hint="Content"
        android:padding="12dp"
        android:textSize="20sp"
        app:historyEnable="true"
        app:historySize="20" />

    <EditText
        android:id="@+id/title_notes_add"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:hint="Title"
        android:minLines="1"
        android:padding="12dp"
        android:textSize="24sp"
        android:textStyle="bold" />

    <HorizontalScrollView
        android:id="@+id/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@android:color/white"
        android:scrollbars="none">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageButton
                android:id="@+id/undo"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:background="?android:selectableItemBackground"
                android:contentDescription="@null"
                android:scaleType="center"
                android:src="@drawable/ic_undo" />

            <ImageButton
                android:id="@+id/redo"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:background="?android:selectableItemBackground"
                android:contentDescription="@null"
                android:scaleType="center"
                android:src="@drawable/ic_redo" />

            <ImageButton
                android:id="@+id/bold"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:background="?android:selectableItemBackground"
                android:contentDescription="@null"
                android:scaleType="center"
                android:src="@drawable/ic_format_bold" />

            <ImageButton
                android:id="@+id/italic"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:background="?android:selectableItemBackground"
                android:contentDescription="@null"
                android:scaleType="center"
                android:src="@drawable/ic_format_italic" />

            <ImageButton
                android:id="@+id/underline"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:background="?android:selectableItemBackground"
                android:contentDescription="@null"
                android:scaleType="center"
                android:src="@drawable/ic_format_underlined" />

            <ImageButton
                android:id="@+id/strikethrough"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:background="?android:selectableItemBackground"
                android:contentDescription="@null"
                android:scaleType="center"
                android:src="@drawable/ic_strikethrough" />

            <ImageButton
                android:id="@+id/bullet"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:background="?android:selectableItemBackground"
                android:contentDescription="@null"
                android:scaleType="center"
                android:src="@drawable/ic_format_list_bulleted" />

            <ImageButton
                android:id="@+id/quote"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:background="?android:selectableItemBackground"
                android:contentDescription="@null"
                android:scaleType="center"
                android:src="@drawable/ic_format_quote" />

            <ImageButton
                android:id="@+id/link"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:background="?android:selectableItemBackground"
                android:contentDescription="@null"
                android:scaleType="center"
                android:src="@drawable/ic_insert_link" />

            <ImageButton
                android:id="@+id/clear"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:background="?android:selectableItemBackground"
                android:contentDescription="@null"
                android:scaleType="center"
                android:src="@drawable/ic_format_clear" />

        </LinearLayout>    
    </HorizontalScrollView>       
</RelativeLayout>

我哪里出了问题以及如何解决这个问题?


在每个 onCreateView 的片段调用中尝试这个

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 中 Fragment 中的布局未浮动在键盘上方 的相关文章

随机推荐