以编程方式添加的TextInputLayout不显示大纲框

问题描述 投票:0回答:1

以编程方式不显示大纲框的情况下添加带有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'

请更正我在这里缺少的内容吗?

用TextInputEditText添加TextInputLayout以编程方式不显示大纲框,我想在片段中显示一些可编辑的字段。如果我使用下面的xml,则显示的片段是...

android material-design android-styles android-textinputlayout programmatically-created
1个回答
1
投票

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

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
© www.soinside.com 2019 - 2024. All rights reserved.