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

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

概述我正在尝试为微调器及其项目添加灰色背景颜色,想要将文本颜色更改为蓝色,并希望将图像放在微调器的右侧.目前我在笔记设备中获得白色并在标签设备中获得黑色. 我是非常新的 android请帮助我. main.xml中 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientati 我正在尝试为微调器及其项目添加灰色背景颜色,想要将文本颜色更改为蓝色,并希望将图像放在微调器的右侧.目前我在笔记设备中获得白色并在标签设备中获得黑色.
我是非常新的 android请帮助我.

main.xml中

<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:orIEntation="horizontal"    androID:layout_wIDth="fill_parent"    androID:layout_height="fill_parent">    <Spinner        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:ID="@+ID/movIEsspinner"        androID:prompt="@string/movIE_prompt" />    <ImageVIEw        androID:src="@androID:drawable/Icon"        androID:layout_wIDth="30dp"        androID:layout_height="30dp"        androID:ID="@+ID/imageVIEw1" /></linearLayout>

itemrow.xml

<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="fill_parent"    androID:layout_height="wrap_content"    androID:orIEntation="vertical"    androID:background="#ffffff"    androID:padding="3dip">    <TextVIEw        androID:padding="3dip"        androID:layout_margintop="2dip"        androID:textcolor="#C11B17"        androID:textStyle="bold"        androID:ID="@+ID/company"        androID:layout_marginleft="5dip"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content" /></relativeLayout>

mainActivity.cs

var movIEsspinner1 = FindVIEwByID<Spinner>(Resource.ID.movIEsspinner);            movIEsspinner1.Adapter = new MovIEsAdapter(this,MovIEsRepository.MovIEs);
解决方法
public class SpinnerAdapter extends ArrayAdapter<SpinnerItem> {    private Context mContext;    private ArrayList<SpinnerItem> ListState;    public SpinnerAdapter(Context context,int resource,ArrayList<SpinnerItem> 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<SpinnerItem> 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

<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="wrap_content"    androID:ID="@+ID/bg"    androID:layout_height="wrap_content" >    <TextVIEw        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"/>    <ImageVIEw        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" /></relativeLayout>

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

总结

以上是内存溢出为你收集整理的在android xamarin中为spinner添加背景颜色和图像全部内容,希望文章能够帮你解决在android xamarin中为spinner添加背景颜色和图像所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1123754.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存