【发布时间】:2016-02-11 00:38:11
【问题描述】:
我正在使用 TranslateTo 在屏幕上垂直移动对象,但我只看到如何将数字用于 x/y 参数。在您说“Constraint.RelativeToParent(...”) 的视图上添加对象时,我该怎么做?
我可以相对于其他东西翻译吗?
【问题讨论】:
标签: xamarin xamarin.forms
我正在使用 TranslateTo 在屏幕上垂直移动对象,但我只看到如何将数字用于 x/y 参数。在您说“Constraint.RelativeToParent(...”) 的视图上添加对象时,我该怎么做?
我可以相对于其他东西翻译吗?
【问题讨论】:
标签: xamarin xamarin.forms
听起来您可能想使用RelativeLayout?
是的 - 您可以对与其他 View 相关的 View 执行 Translate 操作。
下面的例子证明了:-
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 高度。
【讨论】: