以编程方式更改微调器项目文本的颜色

时间:2019-11-12 12:07:59

标签: c# xamarin xamarin.android

我正在通过sqlite数组将记录加载到微调器中。是否可以根据项目本身更改项目的文本颜色?

代码如下:

Spinner spinner = FindViewById<Spinner>(CiberStock.Resource.Id.spnDados);

ArrayAdapter adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, familias);

spinner.Adapter = adapter;

谢谢

1 个答案:

答案 0 :(得分:0)

如果需要,您可以检查一下吗?

您可以自定义适配器,然后将颜色设置为textview,这是将spinnerItems传递给适配器的示例:

[Activity(Label = "SpinnerActivity", MainLauncher = true)]
public class SpinnerActivity : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        SetContentView(Resource.Layout.spinner);

        Spinner sp = FindViewById<Spinner>(Resource.Id.spinner);
        List<MySpinnerItem> spinnerItems = new List<MySpinnerItem>();
        spinnerItems.Add(new MySpinnerItem("Red-",Color.Red));
        spinnerItems.Add(new MySpinnerItem("Yellow--", Color.Yellow));
        spinnerItems.Add(new MySpinnerItem("Green-----", Color.Green));
        spinnerItems.Add(new MySpinnerItem("Blue---", Color.Blue));
        spinnerItems.Add(new MySpinnerItem("Black--------", Color.Black));
        var spinnerAdapter = new MyAdapter(this, Android.Resource.Layout.SimpleSpinnerItem, spinnerItems);
        sp.Adapter = spinnerAdapter;
    }
}

class MyAdapter : ArrayAdapter<MySpinnerItem>
{
    public Context context;
    public List<MySpinnerItem> list;
    public int textViewResourceId;
    public MyAdapter(Context context, int textViewResourceId, List<MySpinnerItem> objects) : base(context, textViewResourceId, objects)
    {
        this.context = context;
        list = objects;
        this.textViewResourceId = textViewResourceId;
    }

    public override View GetDropDownView(int position, View convertView, ViewGroup parent)
    {

        if (convertView == null)
        {
            LayoutInflater inflater = LayoutInflater.From(context);
            convertView = inflater.Inflate(textViewResourceId, parent, false);
        }
        TextView tv = (TextView)convertView.FindViewById(Android.Resource.Id.Text1);
        tv.Text = list[position].Text;
        tv.SetTextColor(list[position].Color);
        return convertView;
    }
    public override View GetView(int position, View convertView, ViewGroup parent)
    {

        if (convertView == null)
        {
            LayoutInflater inflater = LayoutInflater.From(context);
            convertView = inflater.Inflate(textViewResourceId, parent, false);
        }

        TextView tv = (TextView)convertView.FindViewById(Android.Resource.Id.Text1);
        tv.Text = list[position].Text;
        tv.SetTextColor(list[position].Color);
        return convertView;
    }
}

class MySpinnerItem
{
    public string Text;
    public Color Color;

    public MySpinnerItem(string text, Color color)
    {
        Text = text;
        Color = color;
    }
}