如何自定义微调器?

How to custom spinner?

我想要 android 中的自定义微调器,如下所述 - 占位符文本(微调器第一项 - 所选项目将居中) - 下拉列表项将左对齐

我尝试为 Spinner 使用自定义布局适配器,但只为 Spinner 和下拉列表对齐。 我希望有人能帮助我!我是 Android 的新手,抱歉!

这是我最近在应用程序中使用的示例。

在您的 res/layout 文件夹中创建 2 个文件,一个名为 spinner_list.xml,一个名为 spinner_list_dropdown.xml。

spinner_list.xml

<?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:textSize="@dimen/text_size"
    android:padding="10dp"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:textColor="@color/text_view_text"/>

spinner_list_dropdown.xml

<?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:textSize="@dimen/text_size"
    android:padding="10dp"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:textColor="@color/text_view_text"
    android:background="@color/spinner_background"/>

Java 使用自定义微调器的代码:

 ArrayAdapter<String> adapter = new ArrayAdapter<>(YourActivity.this, R.layout.spinner_list, strYourArray);
        adapter.setDropDownViewResource(R.layout.spinner_list_dropdown);
        spinnerCustomList.setAdapter(adapter);

希望这一切对您来说有意义,如果您需要任何帮助,请随时提出。