Text on spinner is white on a white background

后端 未结 8 1918
半阙折子戏
半阙折子戏 2020-12-13 09:01

The text on my spinners is white, and I have no idea why.

\"enter

This is my x

相关标签:
8条回答
  • 2020-12-13 09:35

    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);
    
    0 讨论(0)
  • 2020-12-13 09:39

    I solved this problem using

    getBaseContext()
    

    instead of

    getApplicationContext()
    
    0 讨论(0)
  • 2020-12-13 09:45

    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

    0 讨论(0)
  • 2020-12-13 09:51

    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"/>
    
    0 讨论(0)
  • 2020-12-13 09:51

    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);
    
    0 讨论(0)
  • 2020-12-13 09:53

    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
            }
        }
    
    0 讨论(0)
提交回复
热议问题