键盘打开时工具栏会扩展

问题描述

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

this

我的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 作为我的活动根和 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>