对我有用的解决方案是将 aCheckedTextView
用于下拉资源视图,然后使用颜色选择器更改选中项目的颜色。
spinner_dropdown_item.xml
在layout
文件夹中:
<?xml version="1.0" encoding="utf-8"?>
<!-- A `CheckedTextView` allows the color of the text to be changed when it is selected (checked). -->
<CheckedTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinner_item_textview"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:maxLines="1"
android:ellipsize="end"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:textSize="18sp"
android:textColor="@color/spinner_color_selector"
android:background="@color/spinner_background" />
spinner_color_selector
在color
文件夹中:
<?xml version="1.0" encoding="utf-8"?>
<!-- Highlight the selected (checked) item when the spinner is open. -->
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true" android:color="@color/white" />
<item android:color="@color/blue_100" />
</selector>
spinner_dropdown_item.xml
必须设置为DropDownResourceView
的Adapter
。就我而言,我使用的ResourceArrayAdapter
是从多个来源提取信息的。
// Setup a `MatrixCursor` for the static entries.
String[] matrixCursorColumnNames = {DatabaseHelper._ID, DatabaseHelper.NAME};
MatrixCursor matrixCursor = new MatrixCursor(matrixCursorColumnNames);
matrixCursor.addRow(new Object[]{-2, getString(R.string.first_spinner_item)});
matrixCursor.addRow(new Object[]{-1, getString(R.string.second_spinner_item)});
// Get a `Cursor` with the list of additional items from the database.
Cursor cursor = DatabaseHelper.getCursor();
// Combine `matrixCursor` and `cursor`.
MergeCursor mergeCursor = new MergeCursor(new Cursor[]{matrixCursor, foldersCursor});
// Create a `ResourceCursorAdapter` for the spinner with `this` context. `0` specifies no flags.;
ResourceCursorAdapter resourceCursorAdapter = new ResourceCursorAdapter(this, R.layout.spinner_item, mergeCursor, 0) {
@Override
public void bindView(View view, Context context, Cursor cursor) {
// Get a handle for the spinner item `TextView`.
TextView spinnerItemTextView = (TextView) view.findViewById(R.id.spinner_item_textview);
// Set the `TextView` to display the name.
spinnerItemTextView.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.NAME)));
}
};
// Set the `ResourceCursorAdapter` drop drown view resource.
resourceCursorAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
// Get a handle for the `Spinner`.
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Set the adapter for the folder `Spinner`.
spinner.setAdapter(resourceCursorAdapter);
因为在打开和关闭时ResourceCursorAdapter
使用相同来填充微调器,所以in和的 id必须相同。bindView
TextView
spinner_dropdown_item.xml
spinner_item.xml
spinner_item.xml
在layout
文件夹中:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinner_item_textview"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:maxLines="1"
android:ellipsize="end"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:textSize="18sp"
android:textColor="@color/primaryTextColor" />