问题
The text on my spinners is white, and I have no idea why.
This is my xml, nothing special
<Spinner
android:id="@+id/spinner_date"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
And my code
dateSpinner = (Spinner) findViewById(R.id.spinner_date);
selectedDate = calendar.getTime();
List<String> list = new ArrayList<String>();
list.add(formatter.format(selectedDate));
dateAdapter = new ArrayAdapter<String>(mContext,
android.R.layout.simple_spinner_item, list);
dateSpinner.setAdapter(dateAdapter);
What could be the reason that my text is displayed in white?
EDIT: I've found the reason, I replaced the mContext parameter which was set in my onCreate.
mContext = getApplicationContext();
Now I use d
ateAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
and it works.
回答1:
I have same problem and have found the answer. You dont use application context, instead, just use getActivity() (if you are in fragment) or this (if you are in activity), it will work
dateAdapter = new ArrayAdapter<String>(**this**,
android.R.layout.simple_spinner_item, list);
回答2:
I solved this problem using
getBaseContext()
instead of
getApplicationContext()
回答3:
i change it from
new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, some_list);
to
new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
it's fixed, although i don't want to use "this"
回答4:
I also had same problem it was because of my application theme. Which I solved by replacing the
android.R.layout.simple_spinner_item
with
android.R.layout.simple_list_item_1
in my ArrayAdapter. I hope this may solve your problem
回答5:
Maybe you have a white android:textColor="@android:color/white" attribute in your simple_spinner_item.xml in the layout folder of your project.
Better use a custom spinner item layout with a good android:textColor="@android:color/COLOR_YOU_WANT_TO_USE" attribute.
回答6:
You can easily style the Spinner. Use this in style.xml :
<style name="SpinnerThemeLight" >
<item name="android:colorBackground">@color/black</item>
<item name="android:textColorPrimary">@color/black</item>
<item name="android:textColorSecondary">@color/black</item>
<item name="android:textColorTertiary">@color/black</item>
<item name="android:textColorPrimaryDisableOnly">@color/black</item>
</style>
In the above xml file, I have simply given black color for time sake. Just play with it and sort out your preferred color.
Define the Spinner in the activity.xml as follows :
<android.support.v7.widget.AppCompatSpinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:entries="@array/countrys"
android:spinnerMode="dropdown"
android:theme="@style/SpinnerThemeLight"/>
回答7:
I've changed the text color of that spinner's textview without creating a new layout. I know it's been a long time but it's what worked for me, just thought of sharing it. Best part is you can use it on any default adapter.
Here's the code : (this for Activity & requireActivity for Fragments)
1) Java
arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,groups){
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView listItem = view.findViewById(android.R.id.text1);
listItem.setTextColor(Color.WHITE);
listItem.setTextSize(20);
listItem.setElevation(18);
return view;
}
};
2) Kotlin
arrayAdapter = object : ArrayAdapter<String>(requireActivity(), android.R.layout.simple_spinner_item, spinnerCategoriesList) {
override fun getView(position: Int, @Nullable convertView: View?, parent: ViewGroup): View {
val view = super.getView(position, convertView, parent)
val listItem = view.findViewById<TextView>(android.R.id.text1)
listItem.setTextColor(Color.BLACK)
listItem.textSize = 16f
return view
}
}
回答8:
I assume you have created your own TextView for your Spinner, e.g.
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="@color/white"
android:padding="5dip"
/>
and glued it to your Adapter via a call like that
String[] spinnerItems = getResources().getStringArray(R.array.my_array);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.spinner_text, spinnerItems);
This should result in having the selection-text of your spinner including its items within the dropdown view painted in white. Now the background of your dropdown view will be influenced by your app theme, which in most cases, will lead your white text to be rendered on a white background. To avoid this android allows you to set a resource for your spinner dropdown view. You can set your own view or simply use the default drop-down view, which will overwrite your custom textview within the dropdown menu through the call
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
The complete code could look like this
this.spinner = findViewById(R.id.spinnerView);
String[] spinnerItems = getResources().getStringArray(R.array.my_array);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.spinner_text, spinnerItems);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
this.spinner.setAdapter(adapter);
来源:https://stackoverflow.com/questions/16354168/text-on-spinner-is-white-on-a-white-background