Android 滚动视图与其下方的其他视图重叠

Android Scroll View Overlaps other Views below it

我最近开始学习 Android 开发,并想制作一个应用程序,当您在 EditText 小部件中输入文本时,它会添加一条消息。我的 activity 的顶部包含滚动视图,下面是 EditText 视图和一个用于发送消息的按钮。这是 Activity 的相应 xml 代码:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#A8A8A8"
    tools:context=".chatpage">

    <ScrollView
        android:id="@+id/scrollChat"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="68dp"
        android:layout_weight="1"
        android:fillViewport="false"
        android:scrollbarStyle="insideOverlay"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:id="@+id/chatSpace"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:background="#D6EDBB"
            android:gravity="end"
            android:orientation="vertical">

        </LinearLayout>
    </ScrollView>

    <Button
        android:id="@+id/button"
        android:layout_width="79dp"
        android:layout_height="56dp"
        android:layout_marginBottom="24dp"
        android:layout_marginLeft="12dp"
        android:layout_marginStart="12dp"
        android:onClick="sendMessage"
        android:text="@string/send"
        app:backgroundTint="#36FF00"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/chatMessage" />

    <EditText
        android:id="@+id/chatMessage"
        android:layout_width="278dp"
        android:layout_height="44dp"
        android:layout_marginBottom="28dp"
        android:ems="10"
        android:hint="Enter a message"
        android:inputType="textPersonName"
        android:shadowColor="#CC9999"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.12"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

这是我的 Java 代码:

package com.example.solochat;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class chatpage extends AppCompatActivity {

    LinearLayout chatSpace;
    EditText chat_input;
    String nickname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chatpage);
        nickname = getIntent().getStringExtra("NICKNAME");
        chatSpace = findViewById(R.id.chatSpace);
        chat_input = findViewById(R.id.chatMessage);

    }

    public void sendMessage(View btn)
    {
        TextView msg = new TextView(this);
        String message = chat_input.getText().toString();
        message = nickname + "\n" + message;
        msg.setText(message);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
        );
                params.gravity = Gravity.END;
                params.setMargins(0, 20, 55, 10);
        msg.setLayoutParams(params);
        chatSpace.addView(msg);

    }
}

问题: 当我发送太多消息时,消息(它是一个 TextView)开始通过我的 EditText 小部件。 我什至尝试过滚动视图的 wrap-content 但它没有用。我已经尝试将重力设置为结束,滚动视图的底部以及滚动视图内的线性布局。

关于问题的图片:

Click here for the image

感谢您的宝贵时间。请帮我解决这个问题。

试试这个:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#A8A8A8"
    tools:context=".chatpage">

    <ScrollView
        android:id="@+id/scrollChat"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="68dp"
        android:layout_weight="1"
        android:fillViewport="false"
        android:scrollbarStyle="insideOverlay"
        app:layout_constraintEnd_toEndOf="@+id/button"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:id="@+id/chatSpace"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:background="#D6EDBB"
            android:gravity="end"
            android:orientation="vertical">

        </LinearLayout>
    </ScrollView>

    <Button
        android:id="@+id/button"
        android:layout_width="79dp"
        android:layout_height="56dp"
        android:layout_marginBottom="24dp"
        android:layout_marginLeft="12dp"
        android:layout_marginStart="12dp"
        android:onClick="sendMessage"
        android:text="@string/send"
        app:backgroundTint="#36FF00"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/chatMessage" />

    <EditText
        android:id="@+id/chatMessage"
        android:layout_width="278dp"
        android:layout_height="44dp"
        android:layout_marginBottom="28dp"
        android:ems="10"
        android:hint="Enter a message"
        android:inputType="textPersonName"
        android:shadowColor="#CC9999"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.12"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

为滚动视图添加底部约束。 layout_constraintBottom_toTopOf

像那样:

  <ScrollView
        android:id="@+id/scrollChat"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="68dp"
        android:layout_weight="1"
        android:fillViewport="false"
        android:scrollbarStyle="insideOverlay"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/chatMessage"
        app:layout_constraintTop_toTopOf="parent">