Android 当适配器设置为extended ArrayAdapter时,微调器会更改其大小

Android 当适配器设置为extended ArrayAdapter时,微调器会更改其大小,android,Android,当我将微调器的适配器更改为从ArrayAdapter扩展的自定义适配器时,微调器的大小有问题 若我以标准方式设置适配器,它将显示我如何在styles.xml中设置它的样式 ArrayAdapter<String> adapter = new ArrayAdapter<>(context, android.R.layout.simple_spinner_item, spinnerArray); adapter.setDropDownViewResource(andro

当我将微调器的适配器更改为从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类

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;


}
公共类SpinnerImageAndTextAdapter扩展了ArrayAdapter{
私有静态最终整数[]imageArray={R.drawable.ic_橙色,R.drawable.ic_绿色};
私人语境;
私有字符串[]contentArray;
私有字符串[]arrayIndex;
私人物品;
公共SpinnerImageAndTextAdapter(上下文上下文,字符串[]对象,布尔hasItem,字符串[]arrayIndex){
超级(上下文、android.R.layout.simple\u微调器\u项、对象);
this.context=上下文;
this.contentArray=对象;
this.arrayIndex=arrayIndex;
this.hasItem=hasItem;
}
@凌驾
公共视图getDropDownView(int位置、视图转换视图、视图组父视图){
返回getCustomView(位置、转换视图、父级);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
返回getCustomView(位置、转换视图、父级);
}
私有视图getCustomView(int位置、视图转换视图、视图组父视图){
if(convertView==null){
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
//convertView=充气机.充气(R.layout.template\u微调器\u item\u image\u和\u text,空);
convertView=充气机.充气(android.R.layout.simple\u微调器\u下拉菜单\u项,父项,false);
}
TextView TextView=(TextView)convertView.findViewById(android.R.id.text1);
setText(contentArray[position]);
如果(项目){
if(arrayIndex[position].contains(“blabla1”))
setCompoundDrawableSwithinInstructBounds(imageArray[0],0,0,0);
else if(arrayIndex[position].contains(“blabla2”))
setCompoundDrawableSwithinInstructBounds(imageArray[1],0,0,0);
}
返回视图;
}
以下是我对微调器的样式规则

<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>

匹配父项
@可绘制/编辑\u文本\u样式\u灰色
8dp
8dp
6dp
@颜色/灰色/较深
@颜色/灰色/较深

问题:在为微调器设置自定义适配器后,有什么方法可以防止样式不匹配吗?

我无法了解您的问题。您可以发布实际行为和所需的行为吗?实际上,当我们为任何小部件使用自定义适配器时,我们丢失了多个默认样式,因此轮到您定义自定义样式了e、 它不会从其xml中接受。我解决了这个问题,在定义的getCustomView方法上添加了基于条件的膨胀视图。当此方法从重写的getDropDownView方法调用时,我膨胀了android.R.layout.simple\U spinner\U dropdown\U项,否则是android.R.layout.simple\U spinner\U项布局。
<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>