当我将它的适配器更改为从ArrayAdapter扩展的自定义适配器时,我的微调器大小有问题。
如果我以标准方式设置适配器,我会在styles.xml中设置它的样式
ArrayAdapter<String> adapter = new ArrayAdapter<>(context, android.R.layout.simple_spinner_item, spinnerArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
但我已经为微调器实现了自定义适配器,并以这种方式使用它:
SpinnerImageAndTextAdapter adapter = new SpinnerImageAndTextAdapter(context, spinnerArray, hasItem, arrayIndex);
spinner.setAdapter(adapter);
这是我的Implemented SpinnerImageAndTextAdapter类
public class SpinnerImageAndTextAdapter extends ArrayAdapter<String> {
private static final Integer[] imageArray = {R.drawable.ic_orange, R.drawable.ic_green};
private Context context;
private String[] contentArray;
private String[] arrayIndex;
private boolean hasItem;
public SpinnerImageAndTextAdapter(Context context, String[] objects, boolean hasItem, String[] arrayIndex) {
super(context, android.R.layout.simple_spinner_item, objects);
this.context = context;
this.contentArray = objects;
this.arrayIndex= arrayIndex;
this.hasItem= hasItem;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
private View getCustomView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//convertView = inflater.inflate(R.layout.template_spinner_item_image_and_text, null);
convertView = inflater.inflate(android.R.layout.simple_spinner_dropdown_item, parent, false);
}
TextView textView = (TextView) convertView.findViewById(android.R.id.text1);
textView.setText(contentArray[position]);
if (hasItem) {
if (arrayIndex[position].contains("blabla1"))
textView.setCompoundDrawablesWithIntrinsicBounds(imageArray[0], 0, 0, 0);
else if (arrayIndex[position].contains("blabla2"))
textView.setCompoundDrawablesWithIntrinsicBounds(imageArray[1], 0, 0, 0);
}
return convertView;
}
这是我对微调器的样式规则
<style name="XTheme.SpinnerGrayStyleSmall" parent="Widget.AppCompat.Spinner">
<item name="android:layout_width">match_parent</item>
<item name="android:background">@drawable/edit_text_style_gray</item>
<item name="android:paddingTop">8dp</item>
<item name="android:paddingBottom">8dp</item>
<item name="android:paddingLeft">6dp</item>
<item name="android:textColor">@color/gray_darker</item>
<item name="android:textColorHint">@color/gray_darker</item>
</style>
问题:为微调器设置自定义适配器后,是否有任何方法可以防止样式不匹配?