更改微调器中所选项目的文本颜色

如何更改微调器中所选项目的字体颜色?

我可以改变所选项目的背景颜色,下拉项目的颜色等,但不能select项目的文本颜色…我该怎么做?

我的代码是:这是微调我正在使用 – :

<Spinner android:id="@+id/spinner1" android:layout_width="wrap_content" android:layout_height="32dip" android:background="@drawable/mybg" android:divider="@drawable/list_divider" android:drawSelectorOnTop="true" android:popupBackground="#D3D5D3" android:prompt="@string/activityy_prompt" /> 

这是mybg.xml

 <!-- <item android:drawable="@drawable/blue" android:state_pressed="false"/> --> <!-- <item android:drawable="@drawable/back11"/> --> <item android:drawable="@drawable/greenyellow1" android:state_focused="true" android:state_pressed="false"/> <item android:drawable="@drawable/greenyellow1" android:state_focused="true" android:state_pressed="true"/> <item android:drawable="@drawable/greenyellow1" android:state_focused="false" android:state_pressed="true"/> <item android:drawable="@drawable/greenyellow1" android:state_selected="true"/> <item android:drawable="@drawable/back11"/> 

使用这些我不能够改变selecetd项目的文本颜色…

Solutions Collecting From Web of "更改微调器中所选项目的文本颜色"

  <item android:state_activated="true" android:color="@color/red"/> 

这将改变popup窗口中选定的项目颜色。

只需在您select的项目中使用这一行代码

 public void onItemSelected(AdapterView<?> parent, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub item = (String) parent.getItemAtPosition(arg2); ((TextView) parent.getChildAt(0)).setTextColor(0x00000000); } 

使用select器作为文本颜色。

在drawable中创buildcolor_selector.xml

 <?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> 

并在textview中

 <TextView ........... android:textColor=""@drawable/color_selector"/> 

