android - 键盘打开时 fragment 不会调整大小

标签 android android-fragments android-softkeyboard android-constraintlayout

我有一个 EditText,它监听 Activity 上的点击事件并使用 fragmentTransaction.add() 启动 fragment ; Activity 和 Fragment 都有一个 ConstraintLayout 作为根 ViewGroup。 fragment 布局如下(为简洁起见,省略了一些元素):

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/fragment_CitySelection"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="25dp"
    android:background="@color/white"
    android:clickable="true"
    android:focusable="true"
    tools:context=".CitySelectionFragment">


    <EditText
        android:id="@+id/city_AutoCompleteTextView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="16dp"
        android:background="@layout/rounded_border_edittext"
        android:hint="Ville"
        android:paddingStart="8dp"
        android:paddingLeft="8dp"
        android:paddingTop="6dp"
        android:paddingEnd="8dp"
        android:paddingRight="8dp"
        android:paddingBottom="10dp"
        android:textSize="22sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/back_ImageButton" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/cities_RecyclerView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="12dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/city_AutoCompleteTextView" />

</androidx.constraintlayout.widget.ConstraintLayout>

我尝试添加 android:windowSoftInputMode="adjustResize"在 AndroidManifest 中并以编程方式作为一些类似问题的答案。

最佳答案

问题是由我添加的一些代码引起的,以使状态栏完全透明,我在这个 answer 中找到了这些代码。 .

添加 android:fitsSystemWindows="true"到 fragment 布局解决了这个问题。

关于android - 键盘打开时 fragment 不会调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55539806/

相关文章:

Android - 键盘消失时留下空白

android - onCreate() 内的 finish() 无效回调

Android ListView 状态列表不显示默认项目背景

java - 将初始值设置为静态字段

android - 为什么在调用第一个 fragment 时会自动调用第二个 fragment ?

java - 可运行处理程序未在 fragment 内执行。无法启动可运行的

android - 按回隐藏软键盘

Android:使用 WebView.loadUrl 加载 JavaScript 但保留软键盘

android - 不同动画机制的优缺点是什么

android - 如何通过单击返回(不是硬件后退按钮)返回到上一个 fragment ?