Layout is not floating above keyboard in Fragment

2019-07-17 05:02发布

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?

2条回答
Viruses.
2楼-- · 2019-07-17 05:36

Try this in fragment call on each onCreateView

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
查看更多
Emotional °昔
3楼-- · 2019-07-17 05:39

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>
查看更多
登录 后发表回答