以前弄的一个下拉框时自带的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. javascript执行机制

    文的目的就是要保证你彻底弄懂javascript的执行机制,如果读完本文还不懂,可以揍我. 不论你是javascript新手还是老鸟,不论是面试求职,还是日常开发工作,我们经常会遇到这样的情况:给定的 ...

  2. Python之三目运算符

    Python语言不像Java.JavaScript等这些语言有类似: 判段的条件?条件为真时的结果:条件为假时的结果 这样的三目运算,但是Python也有自己的三目运算符: 条件为真时的结果 if 判 ...

  3. windows下python3.6 32bit 安装django

    在Windows下python3.6安装Django1.11.3 1.首先下载地址:https://pypi.python.org/pypi/Django/1.11.3 pip install dja ...

  4. ShoneSharp语言(S#)的设计和使用介绍—数值Double

    ShoneSharp语言(S#)的设计和使用介绍 系列(5)- 数值Double 作者:Shone 声明:原创文章欢迎转载,但请注明出处,https://www.cnblogs.com/ShoneSh ...

  5. Numpy数组的基本运算操作

    一.算术运算符 In [3]: a = np.arange(0,5) Out[3]array([0, 1, 2, 3, 4]) In [4]: a+4 Out[4]: array([4, 5, 6, ...

  6. ML笔记:Where does the error come from?

    error来自哪? 来自于偏差Bias和方差Variance. 就如打靶时瞄准一个点f平均,打出的点f星分布在该点周围. 该点与实际靶心f帽的距离就是偏差Bias, 打出的点与该点的分布距离就是方差V ...

  7. Dubbo源码学习--服务发布(ProxyFactory、Invoker)

    上文分析了Dubbo服务发布的整体流程,但服务代理生成的具体细节介绍得还不是很详细.下面将会接着上文继续分析.上文介绍了服务代理生成的切入点,如下: Invoker<?> invoker ...

  8. upload 简单的封装

    upload 最简单的封装类 <?php    class Upload{        public function Up($files){            if($files['na ...

  9. MongoDB-Use --auth parameter with connecting error

    When you use mongoDB started as "mongod --dbpath ../../data/db --auth", and you use the ex ...

  10. 自动化构建工具—gulp的简单配置

    把之前用到的gulp总结整理下,有时候说不出来的,就写出来吧,做个笔记,以后也可以慢慢补充 cnpm i --save-dev gulp 把nodejs模块写到package.json配置文件中,当保 ...