tes*_*ing 9 c# layout xamarin xamarin.forms
是否可以RelativeLayout在设置一次后更改a的约束?
在文档中我看到了类似的方法GetBoundsConstraint(BindableObject),但我认为只有拥有XAML文件才能使用它们.现在我正在尝试在代码中执行此操作.null如果我尝试,我会得到
RelativeLayout.GetBoundsConstraint(this.label);
Run Code Online (Sandbox Code Playgroud)
我发现的唯一方法是从布局中删除子项并再次使用新约束添加它.
例:
public class TestPage : ContentPage
{
private RelativeLayout relativeLayout;
private BoxView view;
private bool moreWidth = false;
public TestPage()
{
this.view = new BoxView
{
BackgroundColor = Color.Yellow,
};
Button button = new Button
{
Text = "change",
TextColor = Color.Black,
};
button.Clicked += Button_Clicked;
this.relativeLayout = new RelativeLayout();
this.relativeLayout.Children.Add(this.view,
Constraint.Constant(0),
Constraint.Constant(0),
Constraint.Constant(80),
Constraint.RelativeToParent((parent) =>
{
return parent.Height;
}));
this.relativeLayout.Children.Add(button,
Constraint.RelativeToParent((parent) =>
{
return parent.Width / 2;
}),
Constraint.RelativeToParent((parent) =>
{
return parent.Height / 2;
}));
this.Content = this.relativeLayout;
}
private void Button_Clicked(object sender, EventArgs e)
{
double width = 0;
if(this.moreWidth)
{
width = 120;
}
else
{
width = 80;
}
var c= BoundsConstraint.FromExpression((Expression<Func<Rectangle>>)(() => new Rectangle(0, 0, width, this.Content.Height)), new View[0]);
RelativeLayout.SetBoundsConstraint(this.view, c);
this.relativeLayout.ForceLayout();
this.moreWidth = !this.moreWidth;
}
}
Run Code Online (Sandbox Code Playgroud)
Kei*_*ome 13
当前版本的Xamarin Forms 无法正式实现.该RelativeLayout( -推测的性能,缓存解决约束)容器只需要添加/从它的孩子集合中移除项目时重新计算限制.尽管各种约束都是作为Bindable Properties实现的,但在更改时仍然无法重新计算.
我假设有一天意图是尊重约束更新,例如,这对于动画很有用,但是现在它看起来并不像那样工作.
无论其,我看了一下为RelativeLayout的反编译源,它是可能的方式破解一起围绕它-但可能无法满足您的需求,这取决于你有多少功能要求和多么复杂的约束定义.
请参阅此示例代码(关键部分是使用SetBoundsConstraint设置约束,它会覆盖添加的视图的内部计算边界 - 然后调用ForceLayout()):
public partial class App : Application
{
public App ()
{
var label = new Label {
Text = "Test",
HorizontalTextAlignment = TextAlignment.Center,
VerticalTextAlignment = TextAlignment.Center,
BackgroundColor = Color.Silver
};
var layout = new RelativeLayout ();
layout.Children.Add (label,
Constraint.Constant (50),
Constraint.Constant (100),
Constraint.Constant (260),
Constraint.Constant (30));
MainPage = new ContentPage {
Content = layout
};
var fwd = true;
layout.Animate ("bounce",
(delta) => {
var d = fwd ? delta : 1.0 - delta;
var y = 100.0 + (50.0 * d);
var c = BoundsConstraint.FromExpression ((Expression<Func<Rectangle>>)(() => new Rectangle (50, y, 260, 30)), new View [0]);
RelativeLayout.SetBoundsConstraint(label, c);
layout.ForceLayout ();
}, 16, 800, Easing.SinInOut, (f, b) => {
// reset direction
fwd = !fwd;
}, () => {
// keep bouncing
return true;
});
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8889 次 |
| 最近记录: |