android - 在 Android 的 Fragment 中,布局未 float 在键盘上方

标签 android android-fragments android-edittext android-softkeyboard

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

水平 ScrollView 使用相对布局约束永久固定在父 View 的底部。

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

我已将以下行添加到包含该 fragment 的 Activity 的 AndroidManifest.xml 文件中

android:windowSoftInputMode="adjustResize"

View 仍然没有按预期工作。

我该如何解决这个问题?

当前屏幕:

没有键盘的当前屏幕 [Current Screen w/o keyboard] 1

带键盘的当前屏幕 [Current Screen with keyboard] 2

预期屏幕

没有键盘的预期屏幕 [Expected Screen w/o keyboard] 3

带键盘的预期屏幕 [Exoected Screen with keyboard] 4

这是 fragment xml 文件。

    <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 的 fragment 调用中尝试这个

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

关于android - 在 Android 的 Fragment 中,布局未 float 在键盘上方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48677901/

相关文章:

java - SharedPreferences 变量无法正常工作

Android - 计时器内部服务

android - 我可以得到编辑文本离开事件吗

android - 企业内部的管理API和应用分发

java - 尝试添加时出错 "AdMob"

android - fragment 管理器是否在方向改变后恢复屏幕上最后显示的 fragment ?

java - 在抽屉导航项目单击上切换 fragment

android - 在 fragmentTabs 之间切换会产生意想不到的结果

android - 如何在Android中的edittext开头添加一个空格?

android - 以编程方式创建具有属性 singleLine=true 的 editText