Android中如何修改MaterialSpinner选中项的背景颜色
Since you're using com.jaredrummler.materialspinner.MaterialSpinner and want a darker background for the selected item ("GAIN 3") to improve readability, here are two reliable approaches to solve this:
Approach 1: Use a Custom Adapter (Recommended)
This method gives you full control over the selected item's appearance by overriding adapter methods to adjust backgrounds and text colors. Here's the implementation:
// Define your spinner items (replace with your actual data) List<String> spinnerOptions = Arrays.asList("Item 1", "GAIN 3", "Item 3", "Item 4"); MaterialSpinner spinner = findViewById(R.id.your_spinner_id); // Create a custom ArrayAdapter to handle selected item styling ArrayAdapter<String> customAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinnerOptions) { @Override public View getView(int position, View convertView, ViewGroup parent) { View view = super.getView(position, convertView, parent); TextView textView = (TextView) view; // Apply styling if this is the selected position if (position == spinner.getSelectedItemPosition()) { textView.setBackgroundColor(getResources().getColor(R.color.dark_background)); textView.setTextColor(getResources().getColor(R.color.white)); } else { // Reset to default for non-selected items textView.setBackgroundColor(getResources().getColor(android.R.color.transparent)); textView.setTextColor(getResources().getColor(android.R.color.black)); } return view; } @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { View view = super.getDropDownView(position, convertView, parent); TextView textView = (TextView) view; // Highlight selected item in the dropdown list if (position == spinner.getSelectedItemPosition()) { textView.setBackgroundColor(getResources().getColor(R.color.dark_background)); textView.setTextColor(getResources().getColor(R.color.white)); } else { textView.setBackgroundColor(getResources().getColor(android.R.color.white)); textView.setTextColor(getResources().getColor(android.R.color.black)); } return view; } }; // Set up the spinner with the custom adapter customAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(customAdapter); // Update your item selected listener to refresh the adapter on selection change spinner.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() { @Override public void onItemSelected(MaterialSpinner view, int position, long id, String item) { text = item; // Your existing logic customAdapter.notifyDataSetChanged(); // Refresh to apply styling } @Override public void onNothingSelected(MaterialSpinner view) { // Handle empty selection if needed } });
Don't forget to add these colors to your res/values/colors.xml file:
<color name="dark_background">#4A4A4A</color> <!-- Adjust to your preferred dark shade --> <color name="white">#FFFFFF</color>
Approach 2: Library-Specific XML Attributes (If Supported)
Some versions of the MaterialSpinner library include built-in attributes for selected item styling. Try adding these to your spinner in XML:
<com.jaredrummler.materialspinner.MaterialSpinner android:id="@+id/your_spinner_id" android:layout_width="match_parent" android:layout_height="wrap_content" app:ms_selectedBackgroundColor="@color/dark_background" app:ms_selectedTextColor="@color/white"/>
If these attributes don't work (depends on your library version), stick with the custom adapter approach—it's more reliable across all versions.
Quick Tips:
- Use
notifyDataSetChanged()in your selection listener to ensure styling updates immediately when the user picks a new item. - Match the background color to your app's theme for a consistent look.
- Test on different Android versions to verify the appearance stays consistent.
内容的提问来源于stack exchange,提问作者Ragul Murugan

