相信我,這不是一篇吐槽文章。。。。html
基礎控件android
Android的控件和控件樣式很是特別,它是一種內聯特別高的設計模式,換句話說,它是很是爛的設計。。。。git
但在這種特別的關係裏仍是有必定的規律的,下面咱們一塊兒來看看控件的使用方式。 github
首先咱們定義一個ImageButton,以下:sql
<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文件。。。。以下圖:ide
那麼咱們看看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
----------------------------------------------------------------------------------------------------
注:此文章爲原創,任何形式的轉載都請聯繫做者得到受權並註明出處!
若您以爲這篇文章還不錯,請點擊下方的【推薦】,很是感謝!