以前弄的一个下拉框时自带的spinner,感觉好丑,实际效果实在满足不了基本的UI界面要求,还是自己动手丰衣足食,看了网上关于android中自定义spinner的文章,感觉实现原理还是比较简单,所以这里用xamarin android来实现自定义spinner样式。参考文章:http://blog.csdn.net/jdsjlzx/article/details/41316417

实现原理

1.TextView中显示选择的内容,右边显示上下的箭头,点击事件中设置不同的箭头

2.TextView下显示的是一个PoputWindow,PoputWindow中显示的自定义ListView,在TextView单击事件中显示ListView就是下拉的选择项。

先来看看最终的效果图

代码实现的过程主要分为以下几个部分:

  1. MainActivity布局实现(这个就是一个TextView)
  2. PoputWindow布局和ListView布局实现
  3. 自定类SpinerPopWindow继承PoputWIndow的实现,ListView适配器类的实现(实现的关键)
  4. MainActivity.cs中TextView事件监听、ListView的Item单击事件的监听实现

1. MainActivity布局实现

MainActivity中只有一个TextView

<?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"
android:paddingRight="20dp"
android:background="@color/color_ffffff"
android:paddingLeft="20dp">
<TextView
android:id="@+id/tv_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/color_dedede"
android:drawableRight="@drawable/icon_down"
android:padding="10dp"
android:textColor="@color/content_color"
android:textSize="20sp" />
</LinearLayout>

2.PoputWindow布局和ListView布局实现

1.PoputWindow里面放的是一个ListView控件。spinner_window_layout.xml的background需要添加边框、设置背景颜色

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:orientation="vertical">
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:background="@drawable/shape_popupwindow_list_bg"
android:scrollbars="none" >
</ListView>
</LinearLayout>

2.ListView布局文件spinner_item_layout里面只有一个TextView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="fdsfdsfdsf"
android:textSize="20sp"/>
</LinearLayout>

3.自定类SpinerPopWindow继承PoputWIndow的实现,ListView适配器类的实现(实现的关键)

spinnerPopWindow.cs需要继承PoputWindow,初始化ListView。ListView的Adapter类LVAdapter就不多说了。

using System.Collections.Generic;
using System.Linq;
using Android.Content;
using Android.Views;
using Android.Widget;
using Android.Graphics.Drawables;
using static Android.Views.ViewGroup;
namespace customSpinnerDemo
{
public class SpinerPopWindow<T>:PopupWindow
{
private ListView listView;
private List<T> list;
private LVAdapter<T> adapter;
private Context context;
private LayoutInflater inflater;
public SpinerPopWindow(Context _context ,List<T> _list, AdapterView.IOnItemClickListener itemClickListener)
{
context = _context;
list = _list;
inflater = LayoutInflater.From(_context);
InitListView(itemClickListener);
}
private void InitListView(AdapterView.IOnItemClickListener itemClickListener)
{
View view = inflater.Inflate(Resource.Layout.spiner_window_layout, null);
this.ContentView = view;
//LayoutParams
var parentView = (ViewGroup)view;
var child = parentView.GetChildAt(0);
this.Width = LayoutParams.WrapContent;
this.Height = LayoutParams.WrapContent;
this.Focusable = true;
ColorDrawable cdw = new ColorDrawable(Android.Graphics.Color.Transparent);
SetBackgroundDrawable(cdw);
//View childView = ContentView listView = view.FindViewById<ListView>(Resource.Id.listview);
adapter = new customSpinnerDemo.LVAdapter<T>(context,list);
listView.Adapter = adapter;
listView.OnItemClickListener = itemClickListener;
}
}
public class LVAdapter<T> : BaseAdapter
{
private List<T> list;
private Context context;
public LVAdapter(Context _context,List<T> _list)
{
context = _context;
list = _list;
}
public override int Count
{
get{
return list.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)
{
holder = new ViewHolder();
convertView = LayoutInflater.From(context).Inflate(Resource.Layout.spiner_item_layout,parent,false);
holder.tvName = convertView.FindViewById<TextView>(Resource.Id.tv_name);
convertView.Tag = holder;
}
else
{
holder = (ViewHolder)convertView.Tag;
}
holder.tvName.Text = list[position].ToString();
return convertView;
}
private class ViewHolder:Java.Lang.Object {
internal TextView tvName;
}
}
}

