Android 多色微调器

Android 多色微调器,android,kotlin,Android,Kotlin,我已经使用下面的代码成功地更改了微调器文本和背景色 spinner_layout.xml布局文件 <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="300dp" android:layout_height="wrap_content" an

我已经使用下面的代码成功地更改了微调器文本和背景色

spinner_layout.xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:textColor="@color/colorBlack"
    android:textSize="18sp"
    android:background="@color/colorWhite"
    android:fontFamily="@font/raleway"/>

当微调器打开时,上面的内容会创建一个带有白色文本的黑色背景,但当微调器关闭时,我的文本在黑色上是黑色的,因为我的布局背景也是黑色的。如何将微调器关闭状态文本更改为白色?

使您的微调器如下所示

您的微调器项目视图布局如下

在您的OnItemSelectedListener中,按如下方式更改选定文本的颜色


})

使用此选项,您可以在打开和关闭时拥有不同的布局

 adapter.setDropDownViewResource(R.layout.your_layout_resource_xml)
当然,在您必须创建自定义适配器之前,我想您已经这样做了

要获得关于背景的最佳结果,需要进行更多更改:

基本上,您可以使用两种不同的布局为微调器和微调器项创建自定义布局

  <style name="AppTheme.spinnerStyle" 
    parent="@android:style/Widget.Material.Light.Spinner"> 

     <item name="android:textColor">@android:color/white</item>
     <item name="android:background">@color/colorPrimary</item>

  </style> 
  <style name="AppTheme.spinnerDropDownItemStyle" 
    parent="@android:style/Widget.Material.DropDownItem.Spinner">

    <item name="android:textColor">@android:color/white</item> 
    <item name="android:background">@color/colorPrimary</item>
  </style>

我是新来的,所有这些都是旧代码,在Java中不是Kotlin。这是我已经有的。我希望微调器关闭为:背面为白色文本,当微调器打开时,黑色文本为白色。是否希望所选项目文本颜色为白色,背景为黑色?是,在进行选择之前。当您到达表单时,背景是黑白文本。当你们打开旋转器时,黑色文本变为白色。我已经更改了我的答案,请现在检查,它会工作的
<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:textColor="@color/white"
    android:textSize="18sp"
    android:fontFamily="@font/raleway"/>
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
    ((TextView)view).setTextColor(Color.WHITE);
}

@Override
public void onNothingSelected(AdapterView<?> parentView) {
    // your code here
}
 adapter.setDropDownViewResource(R.layout.your_layout_resource_xml)
  <style name="AppTheme.spinnerStyle" 
    parent="@android:style/Widget.Material.Light.Spinner"> 

     <item name="android:textColor">@android:color/white</item>
     <item name="android:background">@color/colorPrimary</item>

  </style> 
  <style name="AppTheme.spinnerDropDownItemStyle" 
    parent="@android:style/Widget.Material.DropDownItem.Spinner">

    <item name="android:textColor">@android:color/white</item> 
    <item name="android:background">@color/colorPrimary</item>
  </style>