我正在使用TranslateTo在屏幕上垂直移动对象,但我只看到如何使用数字作为x / y参数。如何在视图中添加对象时执行某些操作,例如“Constraint.RelativeToParent(...”?
我可以翻译相对于其他东西吗?
答案 0 :(得分:0)
听起来好像想要使用RelativeLayout
?
是的 - 您可以对Translate
执行与View
相关的View
操作。
以下示例演示了: -
StackLayout objStackLayout = new StackLayout()
{
Orientation = StackOrientation.Vertical,
};
RelativeLayout objRelativeLayout = new RelativeLayout();
objStackLayout.Children.Add(objRelativeLayout);
Label objLabel1 = new Label();
objLabel1.BackgroundColor = Color.Red;
objLabel1.Text = "This is a label";
objLabel1.SizeChanged += ((o2, e2) =>
{
objRelativeLayout.ForceLayout();
});
objRelativeLayout.Children.Add(objLabel1,
xConstraint: Constraint.RelativeToParent((parent) =>
{
return ((parent.Width - objLabel1.Width) / 2);
}));
Button objButton = new Button();
objButton.BackgroundColor = Color.Blue;
objButton.Text = "Hi";
objRelativeLayout.Children.Add(objButton,
xConstraint: Constraint.RelativeToView(objLabel1,
new Func<RelativeLayout, View, double>((pobjRelativeLayout, pobjView) =>
{
return pobjView.X + pobjView.Width;
})));
Button objButton1 = new Button();
objButton1.Text = "Translate the button that is relative to the text";
objButton1.Clicked += ((o2, e2) =>
{
objButton.TranslateTo(100,100,2000);
});
objStackLayout.Children.Add(objButton1);
Button objButton2 = new Button();
objButton2.Text = "Change label text";
objButton2.Clicked += ((o2, e2) =>
{
objLabel1.Text = "text";
});
objStackLayout.Children.Add(objButton2);
单击带有文本“翻译相对于文本的按钮”的按钮,将蓝色按钮平移100宽度和100高度。
当您单击带有“更改标签文本”文本的按钮时,仍会强制执行该规则。请注意,之前应用的平移仍然是距我们正在进行相对布局的Label
末尾的100宽度和100高度的偏移。