【问题标题】:Xamarin Forms TranslateTo relative to parentXamarin Forms TranslateTo 相对于父级
【发布时间】:2016-02-11 00:38:11
【问题描述】:

我正在使用 TranslateTo 在屏幕上垂直移动对象,但我只看到如何将数字用于 x/y 参数。在您说“Constraint.RelativeToParent(...”) 的视图上添加对象时,我该怎么做?

我可以相对于其他东西翻译吗?

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    听起来您可能想使用RelativeLayout

    有一个讨论here 和一个演示here 可以帮助您入门吗?

    是的 - 您可以对与其他 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 高度。

    【讨论】:

    • 我使用的是相对视图,我没有提到这一点。我最终使用的解决方案是获取我想要相对移动的视图的长度(在这种情况下为高度),所以它看起来像 double range = image_graph.Height (image_graph 是相对移动到的视图)。然后,我得到了对象应该在图上多远的百分比,进行了一些划分,然后进行了 TranslateTo。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多