Change text color of selected item in spinner

后端 未结 14 1523
情深已故
情深已故 2020-11-29 06:17

How can I change the font color of the selected item in a spinner?

I am able to change the background color of the selected item, the color of the dropdown item etc,

相关标签:
14条回答
  • 2020-11-29 06:49

    You don't need java code for background color change in Android 2.3v. Just add android:background="#F0F8FF" to your spinner in xml file.

    0 讨论(0)
  • 2020-11-29 06:51

    See my answer to a similar question here. My answer is similar to Priya's, except it properly sets the default selected item's text color as well (so there's no lag with the wrong color when waiting for the spinner to automatically select the default item, which occurs after the user interface is already on-screen).

    0 讨论(0)
  • 2020-11-29 06:52

    I know this is old question, but I had big problem on changing color of selected item in spinner in TabLayout and this really worked for me:

    spinner.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    ((TextView) spinner.getSelectedView()).setTextColor(Color.WHITE); //change to your color
                }
            });
    
    0 讨论(0)
  • 2020-11-29 06:56

    try implementing onItemSelected in your OnItemSelectedListener for change the text color of spinner selected item

    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
        int index = adapterView.getSelectedItemPosition();
        ((TextView) spinner.getSelectedView()).setTextColor(getResources().getColor(R.color.Blue)); //<----
    
    0 讨论(0)
  • 2020-11-29 06:56

    using selector as text color .

    create color_selector.xml in drawable like

    <?xml version="1.0" encoding="utf-8"?>
     <selector xmlns:android="http://schemas.android.com/apk/res/android">
         <item android:state_pressed="true"
               android:color="#000000" /> <!-- pressed -->
         <item android:state_focused="true"
               android:color="#000000" /> <!-- focused -->
         <item android:color="#FFFFFF" /> <!-- default -->
     </selector>
    

    and in textview

    <TextView 
       android:textColor="@drawable/color_selector"/>
    
    0 讨论(0)
  • 2020-11-29 06:56
    mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
          @Override
          public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
               ((TextView) adapterView.getChildAt(0)).setTextColor(Color.WHITE);
    }
    
    0 讨论(0)
提交回复
热议问题