英文:
How to create an EditText with a background with rounded ends
问题
如何创建一个类似这样的 EditText?我尝试了很多次,但光标总是在圆角外面。
英文:
How do I create an EditText like this? I tried many times but the cursor is always outside the round corners.
答案1
得分: 2
首先,在项目的drawable文件夹中添加一个名为 "background.xml" 的新可绘制文件,并在其中编写以下内容:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle">
<solid android:color="#f1f4f9" />
<stroke android:width="1dp" android:color="#f1f4f9" />
<corners
android:radius="24dp" />
</shape>
然后在 activity_main.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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="@drawable/background"
android:hint="Your full name"
android:padding="15dp"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
结果:
祝你好运!
英文:
First, add a new drawable file with name "background.xml" inside your project's drawable folder and write these inside it:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle">
<solid android:color="#f1f4f9" />
<stroke android:width="1dp" android:color="#f1f4f9" />
<corners
android:radius="24dp" />
</shape>
Then in activity_main.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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="@drawable/background"
android:hint="Your full name"
android:padding="15dp"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Result:
Good luck!
答案2
得分: 0
使用EditText中的左填充
android:leftpadding="20dp"
英文:
Use left padding in edittext
android:leftpadding="20dp"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论