简体   繁体   English

Android微调器更改特定项目的背景颜色

[英]android spinner change the background color for specific items

i have a spinner , i want to change the background color for specific items. 我有一个微调器,我想更改特定项目的背景颜色。 i mean if the object is named: country and if it have value section=true so the item of spinner will have background color = blue . 我的意思是,如果该对象被命名为:country,并且其值为section=true ,则微调器的背景color = blue将为color = blue

@Override
   public View getView(int position, View convertView, ViewGroup parent) {
 final kamcoReportType report = values.get(position);
  if (convertView == null) {
    Context mContext = this.getContext();
   LayoutInflater vi = (LayoutInflater)   mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    TextView tv=(TextView) convertView.findViewById(R.id.TextView01);
    if (report.isSection())
       tv.setBackgroundColor=blue  //what ever

how can i do that. 我怎样才能做到这一点。

On your getView() method you can do: 在您的getView()方法上,您可以执行以下操作:

        if (yourCondition) {
            tv.setBackgroundColor(getContext().getResources().getColor(R.color.blue));
        }

This should do the trick. 这应该可以解决问题。

@UPDATE @更新

If I got you right, you want to get the current item on your getView() method, right? 如果我说对了,您想在getView()方法中获取当前项目,对吗?

I assume your adapter is extending ArrayAdapter<T> . 我假设您的适配器正在扩展ArrayAdapter<T> If this is the case, you can get your items using ArrayAdapter getItem(position) method and test it like: 在这种情况下,您可以使用ArrayAdapter getItem(position)方法获取项目并进行如下测试:

if ((getItem(position)).isSection()) { }

Inside your getView() . 在您的getView()内部。

Hope this helps. 希望这可以帮助。

Try this: 尝试这个:

if (report.isSection())
   tv.setBackgroundColor(Color.parseColor("#0D47A1"));

Reading your question, I understood that you want to access the item of the list. 阅读您的问题,我知道您想访问列表中的项目。 Well here is how you can do that: 好吧,这是您可以做到的:

   @Override
   public View getView(int position, View convertView, ViewGroup   parent) {
     final kamcoReportType report = values.get(position);
     if (convertView == null) {
         Context mContext = this.getContext();
         LayoutInflater vi = (LayoutInflater)   mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         TextView tv=(TextView) convertView.findViewById(R.id.TextView01);
         if (report.isSection())
         tv.setBackgroundColor(getContext().getResources().getColor(R.color.blue));
     //here is how will you access your list: let say, your data list name is "itemList"

        item=itemList.get(position);//it will return the item from that list



}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM