要改变Spinner控件的字体大小,可以通过自定义Spinner的样式和设置Adapter来实现。
<style name="SpinnerStyle" parent="Widget.AppCompat.Spinner">
<item name="android:textSize">16sp</item> <!-- 设置字体大小 -->
</style>
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/SpinnerStyle" />
public class CustomAdapter extends ArrayAdapter<String> {
private Context mContext;
private int mResource;
private List<String> mData;
private LayoutInflater mInflater;
public CustomAdapter(Context context, int resource, List<String> data) {
super(context, resource, data);
this.mContext = context;
this.mResource = resource;
this.mData = data;
this.mInflater = LayoutInflater.from(context);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(mResource, parent, false);
}
TextView textView = convertView.findViewById(android.R.id.text1);
textView.setText(mData.get(position));
textView.setTextSize(16); // 设置字体大小
return convertView;
}
@Override
public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(mResource, parent, false);
}
TextView textView = convertView.findViewById(android.R.id.text1);
textView.setText(mData.get(position));
textView.setTextSize(16); // 设置字体大小
return convertView;
}
}
Spinner spinner = findViewById(R.id.spinner);
List<String> data = new ArrayList<>();
data.add("Option 1");
data.add("Option 2");
data.add("Option 3");
CustomAdapter adapter = new CustomAdapter(this, android.R.layout.simple_spinner_item, data);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
通过以上步骤,就可以改变Spinner控件的字体大小。