要改变Spinner控件的字体大小,可以通过自定义Spinner的样式和设置Adapter来实现。
- 创建一个自定义的Spinner样式(styles.xml):
- 在布局文件中使用自定义的Spinner样式:
- 创建一个自定义的Adapter并设置字体大小(CustomAdapter.java):
public class CustomAdapter extends ArrayAdapter{ private Context mContext; private int mResource; private List mData; private LayoutInflater mInflater; public CustomAdapter(Context context, int resource, List data) { super(context, resource, data); this.mContext = context; this.mResource = resource; this.mData = https://www.yisu.com/ask/data;"hljs">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; } }
- 在Activity中设置Spinner的Adapter:
Spinner spinner = findViewById(R.id.spinner); Listdata = https://www.yisu.com/ask/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控件的字体大小。