我想使用提示创建微调器,遵循其中一个解决方案,我将下拉列表高度的第一个元素设置为0,但它被忽略,第一个元素仍然可见,高度> 0。代码:
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, R.layout.spinner_item) {
@Override
public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View v = super.getDropDownView(position, convertView, parent);
ViewGroup.LayoutParams lp = v.getLayoutParams();
v.setVisibility(View.VISIBLE);
if (position == 0) {
lp.height = 0; //doesn't affect anything
} else {
lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
}
v.setLayoutParams(lp);
return v;
}
@Override
public boolean isEnabled(int position){
return position != 0;
}
};
spinnerAdapter.add(getHint());
spinnerAdapter.addAll(getItems());
spinnerView.setAdapter(spinnerAdapter);
当我尝试设置height = 1
它工作,项目几乎消失,但这不是它应该如何工作。这有可能发生吗?有没有办法将高度精确设置为零?
为微调器创建一个自定义适配器并像这样更改代码也添加位置控制
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = flater.inflate(R.layout.list_itemslayout,parent, false);
}
RowItem rowItem = getItem(position);
TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
txtTitle.setText(rowItem.getTitle());
ImageView imageView = (ImageView) convertView.findViewById(R.id.icon);
imageView.setImageResource(rowItem.getImageId());
return convertView;
}