如何在 Material 文本字段的开头插入图标

How to insert an icon in the beginning of a Material Text Field

我正在尝试在 Material 文本字段的开头插入一个图标。我在 material.io 的文档中也找不到合适的内容。

这就是我想要达到的目标。

使用drawableStart属性:

https://developer.android.com/reference/android/widget/TextView.html#attr_android:drawableStart

https://developer.android.com/reference/android/widget/TextView.html#setCompoundDrawablesRelativeWithIntrinsicBounds(int,%20int,%20int,%20int)

使用 TextInputLayoutapp:startIconDrawable

<com.google.android.material.textfield.TextInputLayout
    android:hint="Select Time"
    app:startIconDrawable="@drawable/ic_add_24px"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">

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

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

注意:需要1.1.0版本的库。

第 1 步:复制所需的文本字段

Material 文本字段文档:https://material.io/components/text-fields/android#using-text-fields

第 2 步:select 文本字段 -> 搜索“draw” -> 单击 drawableLeft 图标

第 3 步:Select 或添加图标(我将 select 矢量图标)

Select 完成前的图标

Select 图标并单击“确定”

完成:)