我制作了一个AutoCompletetextView。 AutoCompleteTextView下拉列表中的项目不可见。如何更改这些项目的颜色。
它的外观如下: -
答案 0 :(得分:14)
要控制在自动完成视图中显示项目的方式,必须在适配器中设置textViewResourceId。您可以使用ArrayAdapter并将android.R.layout.simple_dropdown_item_1line
作为textViewResourceId,如下所示。
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, yourList);
AutoCompleteTextView autocompleteView = (AutoCompleteTextView) findViewById(R.id.autocomplete_box);
autocompleteView.setAdapter(adapter);
OR
如果您想为显示的项目创建自己的样式,请创建一个以TextView
为根元素的XML(请将其命名为my_custom_dropdown.xml
,带有黑色文本和白色背景)< / p>
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:padding="5sp"
android:textColor="@color/black"
android:background="@color/white"/>
然后参考适配器中的xml,如下所示 -
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.my_custom_dropdown, yourList);
答案 1 :(得分:7)
只是要指出,使用android.R.layout.simple_dropdown_item_1line
它会给你上面遇到的同样问题。因此,您最好只在TextView
文件中创建自己的.xml
。
答案 2 :(得分:5)
如果您将代码从"android.R.layout.simple_list_item_1"
更改为"android.R.layout.simple_dropdown_item_1line"
您应该尝试在 setContentView
之前编写此代码setTheme(android.R.style.Theme);
它对我有用:)
答案 3 :(得分:0)
只需使用"android.R.layout.simple_list_item_1"
代替"android.R.layout.simple_dropdown_item_1line"
.....你的问题就会解决......:)
答案 4 :(得分:0)
我创建了一个示例项目:AutoCompleteTextViewAdapter(Github Repo)
您需要实现以下源代码:
<强>活动强>
ArrayAdapter<String> myCustomAdapter = new ArrayAdapter<String>(this, R.layout.text_custom_view, countriesNames);
final AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.auto_complete_text_view);
textView.setAdapter(myCustomAdapter);
使用自定义TextView的XML
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/country_name"
style="@style/CustomTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true" />
最终结果
答案 5 :(得分:0)
Here is answer in hope others will benefit
public class SelectState extends Activity {
final static int[] to = new int[] { android.R.id.text1 };
final static String[] from = new String[] { "state" };
private TextView mStateCapitalView;
private AutoCompleteTextView mStateNameView;
private AutoCompleteDbAdapter mDbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDbHelper = new AutoCompleteDbAdapter(this);
setContentView(R.layout.selectstate);
Button confirmButton = (Button) findViewById(R.id.confirm);
confirmButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
setResult(RESULT_OK);
finish();
}
});
mStateCapitalView = (TextView) findViewById(R.id.state_capital);
mStateNameView = (AutoCompleteTextView) findViewById(R.id.state_name);
// Create a SimpleCursorAdapter for the State Name field.
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this,
android.R.layout.simple_dropdown_item_1line, null,
from, to);
mStateNameView.setAdapter(adapter);
// Set an OnItemClickListener, to update dependent fields when
// a choice is made in the AutoCompleteTextView.
mStateNameView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
// Get the cursor, positioned to the corresponding row in the
// result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
// Get the state's capital from this row in the database.
String capital =
cursor.getString(cursor.getColumnIndexOrThrow("capital"));
// Update the parent class's TextView
mStateCapitalView.setText(capital);
}
});
// Set the CursorToStringConverter, to provide the labels for the
// choices to be displayed in the AutoCompleteTextView.
adapter.setCursorToStringConverter(new CursorToStringConverter() {
public String convertToString(android.database.Cursor cursor) {
// Get the label for this row out of the "state" column
final int columnIndex = cursor.getColumnIndexOrThrow("state");
final String str = cursor.getString(columnIndex);
return str;
}
});
// Set the FilterQueryProvider, to run queries for choices
// that match the specified input.
adapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
// Search for states whose names begin with the specified letters.
Cursor cursor = mDbHelper.getMatchingStates(
(constraint != null ? constraint.toString() : null));
return cursor;
}
});
}
}
请检查一下 final static int [] to = new int [] {android.R.id.text1};