我正在使用包含长项和短项的AppCompatSpinner控件。选择短项目时,微调器太宽,其图标显示距离文本太远,因为文本太短。如果项目很小,有没有办法让微调器更小?
答案 0 :(得分:0)
这是非常hacky。
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
String text = adapterView.getItemAtPosition(position).toString();
Rect bounds = new Rect();
Paint textPaint = new Paint();
textPaint.measureText(text);
textPaint.setTextSize(45);
textPaint.getTextBounds(text, 0, text.length(), bounds);
int width = bounds.width();
ViewGroup.LayoutParams params = spinner.getLayoutParams();
params.width = width + 290;
spinner.setLayoutParams(params);
}
@Override public void onNothingSelected(AdapterView<?> adapterView) {
}
});
希望有所帮助:)