Android: 以编程方式添加TextInputLayout

5
我正在尝试以编程方式将TextInputLayout与EditText添加到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);

然而,结果看起来非常有bug:

buggy view


奇怪的是,通过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>

通过xml工作视图


现在,需要补充说明的是,在升级到 Material 1.1 之前,编程方法是可行的。我仅使用Material组件。如何修复这个问题?


1
你确定需要以编程方式实现吗?这会使视图层逻辑更加复杂,应该避免以编程方式添加/删除视图。我会最初在XML中保留此文本输入布局,但在需要时将其从GONE更改为VISIBLE,或者交换片段等。 - Steyrix
就像@Steyrix所写的那样,在xml中将可见性属性设置为不可见,然后在代码中使用.setVisibility(View.VISIBLE)进行更改。 - zulu_papa
我非常清楚不应该通过Java创建UI组件,但在这种情况下是必需的。 - user123456789
2个回答

12

使用样式属性时,您需要使用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());

输出:

这里输入图像描述


4
太棒了!在我的情况下,这行代码 new TextInputEditText(textInputLayout.getContext()); 就是答案。非常感谢! - Umberto Covino
请您能否提供完整的代码作为您的回答。 - Learn2Code

1
我花了几个小时尝试实现同样的事情,当我即将放弃时,我发现了this Material Components GitHub issue。引用下面gabrielemariotti的回答,它适用于MaterialButton,但对TextInputLayout和可能的其他材料组件也适用。
如果要创建具有自定义样式的自定义按钮,则需要:
  • 在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) {
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接