4.MainActivity.cs中TextView事件监听、ListView的Item单击事件的监听实现

实现的原理就是一下代码了

using Android.App;
using Android.Widget;
using Android.OS;
using System.Collections.Generic;
using static Android.Widget.AdapterView;
using Android.Views;
using Android.Graphics.Drawables;
using Android.Support.V7.App;
namespace customSpinnerDemo
{
[Activity(Label = "customSpinnerDemo", MainLauncher = true, Icon = "@drawable/icon",Theme = "@style/AppTheme")]
public class MainActivity : AppCompatActivity, IOnItemClickListener, PopupWindow.IOnDismissListener
{
private List<string> list;
private TextView tv_value;
private SpinerPopWindow<string> SpinnerPopwindow;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView (Resource.Layout.Main);
tv_value = FindViewById<TextView>(Resource.Id.tv_value);
tv_value.Click += (s,e) =>
{
SpinnerPopwindow.Width = tv_value.Width;
SpinnerPopwindow.ShowAsDropDown(tv_value);
SetTextImage(Resource.Drawable.icon_up);
};
list = new List<string>() { "科比","詹姆斯","韦德","波什"};
SpinnerPopwindow = new SpinerPopWindow<string>(this,list,this);
SpinnerPopwindow.SetOnDismissListener(this);
}
/// <summary>
/// 给TextView右边设置图片
/// </summary>
private void SetTextImage(int resId)
{
//var drawable =GetDrawable(resId);
Drawable drawable = Resources.GetDrawable(resId);
drawable.SetBounds(0,0,drawable.MinimumWidth,drawable.MinimumHeight);
tv_value.SetCompoundDrawables(null,null,drawable,null);
}
/// <summary>
/// popupWindow 显示的ListView的item点击事件
/// </summary>
public void OnItemClick(AdapterView parent, View view, int position, long id)
{
SpinnerPopwindow.Dismiss();
tv_value.Text=list[position].ToString();
Toast.MakeText(this,"点击了:"+list[position],ToastLength.Long).Show();
}
/// <summary>
/// popupWindow取消
/// </summary>
public void OnDismiss()
{
SetTextImage(Resource.Drawable.icon_down);
}
}
}

小结

虽然实现原理比较简单,但是要把一个下拉框做的能够调用简单,代码多处复用,代码量小而简单,还是需要琢磨的。

,如果代码有什么看不懂的地方,直接看Github吧:https://github.com/MaChuZhang/Xamarin-Android-Custom-View

作者:张林

标题:xamarin android自定义spinner 原文地址:http://blog.csdn.net/kebi007/article/details/74856836

转载随意注明出处

