首页 > 解决方案 > 软键盘:Adjust_Resize 将内容推送到屏幕外

问题描述

我正在构建一个类似应用程序的消息传递,但是我在键盘推送内容时遇到了一些问题。

我想要的是将内容向上推到与键盘高度一样多的程度,并且让我能够自由地向上滚动以查看所有以前的消息。就像大多数消息传递应用程序一样。

这是我的布局。我将布局添加到滚动视图内的布局中。

<?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/open_messsages_constraint"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".OpenMessages">


    <ScrollView
        android:id="@+id/messages_scroll2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toTopOf="@id/linearLayout">


        <LinearLayout
            android:id="@+id/allMessage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical"
            app:layout_constraintTop_toTopOf="parent">

        </LinearLayout>


    </ScrollView>


    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#ffffff"
        android:backgroundTint="#ffffff"
        app:layout_constraintBottom_toBottomOf="parent">

        <EditText
            android:id="@+id/messageInput"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_gravity="center"
            android:layout_marginLeft="20dp"
            android:layout_weight="4"
            android:background="@drawable/rounded_corner"
            android:backgroundTint="#f1f1f1"
            android:paddingLeft="20dp"
            android:textColorLink="#000000" />


        <com.google.android.material.button.MaterialButton
            android:id="@+id/sendMessageButton"
            android:layout_width="50dp"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:backgroundTint="#ffffff"
            android:gravity="center"
            android:stateListAnimator="@null"
            app:iconTint="#000000">

        </com.google.android.material.button.MaterialButton>
    </LinearLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

这是点击键盘之前: 在我选择键盘之前。 这是之后。我可以向下滚动以查看下面的消息,但我无法向上滚动通过这张图片,所以我错过了一条消息。 在我点击键盘之后。

标签: javaandroidandroid-studiokotlin

解决方案


将您的LinearLayoutfor替换为allMessage以下内容:

<LinearLayout
            android:id="@+id/allMessage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical"
            android:layout_marginTop="50dp" // Add this line to view last messages
            app:layout_constraintTop_toTopOf="parent">

        </LinearLayout>

推荐阅读