问题
For some reason, no matter what I do the text coming up in my AutoCompleteTextView is always white. I've explicitly set textColor to black in my XML. I'm wondering if android.R.layout.simple_list_item_1 is white by default?
Here is where I set up my ArrayAdapter:
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, strings);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.searchUserTextField);
textView.setAdapter(adapter);
Here is my XML:
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="65dp"
android:background="@drawable/edittext_border"
android:layout_gravity="top"
android:textColor="#000000"
android:hint="@string/search_user"
android:id="@+id/searchUserTextField"
android:ems="150"
android:paddingLeft="25dp"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:maxLines ="4"
android:maxLength ="150"
android:scrollHorizontally="false"
android:typeface="sans"
android:textSize="14sp"
android:fontFamily="sans-serif-light"
android:capitalize="none"
android:inputType="textEmailAddress"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/submitSearch"
android:layout_toStartOf="@+id/submitSearch" />
回答1:
Looked like its a logged bug in AOSP. You can find some more information as well as workarounds in this post AutoCompleteTextview Color set white by default
回答2:
Just to add, in case someone is still looking for an answer that works as it is not a bug but you can provide your custom layout for autocomplete suggestions as follows:
Change this line of code
final ArrayAdapter<String> adapter = new ArrayAdapter<String (getApplicationContext(), android.R.layout.simple_list_item_1, strings);
to
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.custom_autocomplete, strings);
Make a layout resource file as custom_autocomplete.xml and provide required settings. Working template
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/colorPrimary"
android:background="@color/white"
android:paddingHorizontal="10dp"
android:paddingVertical="15dp" />
回答3:
Change the default layout for drop down item view to your own custom layout containing only TextView:
Example ( In Kotlin )-
Replace android.R.layout.select_dialog_item in the line:
val adapter: ArrayAdapter<String> = ArrayAdapter<String>(this, android.R.layout.select_dialog_item,gender)
To:
val adapter: ArrayAdapter<String> = ArrayAdapter<String>(this, R.layout.item_drop_down,gender)
where gender is :
private var gender :Array<String> = arrayOf("Male", "Female", "Others")
Here is the custom layout file : item_drop_down
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/appBlue"
android:gravity="center_vertical"
android:paddingStart="14dip"
android:paddingEnd="15dip"
android:ellipsize="marquee"
android:textSize="@dimen/sp_15"
/>
Hope this might help somebody! Happy Coding :)
来源:https://stackoverflow.com/questions/34302785/textcolor-of-my-autocompletetextview-is-always-white