以前弄的一个下拉框时自带的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. C语言的scanf函数

    一. 变量的内存分析 1. 字节和地址 1> 内存以“字节为单位”,Oxffc1,Oxffc2,Oxffc3,Oxffc4....都是字节 ,0x表示的是十六进制 2> 不同类型占用的字节 ...

  2. 爬起点小说 day01

    先介绍下我自己爬起点小说的思路: 1.爬取所有的类型列表 a.链接存redis中 类型表:novel_list 具体每一种类型:bnovel_all_list(把novel_list和bnovel_l ...

  3. 谈谈微服务中的 API 网关(API Gateway)

    前言 又是很久没写博客了,最近一段时间换了新工作,比较忙,所以没有抽出来太多的时间写给关注我的粉丝写一些干货了,就有人问我怎么最近没有更新博客了,在这里给大家抱歉. 那么,在本篇文章中,我们就一起来探 ...

  4. 用Python删除本地目录下某一时间点之前创建的所有文件

    因为工作原因,需要定期清理某个文件夹下面创建时间超过1年的所有文件,所以今天集中学习了一下Python对于本地文件及文件夹的操作.网上 这篇文章 简明扼要地整理出最常见的os方法,抄袭如下: os.l ...

  5. Docker: 限制容器可用的内存

    默认情况下容器使用的资源是不受限制的.也就是可以使用主机内核调度器所允许的最大资源.但是在容器的使用过程中,经常需要对容器可以使用的主机资源进行限制,本文介绍如何限制容器可以使用的主机内存. 为什么要 ...

  6. COM组件转换为.NET元数据2

    上一篇通过命令的方式实现COM组件与.NET元素的转换.这次直接在VS中转换. 以下为步骤:

  7. Solr6.5.0配置solrcore图文详解

    准备环境: solr6.5.0安装完成 jdk1.8 solrhome配置成功 详情:

  8. mysql数据库的安装及体系说明

    第1章 MySQL介绍 1.1 数据的定义 数据是指对客观事件进行记录并可以鉴别的符号,是对客观事物的性质.状态以及相互关系等进行记载的物理符号或这些物理符号的组合,是可识别.抽象的符号 1.2 数据 ...

  9. idea/eclipse下Maven工程集成web服务(tomcat、jetty)

     idea/eclipse下Maven工程集成web服务 转载请注明出处:http://www.cnblogs.com/funnyzpc/p/8093554.html 应用服务器最常用的一般有这哥仨: ...

  10. 关于myeclipse8.6的优化设置

    1.设置一些类型文件的编码格式: >preferences>General>Content  Types>Text>javaScript sourcefile 相同的操作 ...