Xamarin相对于父代形成TranslateTo
我使用TranslateTo在屏幕上垂直移动对象,但我只看到如何使用数字作为x/y参数。在视图中添加对象时,如果您说“Constraint.RelativeToParent(…”,我该如何做Xamarin相对于父代形成TranslateTo,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我使用TranslateTo在屏幕上垂直移动对象,但我只看到如何使用数字作为x/y参数。在视图中添加对象时,如果您说“Constraint.RelativeToParent(…”,我该如何做 我可以相对于其他东西进行翻译吗?听起来你可能想使用相对词yout 有一个讨论和一个演示可以让你开始吗 是-您可以在与其他视图相关的视图上执行翻译操作 以下示例说明:- StackLayout objStackLayout = new StackLayout() {
我可以相对于其他东西进行翻译吗?听起来你可能想使用
相对词yout
有一个讨论和一个演示可以让你开始吗
是-您可以在与其他视图
相关的视图上执行翻译
操作
以下示例说明:-
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);
StackLayout objStackLayout=newstacklayout()
{
方向=堆叠方向。垂直,
};
RelativeLayout objRelativeLayout=新建RelativeLayout();
objStackLayout.Children.Add(objRelativeLayout);
Label objLabel1=新标签();
objLabel1.BackgroundColor=Color.Red;
objLabel1.Text=“这是一个标签”;
objLabel1.SizeChanged+=((o2,e2)=>
{
objRelativeLayout.ForceLayout();
});
objRelativeLayout.Children.Add(objLabel1,
xConstraint:Constraint.RelativeToParent((父项)=>
{
返回((parent.Width-objLabel1.Width)/2);
}));
按钮objButton=新按钮();
objButton.BackgroundColor=Color.Blue;
objButton.Text=“Hi”;
objRelativeLayout.Children.Add(objButton,
xConstraint:Constraint.RelativeToView(objLabel1,
新函数((pobjRelativeLayout,pobjView)=>
{
返回pobjView.X+pobjView.Width;
})));
按钮objButton1=新按钮();
objButton1.Text=“翻译与文本相关的按钮”;
objButton1.单击+=((o2,e2)=>
{
objButton.TranslateTo(1001002000);
});
objStackLayout.Children.Add(objButton1);
按钮objButton2=新按钮();
objButton2.Text=“更改标签文本”;
objButton2.单击+=((o2,e2)=>
{
objLabel1.Text=“Text”;
});
objStackLayout.Children.Add(objButton2);
单击带有文本“平移与文本相关的按钮”的按钮将平移100宽100高的蓝色按钮
当您单击带有文本“更改标签文本”的按钮时,该规则仍然有效。请注意,之前应用的转换仍然在距离我们进行相对布局的标签末端100宽100高的偏移处。我使用的是相对视图,我没有提到这一点。我最终使用的解决方案是获取长度(本例中的高度)我想相对移动的视图,所以它看起来像是双范围=image\u graph.Height(image\u graph是相对移动的视图)。然后,我得到了对象应该在图形上方多远的百分比,进行了一些分割,然后进行了平移。