[置顶] xamarin android自定义spinner的更多相关文章

  1. [置顶] xamarin android自定义标题栏(自定义属性、回调事件)

    自定义控件的基本要求 这篇文章就当是自定义控件入门,看了几篇android关于自定义控件的文章,了解了一下,android自定义控件主要有3种方式: 自绘控件:继承View类,所展示的内容在OnDra ...

  2. [置顶] xamarin android使用zxing扫描二维码

    好久没写了,这片文章篇幅不长,概述一下在xamarin android中用 ZXing.Net.Mobile库扫描二维码读取url的示例.扫码支付,扫码登录,App上各种各样的扫码,好像没个扫码的就有 ...

  3. [置顶] Xamarin android沉浸式状态栏

    虽然关于android "沉浸式"状态栏有很多博客介绍过,从小菜到大神无一例外.我第一次看到这种"沉浸"式的效果我也以为真的是这么叫,然而根本不是这么回事,完全 ...

  4. [置顶] xamarin android toolbar(踩坑完全入门详解)

    网上关于toolbar的教程有很多,很多新手,在使用toolbar的时候踩坑实在太多了,不好好总结一下,实在浪费.如果你想学习toolbar,你肯定会去去搜索androd toolbar,既然你能看到 ...

  5. [置顶] xamarin android使用gps定位获取经纬度

    看了文章你会得出以下几个结论 1.android定位主要有四种方式GPS,Network(wifi定位.基站定位),AGPS定位 2.绝大部分android国产手机使用network进行定位是没有作用 ...

  6. [置顶] xamarin android 布局尺寸了解

    为了使UI界面在不同大小的移动端显示器上能够正常显示,大家可能都知道使用sp作为字体大小的单位,dp作为其他元素长度的单位. 前几天看了一篇文章关于 App设计规范的,文章用心写的非常好,这里是链接  ...

  7. [置顶] xamarin android Fragment实现底部导航栏

    前段时间写了篇关于Fragment的文章,介绍了基础的概念,用静态和动态的方式加载Fragment  Xamarin Android Fragment的两种加载方式.下面的这个例子介绍xamarin ...

  8. [置顶] Xamarin android中使用signalr实现即时通讯

    前面几天也写了一些signalr的例子,不过都是在Web端,今天我就来实践一下如何在xamarin android中使用signalr,刚好工作中也用到了这个,也算是总结一下学到的东西吧,希望能帮助你 ...

  9. [置顶] Xamarin android如何调用百度地图入门示例(一)

    在Xamarin android如何调用百度地图呢? 首先我们要区分清楚,百度地图这是一个广泛的概念,很多刚刚接触这个名词"百度地图api",的确是泛泛而谈,我们来看一下百度地图的 ...

随机推荐

  1. Unity与iOS原生代码之间的相互调用

    1.Unity调用iOS: 1.1.在Unity C#中: [ DllImport( "__Internal" )] private static extern int _show ...

  2. SpringCloud学习笔记(1)——Eureka

    Spring Cloud Spring Cloud为开发者快速构建通用的分布式系统(例如:配置管理.服务发现.断路器.智能路由.微代理.控制总线.一次性的Token.全局锁.领导者选举.分布式会话.集 ...

  3. C# Excel写入数据及图表

    开发工具:VS2017 语言:C DotNet版本:.Net FrameWork 4.0及以上 使用的DLL工具名称:GemBox.Spreadsheet.dll (版本:37.3.30.1185) ...

  4. NYOJ 417 死神来了 鸽巢原理

    死神来了 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 有一天,王小子在遨游世界时,遇到了一场自然灾害.一个人孤独的在一个岛上,没有吃的没有喝的.在他饥寒交迫将要死亡时 ...

  5. Cs Round#54 E Late Edges

    题意:给定一个无向图,你从结点1开始走,每经过一条边需要1的时间,每条边都有一个开放时间,只有当目前所用的时间大于等于开放时间时,这条边才可以被经过.每一单位时间你都必须经过一条边,问最快什么时候可以 ...

  6. springBoot actuator监控配置及使用

    准备环境: 一个springBoot工程 第一步:添加以下依赖 <dependency> <groupId>org.springframework.boot</group ...

  7. Java开发小技巧(二):自定义Maven依赖

    前言 我们在项目开发中经常会将一些通用的类.方法等内容进行打包,打造成我们自己的开发工具包,作为各个项目的依赖来使用. 一般的做法是将项目导出成Jar包,然后在其它项目中将其导入,看起来很轻松,但是存 ...

  8. Java爬虫——人人网模拟登录

    人人网登录地址:http://www.renren.com/ 此处登录没有考虑验证码验证码. 首先对登录方法进行分析 有两种方法. 一)在Elements中分析源码 发现登录点击后的事件是http:/ ...

  9. Android Things 专题6 完整的栗子:运用TensorFlow解析图像

    文| 谷歌开发技术专家 (GDE) 王玉成 (York Wang) 前面絮叨了这么多.好像还没有一个整体的概念.我们怎样写一个完整的代码呢? 如今深度学习非常火,那我们就在Android Things ...

  10. .Net 5分钟搞定网页实时监控

    一.为什么会用到网页实时监控 LZ最近在无锡买房了,虽然在上海工作,但是上海房价实在太高无法承受,所以选择还可以接受的无锡作为安身之地.买过房的小伙伴可能知道买房的流程,买房中间有一步很重要的就是需要 ...