【发布时间】:2021-09-14 10:53:48
【问题描述】:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace App1
{
public partial class MainPage : ContentPage
{
public MainPage()
{
RelativeLayout relativeLayout = new RelativeLayout();
Label upperLeft = new Label
{
Text = "Upper Left",
FontSize = 20
};
relativeLayout.Children.Add( upperLeft ,
Constraint.Constant(0), Constraint.Constant(0)
);
Label constantLabel = new Label
{
Text = "Constants are Absolute",
FontSize = 20
};
relativeLayout.Children.Add(constantLabel,
Constraint.Constant(100),
Constraint.Constant(100),
Constraint.Constant(50),
Constraint.Constant(200)
);
Label halfWayDown = new Label
{
Text = "Halfway down and across",
FontSize = 20
};
relativeLayout.Children.Add(halfWayDown,
Constraint.RelativeToParent((parent) =>
{
return parent.Width / 2;
}),
Constraint.RelativeToParent((parent) =>
{
return parent.Height / 2;
})
) ;
this.Content = relativeLayout;
}
}
}
我有 3 个标签,一个放在左上角,第二个放在随机的 location(100,100) 上,第三个放在父宽度和高度的一半处。我对最后一个标签有疑问。我不知道为什么最后一个标签的文本会穿过屏幕,为什么它没有换行。此外,当我传递最后一个标签的width 和height 的值时,标签的文本会更改行,为什么会发生这种情况?当我只将两个参数传递给第三个标签时,就会发生这种行为(文本穿过屏幕)。我还尝试了第一个标签 where ,当我只将两个参数传递给 Add-method 时,文本会自动更改其行。谁能告诉我RelativeToParent 的方法是如何工作的?如果我没有通过它们,默认的 width 和 height 值是多少?为什么最后一个标签中的文本不改变行?
Link for the image of the output i am getting
【问题讨论】:
-
我无法回答您的全部问题,但
RelativeToParent有文档here,您可以阅读更多信息。
标签: c# xamarin label constraints android-relativelayout