You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android聊天界面弹出键盘Toolbar上移,需改为RecyclerView适配调整

问题:聊天界面键盘弹出时Toolbar上移,需改为RecyclerView自适应

开发聊天类UI,界面包含Toolbar、聊天消息RecyclerView和回复布局。EditText获取焦点弹出键盘时,Toolbar会向上移动,期望改为让RecyclerView调整尺寸适配。已尝试在Toolbar下方添加空ScrollView但无效,且Manifest已设置android:windowSoftInputMode="adjustResize"。

现有XML代码

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <data>

    </data>


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="@dimen/album_dp_20"
        >

        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <androidx.appcompat.widget.Toolbar
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginHorizontal="@dimen/dp20">

                    <ImageView
                        android:id="@+id/back"
                        android:layout_width="24dp"
                        android:layout_height="24dp"
                        android:layout_centerVertical="true"
                        android:src="@drawable/backbutton" />


                    <TextView
                        android:id="@+id/toolbar_title"
                        style="@style/Text_title_black_bold"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        android:text="@string/comment"
                        android:textSize="@dimen/dp20" />
                </RelativeLayout>
            </androidx.appcompat.widget.Toolbar>
        </LinearLayout>


        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@id/bottomLL"
            android:layout_below="@id/linearLayout"
            android:layout_marginTop="@dimen/dp_10"
            tools:listitem="@layout/list_item_comments" />


        <LinearLayout
            android:id="@+id/bottomLL"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginHorizontal="@dimen/dp_32"
            android:layout_marginVertical="@dimen/album_dp_20"
            android:background="@drawable/et_background"
            android:backgroundTint="@color/gray"
            android:weightSum="2">

            <EditText
                android:id="@+id/commentsTV"
                style="@style/Text_title_gray_black_bold"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginStart="16dp"
                android:layout_weight="1.5"
                android:background="@null"
                android:hint="Write Message..."
                android:inputType="textAutoComplete"
                android:paddingVertical="@dimen/dp_10"
                android:textColor="#454545"
                android:textSize="@dimen/text_size_64px" />

            <androidx.appcompat.widget.AppCompatButton
                android:id="@+id/btn_buzz"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="0dp"
                android:layout_height="@dimen/dp60"
                android:layout_weight="0.5"
                android:background="@drawable/et_background"
                android:backgroundTint="@color/red"
                android:fontFamily="@font/segoeui"
                android:text="Post"
                android:textAllCaps="false"
                android:textColor="@color/white"
                android:textSize="@dimen/text_size_64px"
                android:textStyle="bold" />

        </LinearLayout>

        <RelativeLayout
            android:id="@+id/progress"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:background="#6BE1E1D1"
            android:visibility="gone">

            <com.github.ybq.android.spinkit.SpinKitView
                style="@style/SpinKitView.Large.WanderingCubes"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp75"
                android:layout_centerInParent="true"
                app:SpinKit_Color="@color/red_dark" />

        </RelativeLayout>
    </RelativeLayout>

</layout>

Manifest设置

android:windowSoftInputMode="adjustResize"

解决方案

1. 修复根布局高度问题

根RelativeLayout设置android:layout_height="wrap_content"会导致布局无法正确响应adjustResize,改为:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/album_dp_20">

2. 排查全屏/沉浸式设置干扰

如果Activity使用了全屏或沉浸式状态栏,会导致adjustResize失效。检查代码中是否有如下设置,若有则移除:

getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

或者给根布局添加android:fitsSystemWindows="true":

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:paddingBottom="@dimen/album_dp_20">

3. 推荐改用ConstraintLayout布局

RelativeLayout在复杂适配场景下容易出问题,改用ConstraintLayout可精准控制布局关系,确保Toolbar固定顶部、RecyclerView自适应高度:

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <data>
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/album_dp_20">

        <!-- Toolbar -->
        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginHorizontal="@dimen/dp20">

                <ImageView
                    android:id="@+id/back"
                    android:layout_width="24dp"
                    android:layout_height="24dp"
                    android:layout_centerVertical="true"
                    android:src="@drawable/backbutton" />

                <TextView
                    android:id="@+id/toolbar_title"
                    style="@style/Text_title_black_bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:text="@string/comment"
                    android:textSize="@dimen/dp20" />
            </RelativeLayout>
        </androidx.appcompat.widget.Toolbar>

        <!-- RecyclerView -->
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginTop="@dimen/dp_10"
            app:layout_constraintBottom_toTopOf="@id/bottomLL"
            app:layout_constraintTop_toBottomOf="@id/toolbar"
            tools:listitem="@layout/list_item_comments" />

        <!-- 底部回复布局 -->
        <LinearLayout
            android:id="@+id/bottomLL"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="@dimen/dp_32"
            android:layout_marginVertical="@dimen/album_dp_20"
            android:background="@drawable/et_background"
            android:backgroundTint="@color/gray"
            android:weightSum="2"
            app:layout_constraintBottom_toBottomOf="parent">

            <EditText
                android:id="@+id/commentsTV"
                style="@style/Text_title_gray_black_bold"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginStart="16dp"
                android:layout_weight="1.5"
                android:background="@null"
                android:hint="Write Message..."
                android:inputType="textAutoComplete"
                android:paddingVertical="@dimen/dp_10"
                android:textColor="#454545"
                android:textSize="@dimen/text_size_64px" />

            <androidx.appcompat.widget.AppCompatButton
                android:id="@+id/btn_buzz"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="0dp"
                android:layout_height="@dimen/dp60"
                android:layout_weight="0.5"
                android:background="@drawable/et_background"
                android:backgroundTint="@color/red"
                android:fontFamily="@font/segoeui"
                android:text="Post"
                android:textAllCaps="false"
                android:textColor="@color/white"
                android:textSize="@dimen/text_size_64px"
                android:textStyle="bold" />

        </LinearLayout>

        <!-- 加载进度 -->
        <RelativeLayout
            android:id="@+id/progress"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#6BE1E1D1"
            android:visibility="gone"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent">

            <com.github.ybq.android.spinkit.SpinKitView
                style="@style/SpinKitView.Large.WanderingCubes"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp75"
                android:layout_centerInParent="true"
                app:SpinKit_Color="@color/red_dark" />

        </RelativeLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

此布局中RecyclerView高度设为0dp,通过约束绑定Toolbar和底部布局,键盘弹出时会自动挤压RecyclerView高度,Toolbar保持固定。

内容的提问来源于stack exchange,提问作者cqlsys cql

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 13:24:59