简体   繁体   English

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

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

What I did is I have added a label to a relative layout by setting all constraint. 我所做的是通过设置所有约束将标签添加到相对布局中。

Below is my code for that. 下面是我的代码。

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;
    }));

and it working perfectly. 而且效果很好。

Now I want to change that label(textLabel) X Constraint and Width Constraint dynamically. 现在,我想动态更改该label(textLabel)X约束和宽度约束。 For example, from the above code X Constraint is sibling.Width * 0.55 and width is sibling.Width * .45 , then need to change to X as sibling.Width * 0.55 + 10 and width is sibling.Width * .45 - 50 . 例如,从上面的代码中X约束为sibling.Width * 0.55 ,宽度为sibling.Width * .45 ,然后需要将X更改为sibling.Width * 0.55 + 10且width为sibling.Width * .45 - 50 How to do that? 怎么做?

My guess is that it can be done by removing the label for the relative Layout and added again to relative layout with new constraint. 我的猜测是,可以通过删除相对布局的标签并将其再次添加到具有新约束的相对布局中来完成。 But I think there will be a better solution for this. 但我认为对此会有更好的解决方案。

As @LeoZhu-MSFT comment, it works perfectly for me. 正如@ LeoZhu-MSFT的评论一样,它非常适合我。 Here is how I fixed the problem 这是我解决问题的方法

For my question, 我的问题

Now I want to change that label(textLabel) X Constraint and Width Constraint dynamically. 现在,我想动态更改该label(textLabel)X约束和宽度约束。 For example, from the above code X Constraint is sibling.Width * 0.55 and width is sibling.Width * .45, then need to change to X as sibling.Width * 0.55 + 10 and width is sibling.Width * .45 - 50. How to do that? 例如,从上面的代码中X约束为sibling.Width * 0.55,而width为sibling.Width * .45,则需要更改为X成为sibling.Width * 0.55 + 10且width为sibling.Width * .45-50 。 怎么做?

To Change X Constraint 更改X约束

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

To Change Width Constraint 更改宽度约束

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

For more details on 有关更多详细信息

RelativeLayout.SetWidthConstraint => https://docs.microsoft.com/en-us/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 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