Toolbar shifts up, when soft keyboard appears

2019-07-15 01:07发布

问题:

I want to make my activity layout resize when keyboard appears, but at the same time keep its toolbar position unchanged. So far all the screen shifts up, when I click on the edit text and the keyboard appears.

Before keyboard appears:

After keyboard appears, the toolbar isn't visible anymore:

I would like to keep the keyboard in its place and shift below it the image. I can't just specify in the manifest: android:windowSoftInputMode="adjustNothing" , because the edit text will not be visible anymore, when the keyboard appears.

So far, I use the following code:

In AndroidManifest.xml:

android:windowSoftInputMode="adjustPan|stateHidden"

In layout:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:clickable="true"
    android:fitsSystemWindows="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
    tools:context="studentplanner.mobile.yardi.com.studplan.presentation.activities.PersonalInformationActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/toolbar_and_button_height"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:elevation="4dp"
        android:background="@color/main">

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/personal_information_text"
            android:textColor="@android:color/white"
            android:textSize="@dimen/medium2_text_size" />

    </android.support.v7.widget.Toolbar>

    <ImageView
        android:id="@+id/background_container"
        android:layout_width="match_parent"
        android:layout_height="@dimen/extra_large5_dimen"
        android:layout_below="@+id/toolbar"
        android:scaleType="center"
        android:src="@drawable/userbackground" />

...

Any help is welcomed!

回答1:

styles file

<item name="android:windowTranslucentStatus">true</item> 

and Add

android:fitsSystemWindows="true" 

in your activity ParentLayout



回答2:

<item name="android:windowTranslucentStatus">true</item> in styles.xml

add android:fitsSystemWindows="true" in your activity's parent layout and add android:windowSoftInputMode="adjustResize" in manifest



回答3:

i had the same issue.
when dealing with fragments i put isScrollContainer inside of the framelayout and it resolves all keyboard issues.

  <FrameLayout android:id="@+id/container_framelayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:isScrollContainer="true"/>

thats all i needed.

but if you want ti on a per fragment bases then do this by setting your container to scrolling container in onCreateView fragment callback:

@Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_type_barcode, container, false);
        container.setScrollContainer(true);

        return rootView;
    }


回答4:

Adding android:windowSoftInputMode="adjustResize" in AndroidManifest.xml solve the problem.

<activity
        android:name=".MainActivity"
        android:label="@string/activity_main"
        android:windowSoftInputMode="adjustResize">
 </activity>