简体   繁体   English

更改微调器中的文本颜色

[英]change text color in spinner

I want to change a text color in text spinner in dropView. 我想在dropView的文本微调器中更改文本颜色。 I tried to override the method getDropDownView and change a text color but it doesn't work. 我试图覆盖方法getDropDownView并更改文本颜色,但是它不起作用。

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. android.R.layout.simple_spinner_item微调适配器中的此行使用默认行为,请尝试使用自定义布局更改文本颜色。

<?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. 确保其ID为@android:id/text1且不要更改。

use own custom layout 使用自己的自定义布局

  1. Create a layout named spinner_row.xml 创建一个名为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 R.layout.spinner_row.xml替换android.R.layout

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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