如何在android中以编程方式在textinputlayout中添加autocompletetext

时间:2017-04-07 11:30:16

标签: android autocompletetextview android-textinputlayout

我在主活动中创建了一个布局,现在我正在尝试调用此方法,我需要在主布局中添加此视图。它的行为与TextInputLayout行为不同。

请检查我的代码并告诉我应该更改的内容。

public static View getTypeAHeadView(Context context) {

    RelativeLayout layout = new RelativeLayout(context);
    try {
        RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        layout.setLayoutParams(lp1);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.setMargins(0, 5, 0, 0);
        TextInputLayout textInputLayout = new TextInputLayout(context);
        AutoCompleteTextView autoCompleteTextView = new AutoCompleteTextView(context);
        autoCompleteTextView.setHeight(R.dimen.dim_40);
        autoCompleteTextView.setWidth(LinearLayoutCompat.LayoutParams.MATCH_PARENT);
        autoCompleteTextView.setTextSize(R.dimen.dim_20_sp);
        autoCompleteTextView.setLines(1);
        autoCompleteTextView.setDropDownHeight(R.dimen.dim_200);
        autoCompleteTextView.setKeyListener(DigitsKeyListener.getInstance("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,"));
        autoCompleteTextView.setPadding(10, 0, 0, 0);
        autoCompleteTextView.setSingleLine(true);
        autoCompleteTextView.setLayoutParams(lp);

        textInputLayout.addView(autoCompleteTextView);

        layout.addView(textInputLayout);
    } catch (Exception e) {
        e.printStackTrace();
    }
  /*parent.addView(text);
    parent.addView(editText);*/
    return layout;
}

1 个答案:

答案 0 :(得分:0)

我不完全确定,但也许尝试为您以编程方式添加的TextInputLayout定义布局参数。