如何在自定义TextInputLayout中的浮动提示和输入文本之间添加不同的填充?

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

我正在我的 Android 项目中开发自定义 TextInputLayout。我需要区分浮动提示(标签)和输入文本之间的填充。当提示浮动(充当标签)时,它应具有 0dp 的填充,但输入文本应具有 12dp 的填充。这是我想要实现的目标的视觉表示:

enter image description here

我尝试为提示和输入文本设置不同的填充,但两者最终都具有相同的填充。任何有关如何实现这一目标的指导将不胜感激。谢谢!

android android-layout android-textinputlayout android-textinputedittext
1个回答
0
投票

您要求做的事情似乎不是 API 的一部分。但是,您可以通过向 TextInputEditText 添加清晰的开始绘制来获得所需的效果。

<shape 
    android:shape="rectangle">
    <solid android:color="@android:color/transparent" />
    <size
        android:width="12dp"
        android:height="0dp" />
</shape>

<androidx.appcompat.widget.LinearLayoutCompat
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="This is a hint"
        android:paddingTop="16dp">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:drawableStart="@drawable/square"
            android:paddingStart="0dp"
            android:textColor="@color/black" />

    </com.google.android.material.textfield.TextInputLayout>

    <EditText
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp" />
</androidx.appcompat.widget.LinearLayoutCompat>

开始绘制会将输入的文本

12dp
推到右侧,而提示将忽略它。

enter image description here

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