Layout is not floating above keyboard in Fragment in Android

大憨熊 提交于 2019-12-11 03:08:11

问题


I have a layout which has two edit texts and horizontal scroll bar with some icons in a fragment.

The horizontal scroll view is permanently fixed to the bottom of the parent using relative layout constraints.

When an edit is clicked, the soft keyboard appears by default. When that happens, I need the horizontal scroll view to float above the soft keyboard so that everyone can use it.

I have added the following line to the AndroidManifest.xml file to the activity containing the fragment

android:windowSoftInputMode="adjustResize"

The view still doesn't work as expected.

How do I fix this?

Current Screen :

Current Screen without keyboard [Current Screen w/o keyboard]1

Current Screen with keyboard [Current Screen with keyboard]2

Expected Screens

Expected Screen without keyboard [Expected Screen w/o keyboard]3

Expected screen with keyboard [Exoected Screen with keyboard]4

And here is the fragment 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>

Where have I gone wrong and how do I fix this?


回答1:


Try this in fragment call on each onCreateView

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



回答2:


You need to add a line in the manifest to adjust the screen visibility in an Activity.

android:windowSoftInputMode="stateVisible|adjustPan" 

So the activity tag in Manifest would be like this:

<activity
   android:name=".MainActivity"
   android:windowSoftInputMode="stateVisible|adjustPan" 
   android:label="@string/app_name" >
   <intent-filter>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
</activity>


来源:https://stackoverflow.com/questions/48677901/layout-is-not-floating-above-keyboard-in-fragment-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!