经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
C#-Xamarin的Android项目开发(二)——控件应用
来源:cnblogs  作者:kiba518  时间:2019/2/21 9:24:58  对本文有异议

相信我,这不是一篇吐槽文章。。。。

基础控件

Android的控件和控件样式非常特别,它是一种内联特别高的设计模式,换句话说,它是非常烂的设计。。。。

但在这种特别的关系里还是有一定的规律的,下面我们一起来看看控件的使用方式。 

首先我们定义一个ImageButton,如下:

  1. <ImageButton
  2. android:src="@drawable/toolbar_upload_photo_normal"
  3. android:layout_gravity="right|center_vertical"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content"
  6. android:background="@drawable/btn_weight" />

如上代码所示,我们定义了ImageButton,并且设置了他的Src地址,该地址指向了一个图片。

重点,我们来看这句,background="@drawable/btn_weight;背景色指向了一个资源,为什么用说指向的是个资源呢?因为btn_weight并不是个图片,而是个XML文件。。。。如下图:

那么我们看看btn_weight到底是什么把。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <selector
  3. xmlns:android="http://schemas.android.com/apk/res/android">
  4. <item android:state_window_focused="false" android:state_enabled="true" android:drawable="@drawable/btn_weight_normal" />
  5. <item android:state_enabled="false" android:drawable="@drawable/btn_weight_disable" />
  6. <item android:state_pressed="true" android:drawable="@drawable/btn_weight_press" />
  7. <item android:state_focused="true" android:drawable="@drawable/btn_weight_press" />
  8. <item android:drawable="@drawable/btn_weight_normal" />
  9. </selector>

如上述代码所示,btn_weight里设置了按钮按下时和常规时的背景色。

没错,这种设置方法,确实很绕,按钮按下的事件和背景样式混在了一起设置,但在Android里,我们只能去适应它。

----------------------------------------------------------------------------------------------------

好了,现在基础控件写完了,有没有感觉自己从现代化城市回到了农耕社会。。。。

相信我,用Xamarin开发,你在农耕社会还有个犁耙,用AS开发,你会发现你只能用手挖。。。。

GridView

首先,Android的GridView是我见过最奇葩的列表使用方式。。。

然后,我们开始学习使用它把。

先找到GridView控件,代码如下:

  1. GridView my_grid = this.FindControl<GridView>("my_grid");

接着,我们定义一个适配器,并把他赋值给GridView的的Adapter属性,代码如下:

  1. IListAdapter adapter = new GridAdapter(this, this.Resources);
  2. my_grid.Adapter = adapter;//配置适配器

嗯,这里看上去代码还算简洁,但接下来就不一样了,让我们来看看这个奇葩的适配器吧。

