如何更改微调器中所选项目的字体颜色?
我可以更改所选项目的背景颜色,下拉项目的颜色等,但不能更改所选项目的文本颜色......我该怎么做?
我的代码是: 这是我正在使用的微调器 - :
<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项目的文字颜色......
答案 0 :(得分:55)
像这样定义OnItemSelectedListener
:
private AdapterView.OnItemSelectedListener listener = new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
((TextView) parent.getChildAt(0)).setTextColor(0x00000000);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
然后将OnItemSelectedListener
设置为spinner
,如下所示:
spinner.setOnItemSelectedListener(listener);
答案 1 :(得分:15)
抽拉/ mybg:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true">
<color android:color="@color/black" />
</item>
</layer-list>
这将更改弹出窗口中所选的项目颜色。
答案 2 :(得分:15)
尝试在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)); //<----
答案 3 :(得分:8)
使用选择器作为文本颜色。
在drawable中创建color_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"/>
答案 4 :(得分:8)
您可以通过将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) {
}
});
答案 5 :(得分:4)
只需使用此行onItemSelected listner -
public void onItemSelected(AdapterView<?> parent, View arg1, int arg2,long arg3)
{
item = (String) parent.getItemAtPosition(arg2);
((TextView) parent.getChildAt(0)).setTextColor(0x00000000);
}
答案 6 :(得分:3)
在((TextView) view).setTextColor(getResources().getColor(R.color.black));
侦听器方法内设置onItemSelected(...)
即可。
它为您选择的微调文字设置颜色。
答案 7 :(得分:2)
我知道这是一个老问题,但我在TabLayout中更改了微调器中所选项目的颜色时遇到了很大问题,这对我来说真的很有用:
spinner.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
((TextView) spinner.getSelectedView()).setTextColor(Color.WHITE); //change to your color
}
});
答案 8 :(得分:0)
查看我对类似问题here的回答。我的答案类似于Priya,但它也正确设置了默认选定项目的文本颜色(因此在等待微调器自动选择默认值时,没有使用错误颜色的延迟item,在用户界面已经在屏幕上后发生。)
答案 9 :(得分:0)
如果您的微调器使用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
的直接副本答案 10 :(得分:0)
Android 2.3v中的背景颜色更改不需要Java代码。只需添加
{x}文件中的android:background="#F0F8FF"
到您的微调器。
答案 11 :(得分:0)
使用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();
}
}
yourXML是这样的:
<?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>
使用此适配器和yourXML:
创建一个微调器final MyAdapter adapter = new MyAdapter(getContext(), R.layout.yourXML, s.getValues());
final MaterialBetterSpinner spinner = new MaterialBetterSpinner(getContext());
spinner.setAdapter(adapter);
答案 12 :(得分:0)
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
((TextView) adapterView.getChildAt(0)).setTextColor(Color.WHITE);
}
答案 13 :(得分:0)
对我有用的解决方案是使用CheckedTextView
作为下拉资源视图,然后使用颜色选择器更改选中项目的颜色。
spinner_dropdown_item.xml
文件夹中的 layout
:
<?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" />
spinner_color_selector
文件夹中的 color
:
<?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>
必须将{p> spinner_dropdown_item.xml
设置为DropDownResourceView
的{{1}}。在我的情况下,我使用Adapter
从多个来源提取信息。
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
来填充微调器在打开和关闭时,bindView
和TextView
中的spinner_dropdown_item.xml
的ID必须是一样的。
spinner_item.xml
文件夹中的 spinner_item.xml
:
layout