Android TextInputLayout[提示在编辑字段内而不是在边框外]

问题描述 投票:0回答:1

我正在尝试在输入字段内创建带有提示的 TextInputLayout,如下所示:

这是我的代码,我尝试使用 OutLinedBox 和 Filled,如果我使用 Filled 并为布局设置背景,则提示的位置不正确

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_view_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/textValue"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="32dp"
        android:layout_marginRight="16dp"
        android:hint="label">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
android xml kotlin material-design
1个回答
0
投票

编辑答案

<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/text_input_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:layout_marginLeft="24dp"
        android:layout_marginRight="24dp"
        android:hint="Email"
        app:boxBackgroundColor="@android:color/transparent"
        app:boxStrokeWidth="0dp"
        app:boxStrokeWidthFocused="0dp">
        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/edit_text"
            android:background="@android:color/transparent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </com.google.android.material.textfield.TextInputLayout>

选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true">
        <shape android:shape="rectangle">
            <solid android:color="@android:color/white"/>
            <stroke android:width="1dp" android:color="?attr/colorPrimary"/>
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@android:color/white"/>
            <stroke android:width="1dp" android:color="?attr/colorControlNormal"/>
        </shape>
    </item>
</selector>

© www.soinside.com 2019 - 2024. All rights reserved.