在Android微调器中更改选定项目的背景色

在Android微调器中更改选定项目的背景色,android,spinner,Android,Spinner,我正在开发一个android应用程序,并在我的应用程序的许多地方使用Spinner。 我想要的是更改微调器的选定项的背景色,以便可以轻松识别当前选定的项 我已选中此链接,但这样做将更改选定的textview背景颜色,但不会更改下拉列表中的颜色,我希望在看到下拉列表时更改选定textview的背景颜色 我想更改列表中所选项目的颜色而不是微调器,请参见下图 我该怎么做?拜托,有人能帮我吗 非常感谢高级版。 <style name="AppTheme" parent="Theme.AppCom

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

我已选中此链接,但这样做将更改选定的textview背景颜色,但不会更改下拉列表中的颜色,我希望在看到下拉列表时更改选定textview的背景颜色

我想更改列表中所选项目的颜色而不是微调器,请参见下图

我该怎么做?拜托,有人能帮我吗

非常感谢高级版。


<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="colorControlNormal">@color/spinner_background</item>

</style>
@颜色/原色 @颜色/原色暗 @颜色/颜色重音 @颜色/微调器\u背景

在“颜色”文件夹中定义微调器\u背景颜色。

尝试在可绘制文件中创建选择器,例如

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="@color/colorPrimary" />
<item android:drawable="@android:color/transparent" />
</selector>

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

它将帮助您:

 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);
我在互联网上搜索了一个合适的解决方案,不必用java代码硬编码后台行为。 您可以使用drawables实现这一点(设置所选项目的背景色)

您需要做的是将
dropdownViewResource
设置为自定义布局。该布局应如下所示:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/colorPrimaryLight" android:state_hovered="true" />
    <item android:drawable="@color/colorPrimaryLight" android:state_selected="true" />
</selector>

spinner\u item\u background.xml中,可以为每个项目状态定义背景。例如,如果您希望在按下时产生涟漪效果,但选中后产生立体效果,则可以尝试以下操作:


以下是通过XML的解决方案:

微调器看起来像:

<Spinner
        android:id="@+id/settingsSleepingTimePicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/spinner_main_button"
        android:popupBackground="@color/colorPrimary"
        android:textColor="@android:color/white"
        android:textSize="20sp"/>
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/spinner"
    android:textColor="#ffffff"
    android:textSize="20sp" /> 
spinner_item.xml看起来像:

<Spinner
        android:id="@+id/settingsSleepingTimePicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/spinner_main_button"
        android:popupBackground="@color/colorPrimary"
        android:textColor="@android:color/white"
        android:textSize="20sp"/>
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/spinner"
    android:textColor="#ffffff"
    android:textSize="20sp" /> 

最后我们设置@drawable/spinner如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/colorPrimaryLight" android:state_hovered="true" />
    <item android:drawable="@color/colorPrimaryLight" android:state_selected="true" />
</selector>


希望我的回答会有帮助

创建一个
int
变量
public static int posOfItemSpinnerSelected
在您的活动中:

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        posOfItemSpinnerSelected=position;
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
    }
});

您需要实现它的getDropDownView()方法,如果无法实现,请在此处发布您的代码应用微调器的适配器并为特定项目设置新的背景色。@Vickyexpert:您能告诉我如何使用getDropDownView()方法实现此目的吗。这将是一个很大的帮助。非常感谢advanced。向我展示您的适配器类,以便我可以编辑it@Vickyexpert:我正在使用简单的ArrayAdapter,其中没有任何自定义项<代码>ArrayAdapter\u adapter=新的ArrayAdapter(上下文,R.layout.simple\u文本\u视图)_setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项)_适配器。添加(“一”)_添加(“两个”)_添加(“三”)_spinner.setAdapter(_adapter)请检查一次。感谢此代码:),但这将更改第0个位置项目的背景色,而不是在微调器中选择的项目的背景色。我如何才能做到这一点,而且覆盖getDropDownView方法后,下拉图标不可见。您需要将selectedItemPosition而不是0传递到那里,这是可行的,但这是老派的解决方案。我们可以用ripple来配置它,而不是用样式来配置它吗?最后将@Override public void设置为selected(AdapterView父对象、视图视图、最终整型位置、长id){selectedItem=position;}如果我想更改文本颜色,我需要做什么?这种更改对背景没有任何影响,它会使右侧的小三角形消失…对
colorControlNormal
没有影响。我已经尝试过了,但状态不起作用。我正在使用://ArrayAdapter=ArrayAdapter.createFromResource(this.getContext(),R.array.task_status,R.layout.spinner_item_when_closed);//adapter.setDropDownViewResource(R.layout.spinner\u item\u打开时);//在文件“res\layout\spinner\u item\u when\u open.xml”中使用类似“spinner\u item\u background.xml”的代码设置背景。我不知道这是否能解决问题,但背景xml需要位于
drawable
文件夹中。您无法将颜色代码传递给android:drawable it give this error“与属性drawable不兼容”(attr)参考“
if(position== YourActivity.posOfItemSpinnerSelected){
    textView.setBackgroundColor(ContextCompat.getColor(mActivity,R.color.item_spinner_selected)); 
} else {
    textView.setBackgroundColor(ContextCompat.getColor(mActivity,R.color.white));
}