0
我在主要活動中創建了一個佈局,現在我正試圖調用此方法,我需要在我的主佈局中添加此視圖。它的行爲不像TextInputLayout
表現的那樣。如何在android中以編程方式添加autocompletetext
請檢查我的代碼,讓我知道我應該改變什麼。
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;
}
我也這麼做了。仍然沒有按預期正常工作。 –
TextInputLayout.LayoutParams textInputPosition = new TextInputLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT); textInputLayout.addView(autoCompleteTextView,textInputPosition); –
我總是在編程或使用xml加載時遇到此錯誤:「您需要在設計庫中使用Theme.AppCompat主題(或後代)。」 –