Spinner中更改所选项目的背景颜色

Spinner中更改所选项目的背景颜色

本文介绍了在Android Spinner中更改所选项目的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个android应用程序,并在我的应用程序中的许多地方使用了Spinner.我想要的是更改微调器所选项目的背景颜色,以便可以轻松识别当前选中的项目.

我已经检查了此链接我怎样才能做到这一点?拜托,有人可以帮我吗?.

非常感谢高级

.

解决方案

您需要在适配器类中实现以下方法:

它将为您提供帮助:

  int selectedItem = -1;ArrayAdapter< String>dataAdapter = new ArrayAdapter< String>(this,android.R.layout.simple_spinner_item,list){@Override公共视图getDropDownView(int位置,视图convertView,ViewGroup父级){查看v = null;v = super.getDropDownView(position,null,parent);//如果这是所选项目的位置如果(位置== selectedItem){v.setBackgroundColor(Color.BLUE);}别的 {//对于其他视图v.setBackgroundColor(Color.WHITE);}返回v;}};dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);mySpinner.setAdapter(dataAdapter); 

现在将在微调器中选择的项目放在下面

  selectedItem =位置; 

I am working on an android app and using Spinner at many places in my app.What I want is to change the background color of the selected item of spinner, so that one can easily identify which item is currently selected.

I have already checked this link Setting background color for Spinner Item on selection but doing so will change the selected textview background color but do not change its color in dropdown list and I want to change the background color of the selected textview when I will see the dropdown list.

I want to change the color of selected item in list not on spinner, please see the image below.

How can I do this? Please, can someone help me here?.

Thanks a lot in advanced.

解决方案

You need to implement below method in your adapter class:

It will help you:

 int selectedItem = -1;

 ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list) {

   @Override
   public View getDropDownView(int position, View convertView, ViewGroup parent)
   {
       View v = null;
       v = super.getDropDownView(position, null, parent);
       // If this is the selected item position
       if (position == selectedItem) {
           v.setBackgroundColor(Color.BLUE);
       }
       else {
           // for other views
           v.setBackgroundColor(Color.WHITE);

       }
       return v;
   }
};

 dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
 mySpinner.setAdapter(dataAdapter);

Now on item selected in spinner put below

   selectedItem = position;

这篇关于在Android Spinner中更改所选项目的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:40