繁体   English   中英

如何在运行时在xamarin.forms中的相对布局中更改视图的位置或大小

[英]How to change the position or size of a view in Relative Layout at runtime in xamarin.forms

我所做的是通过设置所有约束将标签添加到相对布局中。

下面是我的代码。

relativeLayout.Children.Add(textLabel, Constraint.RelativeToView(innerBorderBox, (parent, sibling) =>
    {
        return sibling.Width * 0.55;
    }), Constraint.RelativeToView(innerBorderBox, (parent, sibling) =>
    {
        return sibling.Y;
    }), Constraint.RelativeToView(innerBorderBox, (parent, sibling) =>
    {
        return sibling.Width * .45;
    }), Constraint.RelativeToView(innerBorderBox, (parent, sibling) =>
    {
        return sibling.Height;
    }));

而且效果很好。

现在,我想动态更改该label(textLabel)X约束和宽度约束。 例如,从上面的代码中X约束为sibling.Width * 0.55 ,宽度为sibling.Width * .45 ,然后需要将X更改为sibling.Width * 0.55 + 10且width为sibling.Width * .45 - 50 怎么做?

我的猜测是,可以通过删除相对布局的标签并将其再次添加到具有新约束的相对布局中来完成。 但我认为对此会有更好的解决方案。

正如@ LeoZhu-MSFT的评论一样,它非常适合我。 这是我解决问题的方法

我的问题

现在,我想动态更改该label(textLabel)X约束和宽度约束。 例如,从上面的代码中X约束为sibling.Width * 0.55,而width为sibling.Width * .45,则需要更改为X成为sibling.Width * 0.55 + 10且width为sibling.Width * .45-50 。 怎么做?

更改X约束

 RelativeLayout.SetXConstraint(textLabel, Constraint.RelativeToView(innerBorderBox, (parent, sibling) =>
 {
    return sibling.Width * 0.55 + 10;
 }));

更改宽度约束

 RelativeLayout.SetWidthConstraint(textLabel, Constraint.RelativeToView(innerBorderBox, (parent, sibling) =>
 {
    return sibling.Width * .45 - 50;
 }));

有关更多详细信息

RelativeLayout.SetWidthConstraint => https://docs.microsoft.com/zh-cn/dotnet/api/xamarin.forms.relativelayout.setwidthconstraint?view=xamarin-forms RelativeLayout.SetXConstraint => https://docs.microsoft.com /en-us/dotnet/api/xamarin.forms.relativelayout.setxconstraint?view=xamarin-forms

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM