英文:
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:
<?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>
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("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:
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:
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:
<?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>
答案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:
<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">
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论