要解决AutoCompleteTextView的下拉视图显示宽度错误,可以尝试以下方法:
AutoCompleteTextView autoCompleteTextView = findViewById(R.id.autoCompleteTextView);
CustomAdapter adapter = new CustomAdapter(this, android.R.layout.simple_dropdown_item_1line, dataList);
autoCompleteTextView.setAdapter(adapter);
// 自定义适配器
public class CustomAdapter extends ArrayAdapter {
public CustomAdapter(Context context, int resource, List objects) {
super(context, resource, objects);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
layoutParams.width = /* 设置合适的宽度 */;
view.setLayoutParams(layoutParams);
}
return view;
}
}
AutoCompleteTextView autoCompleteTextView = findViewById(R.id.autoCompleteTextView);
PopupWindow popupWindow = new PopupWindow(this);
ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, dataList);
ListView listView = new ListView(this);
listView.setAdapter(adapter);
popupWindow.setContentView(listView);
// 设置PopupWindow的宽度
popupWindow.setWidth(/* 设置合适的宽度 */);
autoCompleteTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.showAsDropDown(v, 0, 0);
}
});
在上述示例中,可根据具体需求调整宽度的设置。