简体   繁体   中英

Toolbar extends when keyboard opens

I have a toolbar that is beneath the status bar. If I click on a TextInputLayout, the keyboard opens but my toolbar will extend as shown here (if the keyboard is closed, the Toolbar has the usual size):

我的问题

My xml-files:

The toolbar:

<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/backgroundColor"
    android:fitsSystemWindows="true"
    android:theme="@style/AppTheme.SubAppBarOverlay"
    app:popupTheme="@style/AppTheme.PopupOverlay"
    app:title="Title"
    android:elevation="4dp" />

Where it is included:

<androidx.constraintlayout.widget.ConstraintLayout 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="?attr/backgroundColor"
    android:fitsSystemWindows="false"
    tools:context=".EditDayActivity">

    <include
        android:id="@+id/sub_toolbar"
        layout="@layout/sub_toolbar"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <RelativeLayout
        android:id="@+id/scrollViewContainer"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/sub_toolbar"
        android:fitsSystemWindows="false">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:fitsSystemWindows="true">

   </ScrollView>
    </RelativeLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Try removing the android:fitsSystemWindows="true" from your toolbar

Edit: Maybe you should use a coordinatorlayout instead and put your toolbar in there.

I had the same problem and eventually got around it by using CoordinatorLayout as my activity root with app:statusBarColor="@color/some_color" and moving android:fitsSystemWindows="true" there. Probably not the best solution but the only one that worked with my layout.

<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MyActivity"
    app:statusBarBackground="@color/some_color">

    ...
</androidx.coordinatorlayout.widget.CoordinatorLayout>

I eventually managed to solve my problem by putting the ScrollView directly into the ConstraintLayout like this (and not using a RelativeLayout ):

<androidx.constraintlayout.widget.ConstraintLayout
    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="?attr/backgroundColor"
    android:fitsSystemWindows="true"
    tools:context=".MyActivity">

    <include
        ... />

    <ScrollView 
        ... />
</androidx.constraintlayout.widget.ConstraintLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM