0

我需要子类TextInputLayout化以提供自定义行为。然而,即使是空的主体,布局也会从某个地方选择不同的主题。如果我能够覆盖主题,我不会太担心,但这个主题只是直接坏了,例如错误可绘制不会显示。

香草布局:

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/login_username_input_layoutz"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/login_username_hint">

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

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

在此处输入图像描述


子类布局:

import android.content.Context
import android.util.AttributeSet
import com.google.android.material.textfield.TextInputLayout

class CustomTextInputLayout @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : TextInputLayout(context, attrs, defStyleAttr) {}

            <somepackage.CustomTextInputLayout
                android:id="@+id/login_username_input_layoutz"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/login_username_hint">

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

            </somepackage.CustomTextInputLayout >

在此处输入图像描述


材料设计导入:com.google.android.material:material:1.2.0

父应用主题:Theme.MaterialComponents.Light.NoActionBar

4

1 回答 1

4

在构造函数中使用defStyleAttr: Int = R.attr.textInputStyle而不是defStyleAttr: Int = 0

于 2020-08-14T08:08:35.667 回答