Android滚动视图覆盖其下方的其他视图

huangapple go评论78阅读模式
英文:

Android Scroll View Overlaps other Views below it

问题

<?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>
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.LinearLayout;
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);
    }
}

Problem:
当我发送太多消息时,消息(即 TextView)开始穿过我的 EditText 小部件。
我甚至尝试了将 Scroll View 的高度设置为 wrap-content,但没有起作用。我还尝试了为 Scroll View 以及 Scroll View 内部的 LinearLayout 设置重力为 end 和 bottom。

问题相关的图片:

点击此处查看图片

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

英文:

I have recently started learning Android development and wanted to make an app which adds a message, as you give text input in the EditText widget. My activity's top part is containing the scroll view and below it is the EditText view and a button for sending the message. This is the respective xml Code for the Activity:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:background=&quot;#A8A8A8&quot;
tools:context=&quot;.chatpage&quot;&gt;
&lt;ScrollView
android:id=&quot;@+id/scrollChat&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;0dp&quot;
android:layout_marginTop=&quot;68dp&quot;
android:layout_weight=&quot;1&quot;
android:fillViewport=&quot;false&quot;
android:scrollbarStyle=&quot;insideOverlay&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.0&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;&gt;
&lt;LinearLayout
android:id=&quot;@+id/chatSpace&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;10dp&quot;
android:background=&quot;#D6EDBB&quot;
android:gravity=&quot;end&quot;
android:orientation=&quot;vertical&quot;&gt;
&lt;/LinearLayout&gt;
&lt;/ScrollView&gt;
&lt;Button
android:id=&quot;@+id/button&quot;
android:layout_width=&quot;79dp&quot;
android:layout_height=&quot;56dp&quot;
android:layout_marginBottom=&quot;24dp&quot;
android:layout_marginLeft=&quot;12dp&quot;
android:layout_marginStart=&quot;12dp&quot;
android:onClick=&quot;sendMessage&quot;
android:text=&quot;@string/send&quot;
app:backgroundTint=&quot;#36FF00&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintStart_toEndOf=&quot;@+id/chatMessage&quot; /&gt;
&lt;EditText
android:id=&quot;@+id/chatMessage&quot;
android:layout_width=&quot;278dp&quot;
android:layout_height=&quot;44dp&quot;
android:layout_marginBottom=&quot;28dp&quot;
android:ems=&quot;10&quot;
android:hint=&quot;Enter a message&quot;
android:inputType=&quot;textPersonName&quot;
android:shadowColor=&quot;#CC9999&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.12&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

And this is my Java code:

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(&quot;NICKNAME&quot;);
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 + &quot;\n&quot; + 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);
}
}

Problem:
When i send too many messages, the messages (which is a TextView) starts going through my EditText Widget.
i have even tried wrap-content for the Scroll View yet it didn't work. I have tried setting the gravity to end, bottom for scroll view as well as for the linear Layout which is inside the scroll view.

Image regarding the problem:

Click here for the image

Thank you for your time. Please help me regarding this issue.

答案1

得分: 0

试试这个:

<?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>
英文:

Try this:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:background=&quot;#A8A8A8&quot;
tools:context=&quot;.chatpage&quot;&gt;
&lt;ScrollView
android:id=&quot;@+id/scrollChat&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;0dp&quot;
android:layout_marginTop=&quot;68dp&quot;
android:layout_weight=&quot;1&quot;
android:fillViewport=&quot;false&quot;
android:scrollbarStyle=&quot;insideOverlay&quot;
app:layout_constraintEnd_toEndOf=&quot;@+id/button&quot;
app:layout_constraintHorizontal_bias=&quot;0.0&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;&gt;
&lt;LinearLayout
android:id=&quot;@+id/chatSpace&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;10dp&quot;
android:background=&quot;#D6EDBB&quot;
android:gravity=&quot;end&quot;
android:orientation=&quot;vertical&quot;&gt;
&lt;/LinearLayout&gt;
&lt;/ScrollView&gt;
&lt;Button
android:id=&quot;@+id/button&quot;
android:layout_width=&quot;79dp&quot;
android:layout_height=&quot;56dp&quot;
android:layout_marginBottom=&quot;24dp&quot;
android:layout_marginLeft=&quot;12dp&quot;
android:layout_marginStart=&quot;12dp&quot;
android:onClick=&quot;sendMessage&quot;
android:text=&quot;@string/send&quot;
app:backgroundTint=&quot;#36FF00&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintStart_toEndOf=&quot;@+id/chatMessage&quot; /&gt;
&lt;EditText
android:id=&quot;@+id/chatMessage&quot;
android:layout_width=&quot;278dp&quot;
android:layout_height=&quot;44dp&quot;
android:layout_marginBottom=&quot;28dp&quot;
android:ems=&quot;10&quot;
android:hint=&quot;Enter a message&quot;
android:inputType=&quot;textPersonName&quot;
android:shadowColor=&quot;#CC9999&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.12&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

答案2

得分: 0

给滚动视图添加底部约束。
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">
英文:

Add bottom constraints to scroll view.
layout_constraintBottom_toTopOf

Like that:

  &lt;ScrollView
android:id=&quot;@+id/scrollChat&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;0dp&quot;
android:layout_marginTop=&quot;68dp&quot;
android:layout_weight=&quot;1&quot;
android:fillViewport=&quot;false&quot;
android:scrollbarStyle=&quot;insideOverlay&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.0&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintBottom_toTopOf=&quot;@+id/chatMessage&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;&gt;

huangapple
  • 本文由 发表于 2020年10月19日 01:49:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/64416480.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定