如何在Android中的微调器(下拉菜单)中添加单独的文本颜色和背景颜色



我想要android微调器的白色背景(下拉(和微调器中文本的黑色,但是当我将背景颜色设置为白色时,我的文本也会变为白色并消失。我如何才能确保文本是黑色的,背景是白色的。附件是我的xml文件的代码片段。

<Spinner
android:id="@+id/spinnerDropDown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginEnd="80dp"
android:layout_marginRight="80dp"
android:background="@color/white"
android:text="@color/black"
android:spinnerMode="dropdown"
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" />

我希望它看起来像这样,在这里附上的图像

您可以创建自己的适配器,并可以根据需要更改所有颜色。我使用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>

AdapterPinner.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);

最新更新