更改微调器的文本颜色

Change text color of spinner

从上面的代码可以看出,只有当我打开下拉菜单时,文本的颜色才会设置为蓝色,而如果菜单没有打开,微调器文本仍然是白色,我想确定一下微调器文本的颜色是蓝色,即使它没有打开也不是白色,我该如何实现呢?下面的代码是 xml 和 kotlin

file.xml:

<Spinner
                    android:id="@+id/spinner"
                    android:layout_width="200dp"
                    android:layout_height="40dp"
                    android:layout_alignBottom="@id/textviewtipologia"
                    android:layout_marginLeft="100dp"
                    android:layout_marginTop="-40dp"
                    android:background="#F5F5F5"
                    android:popupBackground="#F5F5F5"
                    android:theme="@style/spinnerstyle" />

  <style name="spinnerstyle" parent="Base.Widget.AppCompat.Spinner">
        <item name="android:textColor">@color/customSpinner</item>
        <item name="android:textColorHighlight">@color/customSpinner</item>
        <item name="android:textColorHint">@color/customSpinner</item>
    </style>

file.kt

spinner = findViewById<Spinner>(R.id.spinner)
        var image: ImageView = findViewById<ImageView>(R.id.imgauto)
        var listaTipologia: List<TipologiaVeicolo> =
            APISupport.getListaTipologieVecioli(firebaseid, email, key)
        val tipologieAuto: ArrayList<String> = ArrayList()
        var count = 0
        for (i in listaTipologia.indices) {
            if (count == 0) {
                idVeicoloSelect = listaTipologia[i].ID
            }
            tipologieAuto.add(listaTipologia[i].DESCRIZIONE)
            count++
        }

您可以尝试通过以下方式创建微调器:

  1. 首先在布局文件中添加微调器:

layout.xml

<Spinner
       android:id="@+id/spinner1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/> 
  1. 现在创建一个 spinner_item.xml 布局文件,在其中添加一个 TextView 作为微调器的选定项目:

spinner_item.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- You can change content of the textview as your need-->
<TextView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:textColor="#F5F5F5"
  android:textSize="18sp"
  android:gravity="left"
  android:padding="5dip"
  /> 
  1. 现在在你的 strings.xml 文件中创建一个 string-array ,这将是微调器的内容:

strings.xml

<string-array name="languages">
    <item>Hindi</item>
    <item>English</item>
    <item>Spanish</item>
    <item>German</item>
    <item>Japanese</item>
</string-array>
  1. 现在最后一件事是在 activity class:
  2. 中添加代码

activity.java

// call this method in onCreate() method of activity class
public void setSpinner(){
    Spinner spinner = findViewById(R.id.spinner1); // get the spinner in the layout file
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.languages, 
    R.layout.spinner_item); // create adapter for spinner and set the item layout
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // set the drop down view to the adapter
    spinner.setAdapter(adapter); // set adapter to the spinner
    spinner.setOnItemSelectedListener((OnItemSelectedListener) this); // set listener to the spinner to get the data whenever item is selected from spinner

}

// override this method to get the selected item of the spinner whenever item of the spinner is selected 
// also implements OnItemSelectedListener to your activity class
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    // this is the text selected from the spinner 
    String text = parent.getItemAtPosition(position).toString(); 

    // do whatever you want from the text
    Toast.makeText(parent.getContext(), text, Toast.LENGTH_SHORT).show();
}

希望对您有所帮助