您可以创建自己的适配器,并且可以根据需要更改所有颜色。我使用 CardView 来更改 Spinner 的背景,并且 Spinner 与 CardView 一起看起来更好。
您的 xml(将 Spinner 的约束替换为 CardView)
<androidx.cardview.widget.CardView
android:id="@+id/cardview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="80dp"
app:cardCornerRadius="4dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.253">
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</androidx.cardview.widget.CardView>
spinner.xml(用于更改文本和背景颜色)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:textSize="16sp" />
</LinearLayout>
AdapterSpinner.class
public class AdapterSpinner extends ArrayAdapter{
Context context;
List<String> liste;
int dropDownBackground, dropDownTextColor, textColor;
public AdapterSpinner(Context context, CardView cv, Spinner spinner,List<String> liste, int background, int textColor, int dropDownBackground, int dropDownTextColor) {
super(context, 0, liste);
this.context = context;
this.liste = liste;
this.textColor = textColor;
this.dropDownTextColor = dropDownTextColor;
this.dropDownBackground = dropDownBackground;
if (cv != null) {
cv.setCardBackgroundColor(background);
}
spinner.getBackground().setColorFilter(textColor, PorterDuff.Mode.SRC_IN);
}
@NonNull
@Override
public View getView(int position, @Nullable View view, @NonNull ViewGroup parent) {
if (view == null) {
view = LayoutInflater.from(context).inflate(R.layout.spinner, parent, false);
}
TextView tv = view.findViewById(R.id.tv_spinner);
tv.setText(getItem(position).toString());
tv.setTextColor(textColor);
view.setPadding(20, 0, 0, 0);
return view;
}
@Override
public View getDropDownView(int position, @Nullable View view, @NonNull ViewGroup parent) {
if (view == null) {
view = LayoutInflater.from(getContext()).inflate(R.layout.spinner, parent, false);
}
TextView tv = view.findViewById(R.id.tv_spinner);
tv.setText(getItem(position).toString());
tv.setTextColor(dropDownTextColor);
tv.setPadding(20, 10, 20, 10);
view.setBackgroundColor(dropDownBackground);
return view;
}
}
在您的活动中:
int background = ContextCompat.getColor(this, R.color.white);
int textColor = ContextCompat.getColor(this, R.color.black);
int dropBackground = ContextCompat.getColor(this, R.color.white);
int dropTextColor = ContextCompat.getColor(this, R.color.black);
Spinner sp = findViewById(R.id.spinner);
CardView cv = findViewById(R.id.cardview);
AdapterSpinner adapter = new AdapterSpinner(this, cv, sp, list, background, textColor, dropBackground, dropTextColor);
sp.setAdapter(adapter);