相信我,這不是一篇吐槽文章。。。。
基礎控件
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.GetDrawable(Resource.Drawable.home_button_search); draw.SetBounds(0, 0, draw.IntrinsicWidth, draw.IntrinsicHeight); text.SetCompoundDrawables(null, draw, null, null); break; } case 2: { text.Text = "checkin"; Drawable draw = this.resources.GetDrawable(Resource.Drawable.home_button_checkin); draw.SetBounds(0, 0, draw.IntrinsicWidth, draw.IntrinsicHeight); text.SetCompoundDrawables(null, draw, null, null); break; } case 3: { text.Text = "promo"; Drawable draw = this.resources.GetDrawable(Resource.Drawable.home_button_promo); draw.SetBounds(0, 0, draw.IntrinsicWidth, draw.IntrinsicHeight); text.SetCompoundDrawables(null, draw, null, null); break; } case 4: { text.Text = "tuan"; Drawable draw = this.resources.GetDrawable(Resource.Drawable.home_button_tuan); draw.SetBounds(0, 0, draw.IntrinsicWidth, draw.IntrinsicHeight); text.SetCompoundDrawables(null, draw, null, null); break; } case 5: { text.Text = "rank"; Drawable draw = this.resources.GetDrawable(Resource.Drawable.home_button_rank); draw.SetBounds(0, 0, draw.IntrinsicWidth, draw.IntrinsicHeight); text.SetCompoundDrawables(null, draw, null, null); break; } case 6: { text.Text = "history"; Drawable draw = this.resources.GetDrawable(Resource.Drawable.home_button_history); draw.SetBounds(0, 0, draw.IntrinsicWidth, draw.IntrinsicHeight); text.SetCompoundDrawables(null, draw, null, null); break; } case 7: { text.Text = "myzone"; Drawable draw = this.resources.GetDrawable(Resource.Drawable.home_button_myzone); draw.SetBounds(0, 0, draw.IntrinsicWidth, draw.IntrinsicHeight); text.SetCompoundDrawables(null, draw, null, null); break; } case 8: { text.Text = "more"; Drawable draw = this.resources.GetDrawable(Resource.Drawable.home_button_more); draw.SetBounds(0, 0, draw.IntrinsicWidth, draw.IntrinsicHeight); text.SetCompoundDrawables(null, draw, null, null); break; } } paramView.SetMinimumHeight((int)(96.0F * localDisplayMetrics.Density)); paramView.SetMinimumWidth(((-12 + localDisplayMetrics.WidthPixels) / 3)); return paramView; } }
代碼如上所示,適配器的構造函數接受了一個參數,是適配器所屬Activity,主要用於在適配器里調用Activy的信息。
然后我們通過LayoutInflater(布局填充類),將xml布局文件實例化為它對應的View對象,以供后續使用。
然后我們重寫BaseAdapter類的一些屬性和方法。
其中重寫的Count屬性需要特別注意,他代表我們列表的顯示數,他是需要賦值的。這里的事例為其定義了一個常數9。
接下來我們重點看下GetView方法。
GetView這個方法干了很多事,作為C#開發者,從字面上是很難理解它是干什么的;不過我們可以聯想思考,我們暫時把他理解為行的導入事件,這樣就很形象了吧。
因為,至於為什么會叫GetView,我想,大概是因為他即干了行綁定數據的事,又干了行視圖布局的事,所以沒有更合適的命名,才這么叫的吧。
這也是為什么我感覺他奇葩的原因,因為在之前的Activity和布局中已經混淆了視圖和數據,然后,在控件里,我們又一次把數據和布局攪和在了一起。。。。
下面我們看看它是如何混淆,不,他是如何工作的吧。
首先,在行導入的GetView中,我們找到要填充的布局XML——activity_label_item.xml。
paramView = inflater.Inflate(Resource.Layout.activity_label_item, null);
接着,我們找這個行布局內的控件,然后為他賦值,這里activity_label_item.xml很簡單,只有一個Textview,也就是說,這里我們需要做的就是給他賦值。
然后,我們通過paramInt來判斷當前行,正常情況,在這里找到Activity的數據集合,找到集合的對應行賦值即可了。
Demo里我們做了一下特殊處理,我們為行視圖添加了圖片。
運行結果如下圖:
如圖所示,列表已經創建完成了。
下面我們為列表添加點擊事件;代碼如下:
my_grid.ItemClick += (s, e) => { this.ShowToast("Click Me" + e.Id); };
代碼很簡單,也很簡潔,實現效果如下:
如上圖所示,我們成功的實現了點擊事件。
到此,控件的基礎應用就講完了,下一篇繼續講解Android軟件的部署。
----------------------------------------------------------------------------------------------------
相關文章:
C#-Xamarin的Android項目開發(一)——創建項目
代碼已經傳到Github上了,歡迎大家下載。
Github地址:https://github.com/kiba518/KibaXamarin_Android
----------------------------------------------------------------------------------------------------
注:此文章為原創,任何形式的轉載都請聯系作者獲得授權並注明出處!
若您覺得這篇文章還不錯,請點擊下方的【推薦】,非常感謝!