如何在下拉列表中更改微调器 的文本颜色?
当我这样做时(在 onItemSelected 方法内):
((TextView) view).setTextColor(getResources().getColor(R.color.accent));
或
((TextView) spinner.getAdapter().getView(position, view, parent)).setTextColor(getResources().getColor(R.color.accent));
或
((TextView) spinner.getChildAt(position)).setTextColor(getResources().getColor(R.color.accent));
或
((TextView) spinner.getSelectedView()).setTextColor(getResources().getColor(R.color.accent));
它会更改微调器所选项目的颜色,但不会更改下拉列表中的颜色。
这样做:
((TextView) spinner.getAdapter().getDropDownView(position, null, parent)).setTextColor(getResources().getColor(R.color.accent));
不起作用。
答案 0 :(得分:2)
您可以达到此目标设置适配器。您应该创建一个自定义适配器,以简单的方式显示您的项目。
this is the official reference for adapters。您可以参考任何方法或变量。
this SO link has the solution。
所以你需要做的是:
<强> ADAPTER 强>
class HighLightArrayAdapter extends ArrayAdapter<CharSequence> {
private int mSelectedIndex = -1;
public void setSelection(int position) {
mSelectedIndex = position;
notifyDataSetChanged();
}
public HighLightArrayAdapter(Context context, int resource, CharSequence[] objects) {
super(context, resource, objects);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View itemView = super.getDropDownView(position, convertView, parent);
if (position == mSelectedIndex) {
itemView.setBackgroundColor(Color.rgb(56,184,226));
} else {
itemView.setBackgroundColor(Color.TRANSPARENT);
}
return itemView;
}
}
<强> PS:强>
由于setSelection
方法不是原生方法,因此您需要从活动中的OnItemSelectedListener
调用它。
希望这有帮助。
答案 1 :(得分:1)
我认为有两种方法可以在android
中实现这一点让我用例子解释每种方法:
尝试 @Override getDropDownView(int position,View convertView,ViewGroup parent),根据您的要求自定义或设置 TextView 属性。
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
TextView textView = (TextView) super.getDropDownView(position, convertView, parent);
textView.setTextColor(Color.BLUE);
return textView;
}
尝试 @Override getView(int position,View convertView,ViewGroup parent),根据您的要求自定义或设置 TextView 属性。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = (TextView) super.getView(position, convertView, parent);
textView.setTextColor(Color.RED);
return textView;
}
使用 @Override ArrayAdapter 方法
进行示例spinnerList = new ArrayList<>();
spinnerList.add("Spinner item 1");
spinnerList.add("Spinner item 2");
spinnerList.add("Spinner item 3");
spinnerList.add("Spinner item 4");
spinnerList.add("Spinner item 5");
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,spinnerList){
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
TextView textView = (TextView) super.getDropDownView(position, convertView, parent);
textView.setTextColor(Color.BLUE);
return textView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = (TextView) super.getView(position, convertView, parent);
textView.setTextColor(Color.RED);
return textView;
}
};
mSpinner.setAdapter(spinnerAdapter);
创建自定义微调器适配器
尝试创建自定义微调器适配器:
public class CustomSpinnerAdapter extends BaseAdapter {
private Context context;
private String[] data;
private int selectedItemColor;
private int itemColor;
public CustomSpinnerAdapter(Context context, String[] data, int selectedItemColor, int itemColor) {
this.context = context;
this.data = data;
this.selectedItemColor = selectedItemColor;
this.itemColor = itemColor;
}
public CustomSpinnerAdapter(Context context, String[] data, String label, int selectedItemColor, int itemColor) {
this.context = context;
this.data = data;
this.data = new String[data.length + 1];
this.data[0] = label;
for (int i = 1; i <= data.length; i++) {
this.data[i] = data[i - 1];
}
this.selectedItemColor = selectedItemColor;
this.itemColor = itemColor;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.spinner_selected_item, parent, false);
holder.txtSpinnerSelected = (TextView) convertView.findViewById(R.id.txtSpinnerSelected);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtSpinnerSelected.setTextColor(itemColor);
holder.txtSpinnerSelected.setText(data[position]);
return convertView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.spinner_drop_down_item, parent, false);
holder.txtSpinnerItem = (TextView) convertView.findViewById(R.id.txtSpinnerItem);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtSpinnerItem.setTextColor(selectedItemColor);
holder.txtSpinnerItem.setText(data[position]);
return convertView;
}
@Override
public int getCount() {
return data.length;
}
@Override
public Object getItem(int position) {
return data[position];
}
@Override
public long getItemId(int position) {
return position;
}
static class ViewHolder {
TextView txtSpinnerSelected;
TextView txtSpinnerItem;
}
}
<强> spinner_drop_down_item.xml 强>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/txtSpinnerItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"></TextView>
<强> spinner_selected_item.xml 强>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/txtSpinnerItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"></TextView>
使用自定义微调器适配器的工作示例:
String[] spinnerArray = new String[5];
spinnerArray[0] = "Spinner item 1";
spinnerArray[1] = "Spinner item 2";
spinnerArray[2] = "Spinner item 3";
spinnerArray[3] = "Spinner item 4";
spinnerArray[4] = "Spinner item 5";
CustomSpinnerAdapter customSpinnerAdapter = new CustomSpinnerAdapter(this,spinnerArray,Color.RED,Color.BLUE);
mSpinner.setAdapter(customSpinnerAdapter);