【问题标题】:How to create an EditText with a background with rounded ends [closed]如何创建带有圆角背景的 EditText [关闭]
【发布时间】:2023-03-19 05:46:01
【问题描述】:

如何创建这样的 EditText?我试了很多次,但光标总是在圆角之外。

【问题讨论】:

  • 检查 TextInputLayout

标签: java android xml layout android-edittext


【解决方案1】:

首先,在项目的可绘制文件夹中添加一个名为“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>

结果:

祝你好运!

【讨论】:

    【解决方案2】:

    在编辑文本中使用左填充

    android:leftpadding="20dp"

    【讨论】:

      猜你喜欢
      • 2011-04-08
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 2020-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多