使用 SpannableString 设置 Spinner-Item Backgroundcolor
Set Spinner-Item Backgroundcolor with SpannableString
我正在使用 SpannableStrings 为 Spinner 中的项目设置不同的背景颜色。
var spinnerList = new List<SpannableString>();
foreach(var mySpinnerItem in spinnerItems)
{
var tmpSpannable = new SpannableString(mySpinnerItem.Text);
tmpSpannable.SetSpan(new BackgroundColorSpan(mySpinnerItem.Color), 0, mySpinnerItem.Text.Length, 0);
spinnerList.Add(tmpSpannable);
}
var spinnerAdapter = new ArrayAdapter<SpannableString>(Context, Android.Resource.Layout.SimpleSpinnerItem, spinnerList);
spinner.Adapter = spinnerAdapter;
结果如下所示:
我希望所有项目的宽度相同。
我找不到解决方案的两个可能结果:
- 将所有项目的宽度设置为最宽项目的宽度
- 将所有项的宽度设置为整个 Spinner 的宽度
我该如何解决这个问题,或者以编程方式为微调项设置不同背景颜色的替代方法是什么?
结果 1 应该是这样的:
请忽略第一个元素的字体颜色没有设置与背景形成对比
在这种情况下,你想列出所有国家或有大名单
尝试:spinnerMode="dialog" 如下所示:
<Spinner
android:id="@+id/spinnerId"
android:layout_width="wrap_content"
android:dropDownWidth="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:spinnerMode="dialog"/>
根据您上面的代码,它只是为您的文本视图添加背景颜色,而不是整个项目,您可以自定义适配器,然后将颜色设置为项目视图,这里是一个将 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;
convertView.SetBackgroundColor(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;
convertView.SetBackgroundColor(list[position].Color);
return convertView;
}
}
class MySpinnerItem
{
public string Text;
public Color Color;
public MySpinnerItem(string text, Color color)
{
Text = text;
Color = color;
}
}
我正在使用 SpannableStrings 为 Spinner 中的项目设置不同的背景颜色。
var spinnerList = new List<SpannableString>();
foreach(var mySpinnerItem in spinnerItems)
{
var tmpSpannable = new SpannableString(mySpinnerItem.Text);
tmpSpannable.SetSpan(new BackgroundColorSpan(mySpinnerItem.Color), 0, mySpinnerItem.Text.Length, 0);
spinnerList.Add(tmpSpannable);
}
var spinnerAdapter = new ArrayAdapter<SpannableString>(Context, Android.Resource.Layout.SimpleSpinnerItem, spinnerList);
spinner.Adapter = spinnerAdapter;
结果如下所示:
我希望所有项目的宽度相同。
我找不到解决方案的两个可能结果:
- 将所有项目的宽度设置为最宽项目的宽度
- 将所有项的宽度设置为整个 Spinner 的宽度
我该如何解决这个问题,或者以编程方式为微调项设置不同背景颜色的替代方法是什么?
结果 1 应该是这样的:
请忽略第一个元素的字体颜色没有设置与背景形成对比
在这种情况下,你想列出所有国家或有大名单 尝试:spinnerMode="dialog" 如下所示:
<Spinner
android:id="@+id/spinnerId"
android:layout_width="wrap_content"
android:dropDownWidth="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:spinnerMode="dialog"/>
根据您上面的代码,它只是为您的文本视图添加背景颜色,而不是整个项目,您可以自定义适配器,然后将颜色设置为项目视图,这里是一个将 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;
convertView.SetBackgroundColor(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;
convertView.SetBackgroundColor(list[position].Color);
return convertView;
}
}
class MySpinnerItem
{
public string Text;
public Color Color;
public MySpinnerItem(string text, Color color)
{
Text = text;
Color = color;
}
}