VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > c#教程 >
  • C#教程之C#教程之Xamarin.Android RelativeLayout

本站最新发布   C#从入门到精通
试听地址  
https://www.xin3721.com/eschool/CSharpxin3721/

初次接触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

相关教程