简体   繁体   中英

change text color in spinner

I want to change a text color in text spinner in dropView. I tried to override the method getDropDownView and change a text color but it doesn't work.

SimpleCursorAdapter adapter = new SimpleCursorAdapter(
            this,
            android.R.layout.simple_spinner_item,
            extendedCursor, from, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER) {

        @Override
        public View getDropDownView(int position, View convertView,android.view.ViewGroup parent){
            View v = convertView;
            if (v == null) {
                Context mContext = AddEditLoadActivity.this;
                LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                // Androids orginal spinner view item
                v = vi.inflate(android.R.layout.simple_spinner_dropdown_item, null);
            }
            // The text view of the spinner list view
            TextView tv = (TextView) v.findViewById(android.R.id.text1);


            boolean disabled = !isEnabled(position);
            if(disabled){tv.setTextColor(Color.WHITE);}
            else{tv.setTextColor(Color.WHITE);}

            return v;
        }

        @Override
        public long getItemId(int position) {
            extendedCursor.moveToPosition(position);
            return extendedCursor.getLong(extendedCursor.getColumnIndex(DatabaseContract.DictionaryTable.ITEM_ID));
        }
    };

android.R.layout.simple_spinner_item this line in your spinner adapter use default behavior, try using custom layout to change text color.

<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="?attr/dropdownListPreferredItemHeight"
android:ellipsize="marquee"
android:singleLine="true"
android:textColor="@color/your_color" />

make sure its id will be @android:id/text1 and don't be changed.

use own custom layout

  1. Create a layout named spinner_row.xml

     <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#316FA2" android:textSize="12sp" android:gravity="left" android:singleLine="true" android:padding="6dip" android:textColor="@color/white" />///you can add your color /> 
  2. replace android.R.layout with R.layout.spinner_row.xml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM