android 旋转器下拉自定义

android spinner dropdown custom

我使用微调器来展示一些东西,这是我的代码:

ArrayAdapter<ClassName> adapter = new ArrayAdapter<ClassName>(getActivity(), android.R.layout.simple_spinner_dropdown_item, nameList);
                adapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);

使用此代码,当单击微调器时,我可以看到文本和复选框。
之后,我尝试像这样自定义微调器:

ArrayAdapter<ClassName> adapter = new ArrayAdapter<ClassName>(getActivity(),
                        R.layout.list_spinner, tenfavList);
                adapter.setDropDownViewResource(R.layout.list_spinner_dropdown);

这是 list_spinner_dropdown.xml :

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:singleLine="true"
android:ellipsize="end"
android:textColor="#0004ff"
android:textSize="@dimen/20sp"/>

当使用这段代码时,当点击微调器时,我只看到文本,没有复选框。
如何解决?

好的,现在我知道你的问题了。问题只是您请在 CheckedTextView 中添加样式以选中或未选中,例如:

在 res/drawable

中添加文件 default_checkbox.xml
<?xml version="1.0" encoding="utf-8"?>

<item android:state_checked="true"
    android:drawable="@drawable/checkbox_checked" /> <!-- checked -->

<item android:state_pressed="true"
    android:drawable="@drawable/checkbox_checked" /> <!-- pressed -->

<item android:drawable="@drawable/checkbox_default" /> <!-- default -->

在你的 Xml 中:

 <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAllCaps="true"
        android:singleLine="true"
        android:ellipsize="end"
        android:checked="true"
        android:checkMark="@drawable/default_checkbox"
        android:text="sdfsdfsdfsdfdsfdsf"
        android:textColor="#0004ff"
        android:textSize="25dp"/>