首先,我们看下适配器代码:

  1. public class GridAdapter : BaseAdapter
  2. {
  3. private DisplayMetrics localDisplayMetrics;
  4. private LayoutInflater inflater;
  5. private Resources resources;
  6. public GridAdapter(Context context)
  7. {
  8. resources = context.Resources;
  9. localDisplayMetrics = resources.DisplayMetrics;
  10. inflater = LayoutInflater.From(context);
  11. }
  12. public override int Count => 9;
  13. public override Object GetItem(int position)
  14. {
  15. return null;
  16. }
  17. public override long GetItemId(int position)
  18. {
  19. return position;
  20. }
  21. public override View GetView(int paramInt, View paramView, ViewGroup paramViewGroup)
  22. {
  23. paramView = inflater.Inflate(Resource.Layout.activity_label_item, null);
  24. TextView text = (TextView)paramView.FindViewById(Resource.Id.activity_name);
  25. switch (paramInt)
  26. {
  27. case 0:
  28. {
  29. text.Text = "local";
  30. Drawable draw = this.resources.GetDrawable(Resource.Drawable.home_button_local);
  31. draw.SetBounds(0, 0, draw.IntrinsicWidth, draw.IntrinsicHeight);
  32. text.SetCompoundDrawables(null, draw, null, null);
  33. break;
  34. }
  35. case 1:
  36. {
  37. text.Text = "search";
  38. Drawable draw = this.resources.GetDrawable(Resource.Drawable.home_button_search);
  39. draw.SetBounds(0, 0, draw.IntrinsicWidth, draw.IntrinsicHeight);
  40. text.SetCompoundDrawables(null, draw, null, null);
  41. break;
  42. }
  43. case 2:
  44. {
  45. text.Text = "checkin";
  46. Drawable draw = this.resources.GetDrawable(Resource.Drawable.home_button_checkin);
  47. draw.SetBounds(0, 0, draw.IntrinsicWidth, draw.IntrinsicHeight);
  48. text.SetCompoundDrawables(null, draw, null, null);
  49. break;
  50. }
  51. case 3:
  52. {
  53. text.Text = "promo";
  54. Drawable draw = this.resources.GetDrawable(Resource.Drawable.home_button_promo);
  55. draw.SetBounds(0, 0, draw.IntrinsicWidth, draw.IntrinsicHeight);
  56. text.SetCompoundDrawables(null, draw, null, null);
  57. break;
  58. }
  59. case 4:
  60. {
  61. text.Text = "tuan";
  62. Drawable draw = this.resources.GetDrawable(Resource.Drawable.home_button_tuan);
  63. draw.SetBounds(0, 0, draw.IntrinsicWidth, draw.IntrinsicHeight);
  64. text.SetCompoundDrawables(null, draw, null, null);
  65. break;
  66. }
  67.  
  68. case 5:
  69. {
  70. text.Text = "rank";
  71. Drawable draw = this.resources.GetDrawable(Resource.Drawable.home_button_rank);
  72. draw.SetBounds(0, 0, draw.IntrinsicWidth, draw.IntrinsicHeight);
  73. text.SetCompoundDrawables(null, draw, null, null);
  74. break;
  75. }
  76. case 6:
  77. {
  78. text.Text = "history";
  79. Drawable draw = this.resources.GetDrawable(Resource.Drawable.home_button_history);
  80. draw.SetBounds(0, 0, draw.IntrinsicWidth, draw.IntrinsicHeight);
  81. text.SetCompoundDrawables(null, draw, null, null);
  82. break;
  83. }
  84. case 7:
  85. {
  86. text.Text = "myzone";
  87. Drawable draw = this.resources.GetDrawable(Resource.Drawable.home_button_myzone);
  88. draw.SetBounds(0, 0, draw.IntrinsicWidth, draw.IntrinsicHeight);
  89. text.SetCompoundDrawables(null, draw, null, null);
  90. break;
  91. }
  92. case 8:
  93. {
  94. text.Text = "more";
  95. Drawable draw = this.resources.GetDrawable(Resource.Drawable.home_button_more);
  96. draw.SetBounds(0, 0, draw.IntrinsicWidth, draw.IntrinsicHeight);
  97. text.SetCompoundDrawables(null, draw, null, null);
  98. break;
  99. }
  100. }
  101. paramView.SetMinimumHeight((int)(96.0F * localDisplayMetrics.Density));
  102. paramView.SetMinimumWidth(((-12 + localDisplayMetrics.WidthPixels) / 3));
  103. return paramView;
  104. }
  105. }

代码如上所示,适配器的构造函数接受了一个参数,是适配器所属Activity,主要用于在适配器里调用Activy的信息。

然后我们通过LayoutInflater(布局填充类),将xml布局文件实例化为它对应的View对象,以供后续使用。

然后我们重写BaseAdapter类的一些属性和方法。

其中重写的Count属性需要特别注意,他代表我们列表的显示数,他是需要赋值的。这里的事例为其定义了一个常数9。

接下来我们重点看下GetView方法。

GetView这个方法干了很多事,作为C#开发者,从字面上是很难理解它是干什么的;不过我们可以联想思考,我们暂时把他理解为行的导入事件,这样就很形象了吧。

因为,至于为什么会叫GetView,我想,大概是因为他即干了行绑定数据的事,又干了行视图布局的事,所以没有更合适的命名,才这么叫的吧。

这也是为什么我感觉他奇葩的原因,因为在之前的Activity和布局中已经混淆了视图和数据,然后,在控件里,我们又一次把数据和布局搅和在了一起。。。。

下面我们看看它是如何混淆,不,他是如何工作的吧。

首先,在行导入的GetView中,我们找到要填充的布局XML——activity_label_item.xml。

  1. paramView = inflater.Inflate(Resource.Layout.activity_label_item, null);

接着,我们找这个行布局内的控件,然后为他赋值,这里activity_label_item.xml很简单,只有一个Textview,也就是说,这里我们需要做的就是给他赋值。

然后,我们通过paramInt来判断当前行,正常情况,在这里找到Activity的数据集合,找到集合的对应行赋值即可了。

Demo里我们做了一下特殊处理,我们为行视图添加了图片。

运行结果如下图:

如图所示,列表已经创建完成了。

下面我们为列表添加点击事件;代码如下:

  1. my_grid.ItemClick += (s, e) =>
  2. {
  3. this.ShowToast("Click Me" + e.Id);
  4. };

代码很简单,也很简洁,实现效果如下:

如上图所示,我们成功的实现了点击事件。

到此,控件的基础应用就讲完了,下一篇继续讲解Android软件的部署。

----------------------------------------------------------------------------------------------------

代码已经传到Github上了,欢迎大家下载。

Github地址:https://github.com/kiba518/KibaXamarin_Android

----------------------------------------------------------------------------------------------------

注:此文章为原创,欢迎转载,请在文章页面明显位置给出此文链接!
若您觉得这篇文章还不错,请点击下右下角的推荐】,非常感谢!

 

原文链接:http://www.cnblogs.com/kiba/p/10395681.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号