android - 在背景边框的左上角带有提示的圆角矩形背景编辑文本

标签 android xml android-layout android-edittext

我在创建 UI 时遇到了困难。请帮助我创建它。

我有一个带有圆角矩形边框的 EditText,这没问题,但占位符位于边框顶部,离开边框。

这是图片

edit text with border

提前致谢。

最佳答案

你必须使用 Vector Drawable用于根据需要创建自定义形状。

我已经为此创建了一个示例。 在 res/drawable/

下创建 custom_vector.xml 文件
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:height="20dp"
    android:width="20dp"
    android:viewportWidth="400"
    android:viewportHeight="400">

    <group
        android:pivotX="10.0"
        android:pivotY="10.0">

        <!-- the outside box -->

        <!-- top line & top left corner -->
        <path android:pathData="M 30 60 H 40 c -40 0 -35 0 -35 35 "
            android:strokeColor="#000000" android:strokeWidth="10" />

        <!-- left line & bottom left corner -->
        <path android:pathData="M 5 64 v 271 c 0 40 0 35 35 35 "
            android:strokeColor="#000000" android:strokeWidth="10" />

        <!-- bottom line & bottom right corner -->
        <path android:pathData="M 30 370 h 330 c 40 0 35 -10 35 -35"
            android:strokeColor="#000000" android:strokeWidth="10" />

        <!-- right line & top right corner -->
        <path android:pathData="M 395 356 v -261 c0 -40 0 -35 -50 -35"
            android:strokeColor="#000000" android:strokeWidth="10" />

        <!-- top line till end-->
        <!-- 140 is the starting point of line after TEXT-->
        <path android:pathData="M 140 60 370 60"
            android:strokeColor="#000000" android:strokeWidth="10" />
    </group>
</vector>

您可能必须更改最后一个路径标记的值

top line till end

根据您在“姓名” 处的文字。您还可以根据需要修改形状或角。

res/layout/下创建一个test_layout.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#faffd5">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/custom_vector"
        android:layout_margin="20dp"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="40dp"
            android:paddingLeft="5dp"
            android:textSize="20sp"
            android:paddingRight="5dp"
            android:text="Name"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="40dp"
            android:layout_marginRight="20dp"
            android:layout_marginBottom="15dp"
            android:textSize="25sp"
            android:background="@android:color/transparent"
            android:text="Alex Smith"/>

    </LinearLayout>
</LinearLayout>

看起来像这个截图

enter image description here

关于android - 在背景边框的左上角带有提示的圆角矩形背景编辑文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46564749/

相关文章:

android - 如何在 firebase 消息服务中添加 View ?

java - Eclipse 中的 log4j.xml

android - 如何使用复合可绘制对象而不是包含 ImageView 和 TextView 的 LinearLayout

java - 找不到类错误膨胀类 android.support.design.widget.BottomNavigationView

java - android数据库的交换id

java - 上传图片后在 Firebase 中如何获取 Url?

java - Android - GregorianCalendar 显示错误的月份

java - 将谷歌地图( fragment ?)添加到 Activity

sql-server - SQL Server 2008 - 将 XML 声明添加到 XML 输出

android - 使 Android 布局可滚动