Xamarin.Android RelativeLayout

來源:互聯網
上載者:User

標籤:修改   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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.