我需要实现单击按钮时更改应用程序主题的功能。
我已经成功更改了按钮、编辑文本和 TextView 的样式。但面临着改变 Spinner
样式的问题。
唯一成功的更改是背景颜色的更改:
spinnerRoutes.setBackgroundResource(R.drawable.dark_theme_spinner_background);
但是仅标题行的背景发生了变化。我还需要更改行和项目的文本颜色,以及微调器行的背景。
我已经尝试过很多类似的 stackoverflow 解决方案,但没有一个有效。
是否可以通过编程方式更改微调器的样式?
最佳答案
如果你想自定义微调器的下拉菜单,你需要重写适配器类中的 getView 函数。另外,如果你想控制 Spinnner 上显示的文本,你需要这个 spinner.onItemSelectedListener() 并在回调方法中修改 TextView
自定义微调器下拉菜单的示例代码:
@Override
public View getDropDownView (int position, View convertView, ViewGroup parent) {
/**The Adapter's view to be supplied for the spinner when constructing a spinner. */
View view = super.getView(position, convertView, parent);
TextView listView = (TextView) view.findViewById(android.R.id.text1);
listView .setBackgroundColor(Color.parseColor(fontBack_BgnColor));
listView .setHeight((int) heightPixels);
listView .setGravity(Gravity.CENTER);
listView .setTextSize(TypedValue.COMPLEX_UNIT_PX, 20);
listView .setTextColor(Color.parseColor(fontColor));
listView .setText(super.getItem(position));
return view;
}
自定义微调器文本(出现在微调器顶部的文本):
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX, 25);
((TextView) view).setTextColor(Color.parseColor(spinnerTextColor));
}
关于android - 以编程方式更改 Spinner 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30481148/