契闊 發表於 2019-8-29 19:46:00

C#-Xamarin的Android项目开发(二)——控件应用

<p>基础控件</p>
<p>Android的控件和控件样式非常特别,它是一种内联特别高的设计模式,换句话说,它是非常烂的设计。。。。</p>
<p>但在这种特别的关系里还是有一定的规律的,下面我们一起来看看控件的使用方式。&nbsp;</p>
<p>首先我们定义一个ImageButton,如下:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;">&lt;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" /&gt;
</pre>
</div>
<p>重点,我们来看这句,background="@drawable/btn_weight;背景色指向了一个资源,为什么用说指向的是个资源呢?因为btn_weight并不是个图片,而是个XML文件。。。。如下图:如上代码所示,我们定义了ImageButton,并且设置了他的Src地址,该地址指向了一个图片。</p>
<p><img id="img15670790974532" src="https://img2018.cnblogs.com/other/1762180/201908/1762180-20190829194556381-569698428.png" alt=""></p>
<p>那么我们看看btn_weight到底是什么把。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">&lt;?</span><span style="color: rgba(255, 0, 255, 1)">xml version="1.0" encoding="UTF-8"</span><span style="color: rgba(0, 0, 255, 1)">?&gt;<br></span><span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">selector</span><span style="color: rgba(255, 0, 0, 1)">xmlns:android</span><span style="color: rgba(0, 0, 255, 1)">="http://schemas.android.com/apk/res/android"</span><span style="color: rgba(0, 0, 255, 1)">&gt;<br></span>    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">item </span><span style="color: rgba(255, 0, 0, 1)">android:state_window_focused</span><span style="color: rgba(0, 0, 255, 1)">="false"</span><span style="color: rgba(255, 0, 0, 1)"> android:state_enabled</span><span style="color: rgba(0, 0, 255, 1)">="true"</span><span style="color: rgba(255, 0, 0, 1)"> android:drawable</span><span style="color: rgba(0, 0, 255, 1)">="@drawable/btn_weight_normal"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;<br></span>    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">item </span><span style="color: rgba(255, 0, 0, 1)">android:state_enabled</span><span style="color: rgba(0, 0, 255, 1)">="false"</span><span style="color: rgba(255, 0, 0, 1)"> android:drawable</span><span style="color: rgba(0, 0, 255, 1)">="@drawable/btn_weight_disable"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;<br></span>    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">item </span><span style="color: rgba(255, 0, 0, 1)">android:state_pressed</span><span style="color: rgba(0, 0, 255, 1)">="true"</span><span style="color: rgba(255, 0, 0, 1)"> android:drawable</span><span style="color: rgba(0, 0, 255, 1)">="@drawable/btn_weight_press"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span> <br>    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">item </span><span style="color: rgba(255, 0, 0, 1)">android:state_focused</span><span style="color: rgba(0, 0, 255, 1)">="true"</span><span style="color: rgba(255, 0, 0, 1)"> android:drawable</span><span style="color: rgba(0, 0, 255, 1)">="@drawable/btn_weight_press"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;<br></span>    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">item </span><span style="color: rgba(255, 0, 0, 1)">android:drawable</span><span style="color: rgba(0, 0, 255, 1)">="@drawable/btn_weight_normal"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;<br>&lt;/</span><span style="color: rgba(128, 0, 0, 1)">selector</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<p>&nbsp;</p>
<p>
没错,这种设置方法,确实很绕,按钮按下的事件和背景样式混在了一起设置,但在Android里,我们只能去适应它。如上述代码所示,btn_weight里设置了按钮按下时和常规时的背景色。</p>
<p>----------------------------------------------------------------------------------------------------</p>
<p>好了,现在基础控件写完了,有没有感觉自己从现代化城市回到了农耕社会。。。。</p>
<p>相信我,用Xamarin开发,你在农耕社会还有个犁耙,用AS开发,你会发现你只能用手挖。。。。</p>
<p>GridView</p>
<p>首先,Android的GridView是我见过最奇葩的列表使用方式。。。</p>
<p>然后,我们开始学习使用它把。</p>
<p>先找到GridView控件,代码如下:</p>
<div class="cnblogs_code">
<pre>GridView my_grid = <span style="color: rgba(0, 0, 255, 1)">this</span>.FindControl&lt;GridView&gt;(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">my_grid</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);

IListAdapter adapter </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> GridAdapter(<span style="color: rgba(0, 0, 255, 1)">this</span>, <span style="color: rgba(0, 0, 255, 1)">this</span>.Resources);my_grid.Adapter = adapter;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">配置适配器</span></pre>
</div>
<p>接着,我们定义一个适配器,并把他赋值给GridView的的Adapter属性,代码如下:</p>
<p>嗯,这里看上去代码还算简洁,但接下来就不一样了,让我们来看看这个奇葩的适配器吧。</p>
<p>首先,我们看下适配器代码:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> GridAdapter : BaseAdapter
    {
      </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> DisplayMetrics localDisplayMetrics;
      </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> LayoutInflater inflater;
      </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Resources resources;
      </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> GridAdapter(Context context)
      {
            resources </span>=<span style="color: rgba(0, 0, 0, 1)"> context.Resources;
            localDisplayMetrics </span>=<span style="color: rgba(0, 0, 0, 1)"> resources.DisplayMetrics;
            inflater </span>=<span style="color: rgba(0, 0, 0, 1)"> LayoutInflater.From(context);
      }
      </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">override</span> <span style="color: rgba(0, 0, 255, 1)">int</span> Count =&gt; <span style="color: rgba(128, 0, 128, 1)">9</span><span style="color: rgba(0, 0, 0, 1)">;
      </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">override</span> Object GetItem(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> position)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
      }
      </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">override</span> <span style="color: rgba(0, 0, 255, 1)">long</span> GetItemId(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> position)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> position;
      }
      </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">override</span> View GetView(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> paramInt, View paramView, ViewGroup paramViewGroup)
      {
            paramView </span>= inflater.Inflate(Resource.Layout.activity_label_item, <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
            TextView text </span>=<span style="color: rgba(0, 0, 0, 1)"> (TextView)paramView.FindViewById(Resource.Id.activity_name);
            </span><span style="color: rgba(0, 0, 255, 1)">switch</span><span style="color: rgba(0, 0, 0, 1)"> (paramInt)
            {
                </span><span style="color: rgba(0, 0, 255, 1)">case</span> <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">:
                  {
                        text.Text </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">local</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
                        Drawable draw </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.resources.GetDrawable(Resource.Drawable.home_button_local);
                        draw.SetBounds(</span><span style="color: rgba(128, 0, 128, 1)">0</span>, <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">, draw.IntrinsicWidth, draw.IntrinsicHeight);
                        text.SetCompoundDrawables(</span><span style="color: rgba(0, 0, 255, 1)">null</span>, draw, <span style="color: rgba(0, 0, 255, 1)">null</span>, <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
                        </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
                  }
                </span><span style="color: rgba(0, 0, 255, 1)">case</span> <span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">:
                  {
                        text.Text </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">search</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
                        Drawable draw </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.resources.GetDrawable(Resource.Drawable.home_button_search);
                        draw.SetBounds(</span><span style="color: rgba(128, 0, 128, 1)">0</span>, <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">, draw.IntrinsicWidth, draw.IntrinsicHeight);
                        text.SetCompoundDrawables(</span><span style="color: rgba(0, 0, 255, 1)">null</span>, draw, <span style="color: rgba(0, 0, 255, 1)">null</span>, <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
                        </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
                  }
                </span><span style="color: rgba(0, 0, 255, 1)">case</span> <span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">:
                  {
                        text.Text </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">checkin</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
                        Drawable draw </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.resources.GetDrawable(Resource.Drawable.home_button_checkin);
                        draw.SetBounds(</span><span style="color: rgba(128, 0, 128, 1)">0</span>, <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">, draw.IntrinsicWidth, draw.IntrinsicHeight);
                        text.SetCompoundDrawables(</span><span style="color: rgba(0, 0, 255, 1)">null</span>, draw, <span style="color: rgba(0, 0, 255, 1)">null</span>, <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
                        </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
                  }
                </span><span style="color: rgba(0, 0, 255, 1)">case</span> <span style="color: rgba(128, 0, 128, 1)">3</span><span style="color: rgba(0, 0, 0, 1)">:
                  {
                        text.Text </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">promo</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
                        Drawable draw </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.resources.GetDrawable(Resource.Drawable.home_button_promo);
                        draw.SetBounds(</span><span style="color: rgba(128, 0, 128, 1)">0</span>, <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">, draw.IntrinsicWidth, draw.IntrinsicHeight);
                        text.SetCompoundDrawables(</span><span style="color: rgba(0, 0, 255, 1)">null</span>, draw, <span style="color: rgba(0, 0, 255, 1)">null</span>, <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
                        </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
                  }
                </span><span style="color: rgba(0, 0, 255, 1)">case</span> <span style="color: rgba(128, 0, 128, 1)">4</span><span style="color: rgba(0, 0, 0, 1)">:
                  {
                        text.Text </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">tuan</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
                        Drawable draw </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.resources.GetDrawable(Resource.Drawable.home_button_tuan);
                        draw.SetBounds(</span><span style="color: rgba(128, 0, 128, 1)">0</span>, <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">, draw.IntrinsicWidth, draw.IntrinsicHeight);
                        text.SetCompoundDrawables(</span><span style="color: rgba(0, 0, 255, 1)">null</span>, draw, <span style="color: rgba(0, 0, 255, 1)">null</span>, <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
                        </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
                  }

                </span><span style="color: rgba(0, 0, 255, 1)">case</span> <span style="color: rgba(128, 0, 128, 1)">5</span><span style="color: rgba(0, 0, 0, 1)">:
                  {
                        text.Text </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">rank</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
                        Drawable draw </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.resources.GetDrawable(Resource.Drawable.home_button_rank);
                        draw.SetBounds(</span><span style="color: rgba(128, 0, 128, 1)">0</span>, <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">, draw.IntrinsicWidth, draw.IntrinsicHeight);
                        text.SetCompoundDrawables(</span><span style="color: rgba(0, 0, 255, 1)">null</span>, draw, <span style="color: rgba(0, 0, 255, 1)">null</span>, <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
                        </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
                  }
                </span><span style="color: rgba(0, 0, 255, 1)">case</span> <span style="color: rgba(128, 0, 128, 1)">6</span><span style="color: rgba(0, 0, 0, 1)">:
                  {
                        text.Text </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">history</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
                        Drawable draw </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.resources.GetDrawable(Resource.Drawable.home_button_history);
                        draw.SetBounds(</span><span style="color: rgba(128, 0, 128, 1)">0</span>, <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">, draw.IntrinsicWidth, draw.IntrinsicHeight);
                        text.SetCompoundDrawables(</span><span style="color: rgba(0, 0, 255, 1)">null</span>, draw, <span style="color: rgba(0, 0, 255, 1)">null</span>, <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
                        </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
                  }
                </span><span style="color: rgba(0, 0, 255, 1)">case</span> <span style="color: rgba(128, 0, 128, 1)">7</span><span style="color: rgba(0, 0, 0, 1)">:
                  {
                        text.Text </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">myzone</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
                        Drawable draw </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.resources.GetDrawable(Resource.Drawable.home_button_myzone);
                        draw.SetBounds(</span><span style="color: rgba(128, 0, 128, 1)">0</span>, <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">, draw.IntrinsicWidth, draw.IntrinsicHeight);
                        text.SetCompoundDrawables(</span><span style="color: rgba(0, 0, 255, 1)">null</span>, draw, <span style="color: rgba(0, 0, 255, 1)">null</span>, <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
                        </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
                  }
                </span><span style="color: rgba(0, 0, 255, 1)">case</span> <span style="color: rgba(128, 0, 128, 1)">8</span><span style="color: rgba(0, 0, 0, 1)">:
                  {
                        text.Text </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">more</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
                        Drawable draw </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.resources.GetDrawable(Resource.Drawable.home_button_more);
                        draw.SetBounds(</span><span style="color: rgba(128, 0, 128, 1)">0</span>, <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">, draw.IntrinsicWidth, draw.IntrinsicHeight);
                        text.SetCompoundDrawables(</span><span style="color: rgba(0, 0, 255, 1)">null</span>, draw, <span style="color: rgba(0, 0, 255, 1)">null</span>, <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
                        </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
                  }
            }
            paramView.SetMinimumHeight((</span><span style="color: rgba(0, 0, 255, 1)">int</span>)(<span style="color: rgba(128, 0, 128, 1)">96.0F</span> *<span style="color: rgba(0, 0, 0, 1)"> localDisplayMetrics.Density));
            paramView.SetMinimumWidth(((</span>-<span style="color: rgba(128, 0, 128, 1)">12</span> + localDisplayMetrics.WidthPixels) / <span style="color: rgba(128, 0, 128, 1)">3</span><span style="color: rgba(0, 0, 0, 1)">));
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> paramView;
      }
    }</span></pre>
</div>
<p>然后我们通过LayoutInflater(布局填充类),将xml布局文件实例化为它对应的View对象,以供后续使用。代码如上所示,适配器的构造函数接受了一个参数,是适配器所属Activity,主要用于在适配器里调用Activy的信息。</p>
<p>然后我们重写BaseAdapter类的一些属性和方法。</p>
<p>其中重写的Count属性需要特别注意,他代表我们列表的显示数,他是需要赋值的。这里的事例为其定义了一个常数9。</p>
<p>接下来我们重点看下GetView方法。</p>
<p>GetView这个方法干了很多事,作为C#开发者,从字面上是很难理解它是干什么的;不过我们可以联想思考,我们暂时把他理解为行的导入事件,这样就很形象了吧。</p>
<p>因为,至于为什么会叫GetView,我想,大概是因为他即干了行绑定数据的事,又干了行视图布局的事,所以没有更合适的命名,才这么叫的吧。</p>
<p>这也是为什么我感觉他奇葩的原因,因为在之前的Activity和布局中已经混淆了视图和数据,然后,在控件里,我们又一次把数据和布局搅和在了一起。。。。</p>
<p>下面我们看看它是如何混淆,不,他是如何工作的吧。</p>
<p>首先,在行导入的GetView中,我们找到要填充的布局XML——activity_label_item.xml。</p>
<pre><code>paramView = inflater.Inflate(Resource.Layout.activity_label_item, null);
</code></pre>
<p><br>
然后,我们通过paramInt来判断当前行,正常情况,在这里找到Activity的数据集合,找到集合的对应行赋值即可了。接着,我们找这个行布局内的控件,然后为他赋值,这里activity_label_item.xml很简单,只有一个Textview,也就是说,这里我们需要做的就是给他赋值。</p>
<p>Demo里我们做了一下特殊处理,我们为行视图添加了图片。</p>
<p>运行结果如下图:</p>
<p><img id="img156707909746112" src="https://img2018.cnblogs.com/other/1762180/201908/1762180-20190829194556705-287427656.png" alt=""></p>
<p>如图所示,列表已经创建完成了。</p>
<p>下面我们为列表添加点击事件;代码如下:</p>
<div class="cnblogs_code">
<pre>my_grid.ItemClick += (s, e) =&gt;<span style="color: rgba(0, 0, 0, 1)">
{
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.ShowToast(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Click Me</span><span style="color: rgba(128, 0, 0, 1)">"</span> +<span style="color: rgba(0, 0, 0, 1)"> e.Id);
};</span></pre>
</div>
<p>代码很简单,也很简洁,实现效果如下:</p>
<p>如上图所示,我们成功的实现了点击事件。</p>
<p>到此,控件的基础应用就讲完了,下一篇继续讲解Android软件的部署。</p>
<p>----------------------------------------------------------------------------------------------------</p>
<p><strong>相关文章:</strong></p>
<p>C#-Xamarin的Android项目开发(一)——创建项目</p>
<p>代码已经传到Github上了,欢迎大家下载。</p>
<p>Github地址:https://github.com/kiba518/KibaXamarin_Android</p>
<p>----------------------------------------------------------------------------------------------------</p>
<p>原文连接:https://www.cnblogs.com/kiba/<br>
&nbsp;</p>
<p>欢迎添加个人微信号:Like若所思。</p>
<p>欢迎关注我的公众号,不仅为你推荐最新的博文,还有更多惊喜和资源在等着你!一起学习共同进步!</p>
<p><img id="img156628651393916" src="https://img2018.cnblogs.com/other/1762180/201908/1762180-20190829194556749-1530806854.jpg" alt=""></p>
<p><br>
&nbsp;</p>
<p>&nbsp;</p>

</div>
<div id="MySignature" role="contentinfo">
   
<div class="div_masklayer" id="div_masklayer"></div>
<div class="div_popup" id="Div_popup"> <img class="img_zfb" id="img_zfb" src="https://images.cnblogs.com/cnblogs_com/cool2feel/1535460/o_IMG_20190827_161853.jpg">
<p class="mid">您的资助是我最大的动力!<br>金额随意,欢迎来赏!</p>

</div><br><br>
来源:https://www.cnblogs.com/cool2feel/p/11431647.html
頁: [1]
查看完整版本: C#-Xamarin的Android项目开发(二)——控件应用