以编程方式添加TextInputLayout

以编程方式添加TextInputLayout

如何以编程方式添加textinputlayout?
我试过了,

    private EditText _editText;
    private TextInputLayout _textInputLayout;

    // Create the EditText
    if (_editText == null) {
        _editText = new EditText(this.getContext());
        _editText.setInputType(
                _isPassword ? InputType.TYPE_TEXT_VARIATION_PASSWORD : InputType.TYPE_TEXT_FLAG_AUTO_CORRECT
        );
        _editText.setLayoutParams(
                new LayoutParams(0, LayoutParams.MATCH_PARENT)
        );

        if (_hint != null) {
            _editText.setHint(String.format("%s%s", HINT_PREFIX, _hint.toLowerCase()));
        }

        _textInputLayout = new TextInputLayout(this.getContext());
        _textInputLayout.setLayoutParams(
                new LayoutParams(0, LayoutParams.WRAP_CONTENT, EDIT_TEXT_WEIGHT)
        );
        _textInputLayout.addView(_editText);

        this.addView(_textInputLayout);
    }

但是,当我编译和运行时,会得到以下错误。我在这里做错什么了?
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:  Caused by: java.lang.ArrayIndexOutOfBoundsException: length=54; index=108
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at android.content.res.TypedArray.getValueAt(TypedArray.java:1132)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at android.content.res.TypedArray.getColor(TypedArray.java:446)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at android.support.design.widget.CollapsingTextHelper.setCollapsedTextAppearance(CollapsingTextHelper.java:191)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at android.support.design.widget.TextInputLayout.setHintTextAppearance(TextInputLayout.java:270)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at android.support.design.widget.TextInputLayout.<init>(TextInputLayout.java:111)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at android.support.design.widget.TextInputLayout.<init>(TextInputLayout.java:82)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at android.support.design.widget.TextInputLayout.<init>(TextInputLayout.java:78)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at com.syncopatesystems.splogle.iconfloatedittext.IconFloatEditText.initialize(IconFloatEditText.java:145)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at com.nixgadgets.iconfloatedittext.IconFloatEditText.<init>(IconFloatEditText.java:75)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at com.nixgadgets.iconfloatedittext.IconFloatEditText.<init>(IconFloatEditText.java:62)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at java.lang.reflect.Constructor.newInstance(Native Method) 
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at android.view.LayoutInflater.createView(LayoutInflater.java:619) 
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764) 
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:838) 
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:838) 
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393) 
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at android.app.Activity.setContentView(Activity.java:2166) 
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at com.syncopatesystems.splogle.activities.SignUpWithEmailActivity.onCreate(SignUpWithEmailActivity.java:43) 
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:6237) 
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148) 
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417) 
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

最佳答案

    EditText editText = new EditText(this);
    LinearLayout.LayoutParams editTextParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT);

    TextInputLayout textInputLayout = new TextInputLayout(this);
    LinearLayout.LayoutParams textInputLayoutParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);

    textInputLayout.setLayoutParams(textInputLayoutParams);
    textInputLayout.addView(editText, editTextParams);
    textInputLayout.setHint("hint");

    setContentView(textInputLayout);

10-08 18:19