【问题标题】:Rounded rectangle background editext with hint in left top of background border背景边框左上角带有提示的圆角矩形背景编辑文本
【发布时间】:2018-03-15 20:10:28
【问题描述】:

我陷入了创建 UI 的困境。请帮我创建它。

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

这是图片

提前致谢。

【问题讨论】:

  • 创建一个由edittext和textview组成的相对布局,然后使用您的可绘制背景到特定布局并添加文本和edittext提示
  • @ysl 我希望背景是透明的,因为我在屏幕上有背景图像。
  • 那么不要为你的drawable设置背景颜色
  • 如果背景线穿过用作提示的文本视图

标签: android xml android-layout android-edittext


【解决方案1】:

您必须使用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>

您可能需要更改最后一个路径标签的值

顶行到最后

根据您在 'Name' 处的文字。您还可以根据需要修改形状或角。

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>

看起来像这个截图

【讨论】:

    【解决方案2】:

    试试下面...

    您可以使用ConstraintLayout 实现此目的。

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 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:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:background="@android:color/white"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <EditText
            android:id="@+id/edittext"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/_10sdp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:background="@drawable/round"
            android:padding="@dimen/_10sdp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:padding="@dimen/_5sdp"
            android:text="Name"
            app:layout_constraintLeft_toLeftOf="@+id/edittext"
            android:layout_marginLeft="@dimen/_15sdp"
            app:layout_constraintTop_toTopOf="@+id/edittext"
            app:layout_constraintBottom_toTopOf="@+id/edittext"
            android:textColor="@android:color/black"
            tools:layout_editor_absoluteX="107dp"
            tools:layout_editor_absoluteY="90dp" />
    </android.support.constraint.ConstraintLayout>
    

    round.xml

    <?xml version="1.0" encoding="utf-8"?>
    <!--  res/drawable/rounded_edittext.xml -->
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" android:padding="10dp">
        <solid android:color="#FFFFFF"/>
        <stroke android:color="@android:color/darker_gray" android:width="@dimen/_1sdp"/>
        <corners
            android:bottomRightRadius="15dp"
            android:bottomLeftRadius="15dp"
            android:topLeftRadius="15dp"
            android:topRightRadius="15dp"/>
    </shape>
    

    输出:see

    【讨论】:

    • 谢谢,但我在屏幕周围都有背景图像,所以我希望用户界面透明,这样背景图像就不会影响
    • 明白你的意思...如果我找到你喜欢的东西,我会更新你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-18
    • 1970-01-01
    • 2011-05-16
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    相关资源
    最近更新 更多