英文:
how to prevent edit text from shrinking when keyboard opens?
问题
以下是您要翻译的内容:
I am a beginner in Android, I am trying to make a simple app where text in the edit text gets added to recycler view, but as soon as the Keyboard opens, edit text shrinks and content inside it is not visible
This is activty_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp" >
<EditText
android:hint="ADD ITEMS"
android:layout_margin="10dp"
android:id="@+id/etItems"
android:textSize="24sp"
android:textColor="@android:color/black"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />
<Button
android:layout_margin="10dp"
android:id="@+id/btnAdd"
android:text="ADD"
android:layout_weight="1"
android:autoSizeTextType="uniform"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvItems"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6" />
</LinearLayout>
This is MainActivity.java
package com.example.todolist_09082020;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<String> items;
Button btnAdd;
EditText etItems;
RecyclerView rvItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAdd = findViewById(R.id.btnAdd);
etItems = findViewById(R.id.etItems);
rvItems = findViewById(R.id.rvItems);
items = new ArrayList<>();
final ItemAdapter adapter = new ItemAdapter(items);
rvItems.setLayoutManager(new LinearLayoutManager(this));
rvItems.setAdapter(adapter);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String text = etItems.getText().toString();
if(!text.equals("")){
items.add(text);
adapter.notifyDataSetChanged();
}
etItems.setText("");
}
});
}
}
I tried solutions from other posts related to this, but this could not be solved.
英文:
I am a beginner in Android, I am trying to make a simple app where text in the edit text gets added to recycler view, but as soon as the Keyboard opens, edit text shrinks and content inside it is not visible
This is activty_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp" >
<EditText
android:hint="ADD ITEMS"
android:layout_margin="10dp"
android:id="@+id/etItems"
android:textSize="24sp"
android:textColor="@android:color/black"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />
<Button
android:layout_margin="10dp"
android:id="@+id/btnAdd"
android:text="ADD"
android:layout_weight="1"
android:autoSizeTextType="uniform"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvItems"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6" />
</LinearLayout>
This is MainActivity.Java
package com.example.todolist_09082020;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<String> items;
Button btnAdd;
EditText etItems;
RecyclerView rvItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAdd = findViewById(R.id.btnAdd);
etItems = findViewById(R.id.etItems);
rvItems = findViewById(R.id.rvItems);
items = new ArrayList<>();
final ItemAdapter adapter = new ItemAdapter(items);
rvItems.setLayoutManager(new LinearLayoutManager(this));
rvItems.setAdapter(adapter);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String text = etItems.getText().toString();
if(!text.equals("")){
items.add(text);
adapter.notifyDataSetChanged();
}
etItems.setText("");
}
});
}
}
I tried solutions from other posts related to this, but this could not be solved.
答案1
得分: 0
将EditText和其父LinearLayout的layout_height
更改为wrap_content
。这应该会解决您的问题。
英文:
Change layout_height
of EditText and its parent LinearLayout to wrap_content
. It should solve your issue
答案2
得分: 0
你的问题是线性布局中文本和按钮高度的设置。有几种方法可以解决,但保持它不会折叠的最简单方式是将
android:layout_height="0dp"
改为
android:layout_height="wrap_content"
如果你还将文本和按钮的高度改为wrap_content
,并为按钮文本选择一个固定的字体大小(例如24sp
),可能会看到更好的效果。
英文:
Your problem is the linear layout holding the text and button height. There's several things you can do, but a minimal way to keep it from collapsing is to change
android:layout_height="0dp"
to
android:layout_height="wrap_content"
You'll probably also see nicer results if you additionally change the text and button heights to wrap_content
and choose a fixed font size (24sp
looked nice) for your button text.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论