定制微调的项目背景颜色 [英] Customizing spinner's item background color
问题描述
我想提出一个应用程序来计算电阻值,其颜色波段。一切工作正常的纯编程方面。目前,对于selcting我使用的填充颜色列表中的微调色带。
我真的想使它看起来更好。我希望能够在微调设置每个项目的背景颜色给其相应的颜色:
我应该怎么做才达到这一点?这里是我的code的一部分:
Array用于微调:
<字符串数组名=FourBandResistorFullColor>
<项目>黑< /项目>
<项目>布朗和LT; /项目>
<项目>红色和LT; /项目>
<项目>橙色与LT; /项目>
<项目→黄色< /项目>
<项目>绿色< /项目>
<项目>蓝色< /项目>
<项目>紫< /项目>
<项目>灰色< /项目>
<项目>白色< /项目>
< /字符串数组>
在的OnCreate
适配器code方法:
VAR bandOne = FindViewById<&微调GT;(Resource.Id.bandOneColor);
VAR bandOneAdapter = ArrayAdapter.CreateFromResource(这一点,Resource.Array.FourBandResistorFullColor,
Android.Resource.Layout.SimpleSpinnerItem);
bandOneAdapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
bandOne.Adapter = bandOneAdapter;
bandOne.ItemSelected + = BandOne_ItemSelected;
您需要使用自定义适配器。我猜你使用的是ArrayAdapter?如果是这样,你能做到这一点,像这样:
私有类MyArrayAdapter扩展ArrayAdapter<串GT; { 公共MyArrayAdapter(上下文的背景下,诠释textViewResourceId){
超(背景下,textViewResourceId);
} @覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
查看查看= super.getView(位置,convertView,父母);
INT颜色= getColorFromName(的getItem(位置));
view.setBackgroundColor(颜色);
返回视图。
}
}
注意 getColorFromName
是您需要实现自己的功能。它从字符串映射到一个int颜色值(ARGB)。红色为〔实施例是为0xffff0000。
I am making an app to calculate resistor value out of its color bands. Everything works fine on the pure programming side. At the moment for selcting color bands I am using a spinner filled with list of colors.
I really want to make it look better. I want to be able to set background color of each item in the spinner to its corresponding color:
What should I do to achive this? Here is part of my code:
Array used for spinner:
<string-array name="FourBandResistorFullColor">
<item>Black</item>
<item>Brown</item>
<item>Red</item>
<item>Orange</item>
<item>Yellow</item>
<item>Green</item>
<item>Blue</item>
<item>Violet</item>
<item>Gray</item>
<item>White</item>
</string-array>
Adapter code in OnCreate
method:
var bandOne = FindViewById<Spinner>(Resource.Id.bandOneColor);
var bandOneAdapter = ArrayAdapter.CreateFromResource(this, Resource.Array.FourBandResistorFullColor,
Android.Resource.Layout.SimpleSpinnerItem);
bandOneAdapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
bandOne.Adapter = bandOneAdapter;
bandOne.ItemSelected += BandOne_ItemSelected;
You need to use a custom adapter. I guess you are using an ArrayAdapter? If so you could do it like so:
private class MyArrayAdapter extends ArrayAdapter<String>{
public MyArrayAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
int color = getColorFromName(getItem(position));
view.setBackgroundColor(color);
return view;
}
}
Note that getColorFromName
is a function you would need to implement yourself. It maps from String to an int color value (ARGB). Red for exmaple would be 0xffff0000.
这篇关于定制微调的项目背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!