设为首页 加入收藏

TOP

C#-Xamarin的Android项目开发(二)——控件应用(一)
2019-09-01 23:13:11 】 浏览:60
Tags:C#-Xamarin Android 项目开发 控件 应用

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

基础控件

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

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

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

<ImageButton
    android:src="@drawable/toolbar_upload_photo_normal"
    android:layout_gravity="right|center_vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/btn_weight" />

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

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

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

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

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

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

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

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

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

GridView

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

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

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

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

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

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

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

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

public class GridAdapter : BaseAdapter
    {
        private DisplayMetrics localDisplayMetrics;
        private LayoutInflater inflater;
        private Resources resources;
        public GridAdapter(Context context)
        { 
            resources = context.Resources;
            localDisplayMetrics = resources.DisplayMetrics;
            inflater = LayoutInflater.From(context);
        }
        public override int Count => 9; 
        public override Object GetItem(int position)
        {
            return null;
        } 
        public override long GetItemId(int position)
        {
            return position;
        }
        public override View GetView(int paramInt, View paramView, ViewGroup paramViewGroup)
        {
            paramView = inflater.Inflate(Resource.Layout.activity_label_item, null);
            TextView text = (TextView)paramView.FindViewById(Resource.Id.activity_name);
            switch (paramInt)
            {
                case 0:
                    {
                        text.Text = "local";
                        Drawable draw = this.resources.GetDrawable(Resource.Drawable.home_button_local);
                        draw.SetBounds(0, 0, draw.IntrinsicWidth, draw.IntrinsicHeight);
                        text.SetCompoundDrawables(null, draw, null, null);
                        break;
                    }
                case 1:
                    {
                        text.Text = "search";
                        Drawable draw = this.resources.Ge
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇阿里云对象存储OSS访问控制 下一篇google zxing android扫描优化&解..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目