android:旋转器的每个项目都有不同的颜色
我有一个微调器,我希望其中每个项目的文本具有不同的颜色。例如,在 item0 上,文本应为红色,在 item1 上,文本应为蓝色,在 item2 上,文本应为绿色。我尝试将要更改的项目设置为 textView 并更改 textview 的颜色,但它不起作用。关于如何完成这项任务有什么想法吗?
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayList<String> array = new ArrayList<String>();
array.add("item0");
array.add("item1");
array.add("item2");
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,R.layout.row, array);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerArrayAdapter)
try{
TextView tv = new TextView((Context) spinner.getItemAtPosition(0));
tv.setTextColor(Color.argb(0, 255, 0, 0));
}catch(Exception e){
Toast.makeText(getApplicationContext(), "Error: " + e.toString(), Toast.LENGTH_LONG);
}
I have a spinner in which I would like to have the text of each item be a different color. for example on item0 the text should be red, item1 the text should be blue, and item2 the text should be green. I tried setting the item I want to change to a textView and changing the color of the textview but it does not work that way. any ideas on how to acomplish this task?
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayList<String> array = new ArrayList<String>();
array.add("item0");
array.add("item1");
array.add("item2");
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,R.layout.row, array);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerArrayAdapter)
try{
TextView tv = new TextView((Context) spinner.getItemAtPosition(0));
tv.setTextColor(Color.argb(0, 255, 0, 0));
}catch(Exception e){
Toast.makeText(getApplicationContext(), "Error: " + e.toString(), Toast.LENGTH_LONG);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建您自己的类来扩展 BaseAdapter 并实现 SpinnerAdapter。
重写 getDropDownView,当您处理位置时,您可以从您膨胀的自定义布局中格式化文本视图。
create your own class that extends BaseAdapter and implements SpinnerAdapter.
Override getDropDownView, and as you process the position you can format the textview from a custom layout you inflate.