键盘打开时工具栏会扩展

Toolbar extends when keyboard opens

我在状态栏下方有一个工具栏。如果我单击 TextInputLayout,键盘打开,但我的工具栏将扩展,如下所示(如果键盘关闭,工具栏具有通常的大小):

我的 xml- 文件:

工具栏:

<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" />

包含的地方:

<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>

尝试从工具栏中删除 android:fitsSystemWindows="true"

编辑: 也许你应该改用 coordinatorlayout 并将你的工具栏放在那里。

我遇到了同样的问题,最终通过使用 CoordinatorLayout 作为我的 activity 根和 app:statusBarColor="@color/some_color" 并将 android:fitsSystemWindows="true" 移到那里来解决了这个问题。可能不是最好的解决方案,但却是唯一适用于我的布局的解决方案。

<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>

我最终设法通过将 ScrollView 直接放入 ConstraintLayout 中解决了我的问题(而不是使用 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>