个别 Spinner 项目的自定义格式不起作用

custom formatting of individual Spinner items not working

我想在 Spinner 中显示的第一个项目上显示特定的文本。 虽然调用并执行了下面的代码,但微调器中的第一项未按预期格式设置。

public class ClubsAdapter extends ArrayAdapter<String> {
private static List<String> mClubs = null;
public TextView tv;

public ClubsAdapter(Context context, int textViewResourceId, List<String> dataset) {
    super(context, textViewResourceId, dataset);
    mClubs = dataset;

    ListIterator<String> listIterator = mClubs.listIterator();
    String s = listIterator.next();
    listIterator.set(s.substring(1));
}


public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;

    if (v == null) {
        LayoutInflater inflater =
                (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.club, null);
    }
    
    tv = v.findViewById(R.id.club);

    String club = mClubs.get(position);
    if (club != null) {
        if (position == 0) {
            tv.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC));
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                tv.setTextAppearance(android.R.style.TextAppearance_Medium);
            } else {
                tv.setTextAppearance(mContext, android.R.style.TextAppearance_Medium);
            }
        } else {
            tv.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                tv.setTextAppearance(android.R.style.TextAppearance_Large);
            } else {
                tv.setTextAppearance(mContext, android.R.style.TextAppearance_Large);
            }
        }

        tv.setText(club);
    }

    return v;
}

}

这里是 XML:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/club"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />

这就是我使用此适配器的方式:

        Spinner clubSpinner = (Spinner) root.findViewById(R.id.clubSpinner);
        ArrayAdapter<String> clubsAdapter = new ClubsAdapter(getContext(), R.layout.club, clubNames);
        clubsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        clubSpinner.setAdapter(clubsAdapter);

*** 编辑 *** 根据要求,这是 Spinner 的 XML:

    <Spinner            android:id="@+id/clubSpinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@id/courseSpinner"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

setTextAppearance之后setTypeface移动。后者还设置字体。 简单又愚蠢...

正确的代码是

    if (position == 0) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            tv.setTextAppearance(android.R.style.TextAppearance_Medium);
        } else {
            tv.setTextAppearance(mContext, android.R.style.TextAppearance_Medium);
        }
        tv.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC));
    } else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            tv.setTextAppearance(android.R.style.TextAppearance_Large);
        } else {
            tv.setTextAppearance(mContext, android.R.style.TextAppearance_Large);
        }
        tv.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
    }