【问题标题】:Programmatically added TextInputLayout not showing the Outline Box以编程方式添加的 TextInputLayout 不显示大纲框
【发布时间】:2020-05-19 21:02:07
【问题描述】:

以编程方式使用 TextInputEditText 添加 TextInputLayout 不显示大纲框

我想在片段中显示一些可编辑的字段。如果我使用下面的 xml,那么我的片段会按预期显示带有框轮廓的 TextInputLayout

sample_layout.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/clParent"
    android:theme="@style/Theme.MaterialComponents.Light.DarkActionBar">

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/glvHoldLabelTop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent=".05"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/glhHoldLabelStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.05" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/glhHoldLabelEnd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.95" />

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/tilUsername"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:hint="Username"
        app:layout_constraintEnd_toEndOf="@id/glhHoldLabelEnd"
        app:layout_constraintStart_toStartOf="@id/glhHoldLabelStart"
        app:layout_constraintTop_toTopOf="@id/glvHoldLabelTop">

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

</androidx.constraintlayout.widget.ConstraintLayout>

这是 sample_layout.xml TextInputLayout from XML 的输出


但是当我尝试通过程序做同样的事情时,它没有显示大纲框并且TextInputLayout使用的主题也不同

代码:

        TextInputLayout tilUsername = new TextInputLayout(getActivity(), null, R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox);


        TextInputEditText edtUsername = new TextInputEditText(getActivity());
        tilUsername.addView(edtUsername);

//        Set Layout parameters
        ConstraintLayout.LayoutParams clpTextInputLayout = new ConstraintLayout.LayoutParams(
                ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
        clpTextInputLayout.topToTop = glvHoldLabelTop.getId();
        clpTextInputLayout.topMargin = 50;
        clpTextInputLayout.setMarginStart(100);
        clpTextInputLayout.setMarginEnd(100);
        tilUsername.setLayoutParams(clpTextInputLayout);
        tilUsername.setHint("Username");
        tilUsername.setBoxBackgroundMode(TextInputLayout.BOX_BACKGROUND_OUTLINE);
        tilUsername.setBoxCornerRadii(5, 5, 5, 5);

//        Add TextInputlayout to ConstraintLayout
        clParent.addView(tilUsername);

这是代码的输出: TextInputLayout from Code

注意: Build.gradle(:app) 添加了以下依赖项

实现'com.google.android.material:material:1.2.0-alpha06'

请更正我在这里遗漏的内容?

【问题讨论】:

  • @MikeM。 setBoxBackgroundMode 调用已经存在。在此answer 中,输出 TextInputLayout 具有框轮廓。我遵循相同的方法,但我得到的结果不同
  • 哦,哎呀。抱歉,我不知道我之前是怎么错过的。

标签: android material-design android-styles android-textinputlayout programmatically-created


【解决方案1】:

我终于弄清楚是什么导致了这个问题。在style.xml 文件中,将AppTheme 样式的parent 属性从

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">

显示大纲框。现在我可以在 TextInputLayout 中看到预期的输出。认为这会帮助面临同样问题的人。但我仍然不确定更改 AppTheme 的影响。欢迎提出建议!

编辑:

我还必须修改初始化 TextInputLayout 并将其添加到父布局的方法。以下是我的发现,

    TextInputLayout tilUsername = new TextInputLayout(new ContextThemeWrapper(getContext(), R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox));

//    Set Box related configurations first before adding TextInputEditText to the TextInputLayout
    tilUsername.setBoxBackgroundMode(TextInputLayout.BOX_BACKGROUND_OUTLINE);
    tilUsername.setBoxBackgroundColor(ContextCompat.getColor(tilUsername.getContext(), android.R.color.transparent));
    tilUsername.setHint("Username");

//    Set Layout parameters
    ConstraintLayout.LayoutParams clpTextInputLayout = new ConstraintLayout.LayoutParams(
            ConstraintLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    tilUsername.setLayoutParams(clpTextInputLayout);
    clpTextInputLayout.topToTop = glvHoldLabelTop.getId();
    clpTextInputLayout.topMargin = 50;
    clpTextInputLayout.setMarginStart(100);
    clpTextInputLayout.setMarginEnd(100);

//    Add TextInputEditText to TextInputLayout
    TextInputEditText edtUsername = new TextInputEditText(tilUsername.getContext());
    tilUsername.addView(edtUsername);

//    Call this line after adding TextInputEditText to the TextInputLayout to get rid error 'float com.google.android.material.shape.MaterialShapeDrawable.getTopLeftCornerResolvedSize()' on a null object reference'
    tilUsername.setBoxCornerRadii(50, 50, 50, 50);


//    Add TextInputlayout to ConstraintLayout
    clParent.addView(tilUsername);

这是最终输出:

【讨论】:

  • 是的,每当您使用 Material Components 时,您都必须这样做,我只是检查了图像,发现以编程方式添加的那个是库存的,而不是材料的,这显然意味着它是没有得到 OutLined 材料主题。很好,你已经自己弄清楚了。删除了反对票。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-07
相关资源
最近更新 更多