Xamarin.Forms 布局讲解(二)

以下主要内容转自:http://www.jianshu.com/p/2ff32a196366

 

RelativeLayout介绍

RelativeLayout是一种相对布局,相对的位置可以是父布局(layout)或其它视图(view)。虽然名称和Android中的RelativeLayout布局相同,但是使用却有很大的差距,Forms RelativeLayout布局借助Constraints(约束)来确定子视图的位置和大小。

布局文件 添加如下代码:

    <RelativeLayout x:Name="layout">

    </RelativeLayout>

xaml文件中定义涉及到ConstraintExpression的使用,为了更好的理解Constraint在RelativeLayout中的作用,先用代码的方式定义布局。

在页面的构造函数中InitializeComponent方法后添加如下代码:

            var label1 = new Label()
            {
                Text = "Text 1",
                BackgroundColor = Color.Red
            };
            layout.Children.Add(label1,
            Constraint.RelativeToParent((layoutView) =>
            {
                return 100;
            }), Constraint.RelativeToParent((layoutView) =>
            {
                return 100;
            }), Constraint.RelativeToParent((layoutView) =>
            {
                return 200;
            }), Constraint.RelativeToParent((layoutView) =>
            {
                return 200;

            }));

通常情况用户自己编写的代码要放在InitializeComponent方法后,InitializeComponent表示xaml定义的页面初始化完成。

layout指的是我们在xaml文件中指定了x:Name的RelativeLayout布局。RelativeLayout的Children属性是RelativeLayout.IRelativeList<View>类型,提供了三种重载的Add方法. 

本文中调用第三个重载方法定义向RelativeLayout中添加子视图。

后四个参数分别代表子视图的x,y坐标和宽高的约束。相对于布局的约束还是其它子视图的约束可以通过Constraint的两个静态方法区分,Constraint.RelativeToParent创建相对于布局视图的约束,Constraint.RelativeToView创建相对于其它子视图的约束。参数中Func委托反悔的double是当前Constraint的值,即创建xConstraint时返回值对应视图的X坐标。运行效果:

 

两个方法的实现就是根据我们传入的Func委托(RelativeToView方法多一个View视图)定义一个Constraint实例返回:

 

继续添加如下代码,示范RelativeToView方法的使用:

            layout.Children.Add(new Label()
            {
                Text = "Text 2",
                BackgroundColor = Color.Blue
            }, Constraint.RelativeToParent((layoutView) =>
            {
                return 0;
            }), Constraint.RelativeToView(label1, (layoutView, view) =>
            {
                return view.Height + view.X + 100;
            }), Constraint.RelativeToParent((layoutView) =>
            {
                return 400;
            }), Constraint.RelativeToParent((layoutView) =>
            {
                return 100;

            }));

RelativeToView的第一个参数表示根据哪个视图创建约束,与第二个Func中的第二个参数应该表示同一个View。

运行项目查看效果:

 

接下来示范如何直接在Xaml文件中定义RelativeLayout布局。

定义在RelativeLayout中的视图会有对应x,y,width,height约束的扩展属性如RelativeLayout.XConstraint,在xaml中通过ConstraintExpression设置对应的约束。

xaml文件添加如下代码:

  <RelativeLayout>
      <Label x:Name="label1" Text="Label 1"
          BackgroundColor="Blue" WidthRequest="200" HeightRequest="200"
          RelativeLayout.XConstraint = "{ConstraintExpression Type=RelativeToParent,Property=X,Factor=1,Constant=100}"
          RelativeLayout.YConstraint = "{ConstraintExpression Type=RelativeToParent,Property=Y,Factor=1,Constant=100}"/>
      <Label Text="Label 2" BackgroundColor="Red"
          RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent,Property=X,Factor=1,Constant=0}"
          RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToView,ElementName=label1,Property=Y,Factor=2,Constant=200}"
          RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToView,ElementName=label1,Property=Width,Factor=2,Constant=0}"
          RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToView,ElementName=label1,Property=Height,Factor=0.5,Constant=0}"/>

  </RelativeLayout>

通过RelativeLayout.XConstraint和RelativeLayout.YConstraint确定视图的位置(Position),视图的大小可以通过设置WidthRequest和HeightRequest或者设置RelativeLayout.WidthConstraint和RelativeLayout.HeightConstraint两种方式确定。

根据ConstraintExpression定义约束时每个字段含义介绍:

Type – 以什么方式定义约束,RelativeToParent和RelativeToView

Property – 以相对视图的哪个属性作为基础值.

Factor – Property的倍数值.

Constant – 约束Value的偏移数值.

ElementName – 当Type定义为RelativeToView时,ElementName用来确定约束相对与哪个子视图.

以Label 2 HeightConstraint的为例:

RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToView,ElementName=label1,Property=Height,Factor=0.5,Constant=0}"

表示:Label 2 的高度 = Label 1 的高度 (Property=Height)* 0.5(Factor=0.5) + 0(Constant=0)

效果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值