Xamarin android spinner的使用方法
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <Spinner android:id="@+id/spin_status" android:layout_width="300dp" android:layout_height="50dp" android:spinnerMode="dropdown" /> </LinearLayout>
xamarin android spinner的如何使用呢,大多数web开发人员经常会听到DropDownList 和Combobox 这种下拉选择框,spinner 的意思差不多,有道词典一下意思是“下拉列表”、“台湾斯普”,“下拉列表组件”,“微调控件”。xamarin android中我写几个简单的例子来掌握这个spinner的用法
spinner的相关属性:
- android:dropDownHorizontalOffset:设置列表框的水平偏移距离
- android:dropDownVerticalOffset:设置列表框的水平竖直距离
- android:dropDownSelector:列表框被选中时的背景
- android:dropDownWidth:设置下拉列表框的宽度
- android:gravity:设置里面组件的对其方式
- android:popupBackground:设置列表框的背景
- android:prompt:设置对话框模式的列表框的提示信息(标题),只能够引用string.xml中的资源id,而不能直接写字符串
- android:spinnerMode:列表框的模式,有两个可选值:dialog:对话框风格的窗口dropdown:下拉菜单风格的窗口(默认)
- 可选属性:android:entries:使用数组资源设置下拉列表框的列表项目
1.一个最基本的用法,使用ArrayAdapter数据源,默认的SimpleSpinnerItem样式来绑定数据
看一下效果图:
瞬间感觉丑爆了,的确如此,因为用的的android 默认的SimpleSpinnerItem样式。
布局页加一个Spinner的控件就ok了:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <Spinner android:id="@+id/spin_status" android:layout_width="300dp" android:layout_height="50dp" android:spinnerMode="dropdown" /> </LinearLayout>
使用ArrayAdapter为spinner绑定数据
protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); Spinner spinner1 = (Spinner)FindViewById(Resource.Id.spin_status); string[] myWeb = new string[] {"闲蛋客","闲蛋客网赚","闲蛋客博客" }; ArrayAdapter adapter = new ArrayAdapter(this,Android.Resource.Layout.SimpleSpinnerItem,myWeb); spinner1.Adapter = adapter; }
代码很少一个spinner最基本的用法就是这样,当然string[] 数据 ,也可以写Xml文件中,那就写一个体验一下吧
首先在布局文件中加上一个entries属性就ok了
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <Spinner android:id="@+id/spin_status" android:layout_width="300dp" android:layout_height="50dp" android:spinnerMode="dialog" android:entries="@array/myWeb" /> </LinearLayout>
写一个xml文件:
<?xml version="1.0" encoding="utf-8" ?> <resources> <string-array name="myWeb"> <item>闲蛋客</item> <item>闲蛋客网赚</item> <item>闲蛋客博客</item> </string-array> </resources>
Activity中就不要写绑定数据的代码了。
我们看一下结果是不是一样的,注意一下这个spinnerMode 显示的模式 值有两种 ,dialog ,dropdown (默认的是这个)
似乎效果比上一个SimpleSpinnerItem默认的样式要好一点,但终究两个都是默认很丑陋对不对。这个时候我们就要自己写一个布局吧。
使用自定义布局的方式绑定Spinner的数据源:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/img_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_web" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>数据适配器 MyAdapter.cs 比较普通一点的
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Android.App; using Android.Content; using Android.OS; using Android.Runtime; using Android.Views; using Android.Widget; using Java.Lang; namespace SpinnerDemo.Resources.Adapter { public class Xiandnake { public int Icon { get; set; } public string Web { get; set; } public Xiandnake(int icon ,string web) { this.Icon = icon; this.Web = web; } } public class MyAdapter:BaseAdapter { private List<Xiandnake> data; private Context context; public MyAdapter(Context context, List<Xiandnake> data) { this.context = context; this.data = data; } public override int Count { get { return data.Count; } } public override Java.Lang.Object GetItem(int position) { return null; } public override long GetItemId(int position) { return position; } public override View GetView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { convertView = LayoutInflater.From(context).Inflate(Resource.Layout.spinner_item_xiandanke, null); holder = new ViewHolder(); holder.Img_icon = convertView.FindViewById<ImageView>(Resource.Id.img_icon); holder.Tv_Web = convertView.FindViewById<TextView>(Resource.Id.tv_web); convertView.Tag = holder; } else { holder = (ViewHolder)convertView.Tag; } holder.Tv_Web.Text = data[position].Web; holder.Img_icon.SetImageResource(data[position].Icon); return convertView; } } public class ViewHolder:Java.Lang.Object { public ImageView Img_icon { get; set; } public TextView Tv_Web { get; set; } } }
在activity中绑定数据,MainActivity.cs
using System; using Android.App; using Android.Content; using Android.Runtime; using Android.Views; using Android.Widget; using Android.OS; using SpinnerDemo.Resources.Adapter; using System.Collections.Generic; namespace SpinnerDemo { [Activity(Label = "SpinnerDemo", MainLauncher = true, Icon = "@drawable/icon")] public class MainActivity : Activity { int count = 1; protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.Main); List<Xiandnake> data = new List<Xiandnake>(); data.Add(new Xiandnake(Resource.Id.img_icon,"闲蛋客")); data.Add(new Xiandnake(Resource.Id.img_icon, "闲蛋客博客")); data.Add(new Xiandnake(Resource.Id.img_icon, "闲蛋客网赚")); MyAdapter adapter = new MyAdapter(this,data); Spinner spinner = (Spinner)FindViewById(Resource.Id.spinner_xiandanke); spinner.SetSelection(2); //设置默认的选择项 spinner.Adapter = adapter; spinner.ItemSelected += delegate { //int index = spinner.SelectedItemId; int position = spinner.SelectedItemPosition; Toast.MakeText(this, "你选择的是" + data[spinner.SelectedItemPosition].Web, ToastLength.Short).Show(); }; } } }
例子写完,突然感觉毫无创新,这个MyAdapter 值得推敲,完全可以自己重新一个更好的。有愧了。下载的链接地址还是发一下:http://download.csdn.net/detail/kebi007/9675791
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构