Android: 以编程方式添加 TextInputLayout

Android: Programmatically adding TextInputLayout

我正在尝试以编程方式将带有 EditText 的 TextInputLayout 添加到 LinearLayout。我的做法:

TextInputLayout textInputLayout = new TextInputLayout(new ContextThemeWrapper(getContext(), R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox));
textInputLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
textInputLayout.setHintTextAppearance(R.style.Base_Widget_MaterialComponents_TextInputLayout_TextInputLayout);

TextInputEditText editText = new TextInputEditText(getContext());
editText.setHint("test");

textInputLayout.addView(editText, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayout.addView(textInputLayout);

但是,结果看起来非常错误:


奇怪的是,通过 XML 添加相同的 TextInputLayout 有效:

<com.google.android.material.textfield.TextInputLayout
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="kommentar"
    app:hintTextAppearance="@style/Base.Widget.MaterialComponents.TextInputLayout.TextInputLayout">

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


现在,我需要补充一点,在升级到 Material 1.1 之前,编程方法是有效的。我只使用 material 组件。我该如何解决这个问题?

使用样式属性你必须使用setBoxBackgroundMode()方法来使用OutlineBox样式。除此之外,您应该使用 TextInputLayoutcontext 来创建 TextInputEditText。检查以下内容:

textInputLayout.setBoxBackgroundColor(ContextCompat.getColor(getContext(), android.R.color.white));
textInputLayout.setBoxBackgroundMode(TextInputLayout.BOX_BACKGROUND_OUTLINE);

//Must use context of textInputLayout
TextInputEditText editText = new TextInputEditText(textInputLayout.getContext());

输出:

我花了几个小时试图实现同样的目标,当我准备放弃时,我发现了 this Material Components GitHub issue。下面引用 gabrielemariotti 的回答,这是针对 MaterialButton,但同样适用于 TextInputLayout,可能还有其他 Material 个组件。

如果您想创建具有自定义样式的自定义按钮,您必须:

  • 在attrs.xml中定义一个属性 <attr name="myButtonStyle" format="reference"/>

  • 在您的应用主题中为这个新属性分配一个值:

    <style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
        <item name="myButtonStyle">@style/AccentButtonStyle</item>
    </style>

与:

    <style name="AccentButtonStyle" parent="Widget.MaterialComponents.Button">
        <item name="backgroundTint">@color/...</item>
        <item name="icon">@drawable/....</item>
        <item name="iconTint">@color/....</item>
    </style>

然后使用这个构造函数:

class AccentButton @JvmOverloads constructor(
        context: Context,
        attrs: AttributeSet? = null,
        @AttrRes defStyleAttr: Int = R.attr.myButtonStyle
  ) : MaterialButton(context, attrs, defStyleAttr) {
}