尝试在OnItemSelectedListener中实现onItemSelected,以更改微调选框项目的文本颜色

 public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { int index = adapterView.getSelectedItemPosition(); ((TextView) spinner.getSelectedView()).setTextColor(getResources().getColor(R.color.Blue)); //<---- 

只要使用这行onItemSelected listner –

 public void onItemSelected(AdapterView<?> parent, View arg1, int arg2,long arg3) { item = (String) parent.getItemAtPosition(arg2); ((TextView) parent.getChildAt(0)).setTextColor(0x00000000); } 

您可以通过将OnItemSelectedListener添加到spinner来更改选定的文本颜色

 qtySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { ((TextView) view).setTextColor(Color.BLACK); //Change selected text color } @Override public void onNothingSelected(AdapterView<?> parent) { } }); 

看到我在这里回答类似的问题。 我的答案类似于Priya的,除了它正确地设置默认select的项目的文本颜色(所以当等待微调器自动select默认项目,这是在用户界面已经开始后,屏幕)。

如果您的微调控制器正在使用ArrayAdapter,则可以简单地为您的微调项目传递一个自定义布局。 该布局可以是一个简单的textView,但与android:textColor属性。

MainActivity.java onCreate()

 adapter = new ArrayAdapter<>(this, R.layout.spinner_custom_textcolor, data); spinner.setAdapter(adapter); 

spinner_custom_textcolor.xml

 <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" style="?android:attr/spinnerItemStyle" android:textColor="@color/YOUR_COLOR_HERE" android:singleLine="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="marquee" android:textAlignment="inherit"/> 

*除了android:textColor以外的所有内容都是android.R.layout.simple_spinner_item的直接副本

在Android 2.3v中,您不需要Java代码来更改背景颜色。 只需在xml文件中添加android:background="#F0F8FF"到你的微调器。

有些人使用MaterialBetterSpinner和绑定你的布局,以上所有都不会帮助,试试这个,希望它可以帮助你:

 public class MyAdapter extends ArrayAdapter<String> { public MyAdapter(Context context, int textViewResourceId, List<String> objects) { super(context, textViewResourceId, objects); } @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { return getCustomView(position, convertView, parent); } @Override public View getView(int position, View convertView, ViewGroup parent) { return getCustomView(position, convertView, parent); } public View getCustomView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); final YourXMLBinding rowBinding = DataBindingUtil.inflate(inflater, R.layout.yourXML, parent,false); rowBinding.tv1.setText(mMy.getValues().get(position)); if(position == mMy.getCurrentIndex()) { rowBinding.tv1.setTypeface(Typer.set(getContext()).getFont(Font.ROBOTO_BOLD));//change font rowBinding.tv1.setTextColor(ContextCompat.getColor(getContext(), R.color.yourColor));//change color } return rowBinding.getRoot(); } } 

你的XML是这样的:

 <?xml version="1.0" encoding="utf-8"?> <layout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="@color/colorBackgroundStart"> <TextView android:id="@+id/tv1" android:layout_width="0dp" android:layout_weight="0.7" android:layout_height="30dp" android:textColor="#fff" android:textSize="16dp" android:layout_marginTop="8dp" android:layout_marginBottom="10dp" android:layout_marginLeft="8dp"/> </layout> 

用这个适配器和你的XML创build一个微调器:

 final MyAdapter adapter = new MyAdapter(getContext(), R.layout.yourXML, s.getValues()); final MaterialBetterSpinner spinner = new MaterialBetterSpinner(getContext()); spinner.setAdapter(adapter); 
 mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) { ((TextView) adapterView.getChildAt(0)).setTextColor(Color.WHITE); } 

对我来说,解决scheme是使用CheckedTextView作为下拉资源视图,然后使用颜色select器更改选中项目的颜色。

layout文件夹中的spinner_dropdown_item.xml

 <?xml version="1.0" encoding="utf-8"?> <!-- A `CheckedTextView` allows the color of the text to be changed when it is selected (checked). --> <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/spinner_item_textview" android:layout_height="wrap_content" android:layout_width="match_parent" android:maxLines="1" android:ellipsize="end" android:paddingStart="20dp" android:paddingEnd="20dp" android:paddingTop="8dp" android:paddingBottom="8dp" android:textSize="18sp" android:textColor="@color/spinner_color_selector" android:background="@color/spinner_background" /> 

color文件夹中的spinner_color_selector

 <?xml version="1.0" encoding="utf-8"?> <!-- Highlight the selected (checked) item when the spinner is open. --> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_checked="true" android:color="@color/white" /> <item android:color="@color/blue_100" /> </selector> 

必须将spinner_dropdown_item.xml设置为AdapterDropDownResourceView 。 在我的情况下,我使用从多个来源拉取信息的ResourceArrayAdapter

 // Setup a `MatrixCursor` for the static entries. String[] matrixCursorColumnNames = {DatabaseHelper._ID, DatabaseHelper.NAME}; MatrixCursor matrixCursor = new MatrixCursor(matrixCursorColumnNames); matrixCursor.addRow(new Object[]{-2, getString(R.string.first_spinner_item)}); matrixCursor.addRow(new Object[]{-1, getString(R.string.second_spinner_item)}); // Get a `Cursor` with the list of additional items from the database. Cursor cursor = DatabaseHelper.getCursor(); // Combine `matrixCursor` and `cursor`. MergeCursor mergeCursor = new MergeCursor(new Cursor[]{matrixCursor, foldersCursor}); // Create a `ResourceCursorAdapter` for the spinner with `this` context. `0` specifies no flags.; ResourceCursorAdapter resourceCursorAdapter = new ResourceCursorAdapter(this, R.layout.spinner_item, mergeCursor, 0) { @Override public void bindView(View view, Context context, Cursor cursor) { // Get a handle for the spinner item `TextView`. TextView spinnerItemTextView = (TextView) view.findViewById(R.id.spinner_item_textview); // Set the `TextView` to display the name. spinnerItemTextView.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.NAME))); } }; // Set the `ResourceCursorAdapter` drop drown view resource. resourceCursorAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item); // Get a handle for the `Spinner`. Spinner spinner = (Spinner) findViewById(R.id.spinner); // Set the adapter for the folder `Spinner`. spinner.setAdapter(resourceCursorAdapter); 

因为ResourceCursorAdapter在打开和closures时使用相同的bindView来填充微调spinner_dropdown_item.xmlspinner_item.xml spinner_dropdown_item.xmlspinner_item.xml中的TextView的id必须相同。

layout文件夹中的spinner_item.xml

 <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/spinner_item_textview" android:layout_height="wrap_content" android:layout_width="match_parent" android:maxLines="1" android:ellipsize="end" android:paddingStart="10dp" android:paddingEnd="10dp" android:textSize="18sp" android:textColor="@color/primaryTextColor" />