如何创建一个带有圆角末端背景的EditText。

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

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.

如何创建一个带有圆角末端背景的EditText。

答案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>

结果:

如何创建一个带有圆角末端背景的EditText。

祝你好运!

英文:

First, add a new drawable file with name "background.xml" inside your project's drawable folder and write these inside it:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;shape xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:padding=&quot;10dp&quot;
    android:shape=&quot;rectangle&quot;&gt;
    &lt;solid android:color=&quot;#f1f4f9&quot; /&gt;
    &lt;stroke android:width=&quot;1dp&quot; android:color=&quot;#f1f4f9&quot; /&gt;
    &lt;corners
        android:radius=&quot;24dp&quot; /&gt;
&lt;/shape&gt;

Then in activity_main.xml :

&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;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;&gt;

    &lt;EditText
        android:id=&quot;@+id/editText&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_margin=&quot;20dp&quot;
        android:background=&quot;@drawable/background&quot;
        android:hint=&quot;Your full name&quot;
        android:padding=&quot;15dp&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;

&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

Result:

如何创建一个带有圆角末端背景的EditText。

Good luck!

答案2

得分: 0

使用EditText中的左填充

android:leftpadding="20dp"

英文:

Use left padding in edittext

android:leftpadding="20dp"

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

发表评论

匿名网友

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

确定