android spinner 背景,在android xamarin中为spinner添加背景颜色和图像

public class SpinnerAdapter extends ArrayAdapter {

private Context mContext;

private ArrayList listState;

public SpinnerAdapter(Context context,int resource,ArrayList objects) {

super(context,resource,objects);

this.mContext = context;

this.listState = objects;

}

@Override

public View getDropDownView(int position,View convertView,ViewGroup parent) {

return getCustomView(position,convertView,parent,true);

}

@Override

public View getView(int position,false);

}

@SuppressLint("InflateParams")

public View getCustomView(int position,ViewGroup parent,boolean isDropDown) {

final ViewHolder holder;

if (convertView == null) {

LayoutInflater layoutInflator = LayoutInflater.from(mContext);

convertView = layoutInflator.inflate(R.layout.spinner_item_layout,null);

holder = new ViewHolder();

holder.mTextView = (TextView) convertView.findViewById(R.id.text);

holder.mCheckedImage = (ImageView) convertView

.findViewById(R.id.checkbox);

holder.mBgLayout= (RelativeLayout) convertView

.findViewById(R.id.bg);

convertView.setTag(holder);

} else {

holder = (ViewHolder) convertView.getTag();

}

holder.mTextView.setText(listState.get(position).getTitle());

if (isDropDown) {

if (listState.get(position).isSelected()) {

holder.mCheckedImage.setVisibility(View.VISIBLE);

holder.mBgLayout.setBackgroundColor(android.Color.BLUE); // not sure if the syntax is correct. you need to check this line

} else {

holder.mCheckedImage.setVisibility(View.INVISIBLE);

holder.mBgLayout.setBackgroundColor(android.Color.YELLOW); // not sure if the syntax is correct. you need to check this line

}

}

return convertView;

}

private class ViewHolder {

private TextView mTextView;

private ImageView mCheckedImage;

private RelativeLayout mBgLayout;

}

public void setSpinnerAdapter(ArrayList spinnerItems) {

this.listState = spinnerItems;

notifyDataSetChanged();

}

}

private void setBottelCountData() {

final String[] select_qualification = { "","1","2","3" };

bottelCountList = new ArrayList<>();

for (int i = 0; i < select_qualification.length; i++) {

SpinnerItem spinnerItem = new SpinnerItem();

spinnerItem.setTitle(select_qualification[i]);

if (i == 2) {

spinnerItem.setSelected(false);

} else {

spinnerItem.setSelected(false);

}

bottelCountList.add(spinnerItem);

}

bottelCountAdapter = new SpinnerAdapter(getActivity(),bottelCountList);

bottelCountAdapter.setDropDownViewResource(R.layout.spinner_item_layout);

bottel_Count_Spinner.setAdapter(bottelCountAdapter);

bottel_Count_Spinner.setSelection(3);

}

bottel_Count_Spinner

.setOnItemSelectedListener(new OnItemSelectedListener() {

public void onItemSelected(AdapterView> parent,View view,int position,long id) {

if (position == 0) {

for (int count = 0; count < bottelCountList.size(); count++) {

bottelCountList.get(count).setSelected(false);

}

noOfBottel = 0;

return;

}

bottelCountList.get(position).setSelected(true);

for (int count = 0; count < bottelCountList.size(); count++) {

if (position != count) {

bottelCountList.get(count).setSelected(false);

}

}

bottel_Count_Spinner.setPrompt("Hello");

bottelCountAdapter.setSpinnerAdapter(bottelCountList);

noOfBottel = Integer.parseInt(bottelCountList.get(

position).getTitle());

}

public void onNothingSelected(AdapterView> parent) {

}

});

使用这个xml

android:layout_width="wrap_content"

android:id="@+id/bg"

android:layout_height="wrap_content" >

android:id="@+id/text"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_toRightOf="@+id/checkbox"

android:layout_centerVertical="true"

android:padding="10dp"/>

android:id="@+id/checkbox"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:padding="10dp"

android:src="@android:drawable/checkbox_on_background"

android:contentDescription="@string/app_name"

android:layout_marginLeft="10dp"

android:layout_alignParentStart="true" />

我希望这会对你有所帮助.如果您发现任何错误,请告知

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值