標籤:修改 rate 空間 block nan str 國內 apc 基礎
初次接觸Xamarin.Android.
由於國內Xamarin的資料少見,我大多參考JAVA原生代碼,慢慢摸索過來。
我把摸索出來的結果廣而告之,希望後來人能少走一點彎路,也希望你也能做出一份貢獻。
如果你學會了RelativeLayout,那LinearLayout自然手到擒來。
動態添加學會了,靜態添加還遠嗎?
1. 建立RelativeLayout
RelativeLayout Test = new RelativeLayout(this.Context);
2. 添加控制項
2.1 基礎添加
TextView TestText = new TextView(this.Context);Test.AddView(TestText);
2.2 頂部添加 (底部添加等等類似)
RelativeLayout Test = new RelativeLayout(this.Context);TextView TestText = new TextView(this.Context);
TestText.Text="我是1號";RelativeLayout.LayoutParams TestTextRP = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WrapContent, RelativeLayout.LayoutParams.WrapContent);TestTextRP.AddRule(LayoutRules.AlignParentTop);Test.AddView(TestText,TestTextRP);
Android使用LayoutParams控制控制項的空間布局。
而C#常用的布局是通過對控制項的空間屬性進行修改,這種設計思路在Android不適用了。
我不得不告訴你,如果你需要對控制項布局參數進行修改,請務必使用LayoutParams。入鄉隨俗。
2.3 添加在另一個控制項的後面
RelativeLayout Test = new RelativeLayout(this.Context);TextView TestText = new TextView(this.Context);TestText.Text = "我是1號";RelativeLayout.LayoutParams TestTextRP = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WrapContent, RelativeLayout.LayoutParams.WrapContent);TestTextRP.AddRule(LayoutRules.AlignParentTop);Test.AddView(TestText,TestTextRP);TextView NextText = new TextView(this.Context);NextText.Text = "我是2號";RelativeLayout.LayoutParams NextTextRP = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WrapContent, RelativeLayout.LayoutParams.WrapContent);//注意這裡TestText.Id = View.GenerateViewId();NextTextRP.AddRule(LayoutRules.Below,TestText.Id);Test.AddView(NextText, NextTextRP);
測試圖片:
這裡面的關鍵點是給上一個控制項的ID進行賦值。(我使用的是系統產生的值。你可以嘗試其它值,如1,2,3..等等。)
如果你不賦值,那麼實際效果是兩個TextView將會重疊。
經過測試,每個控制項的預設ID是-1.
2.4 待續
出自: https://www.cnblogs.com/nanyunan/p/9189057.html
Xamarin.Android RelativeLayout