我面临相对布局的问题 . 我的目标是创建类似的布局:
免责声明:这个 should be 相对布局只是因为我需要添加一些其他元素的位置取决于这两个 .
这是我的代码:
var layout = new RelativeLayout();
var box = new BoxView { BackgroundColor = Color.Olive, WidthRequest = 50, HeightRequest = 50 };
var label = new Label
{
LineBreakMode = LineBreakMode.WordWrap,
Text = "Here is a lot of text ..... Here is a lot of text";
};
layout.Children.Add(box, Constraint.Constant(10), Constraint.Constant(10));
layout.Children.Add(label,
Constraint.RelativeToView(box, (relativeLayout, view) => view.X + view.Width + 20),
Constraint.RelativeToView(box, (relativeLayout, view) => view.Y),
//Constraint.RelativeToParent(relativeLayout => relativeLayout.Width - 20 - 50 -10));
MainPage = new ContentPage
{
Content = layout
};
这是我的问题 . 如果我没有添加注释行,则标签不在屏幕之外 . 喜欢这里:
如果我添加注释字符串(这是宽度约束),那么还有另一个奇怪的事情:文本没有完全显示 . 我的意思是应该有大约10个单词,但它们会突然消失 .
我没有设置任何 Height 约束,所以 should not 限制了标签的大小 .
你能帮帮我吗?谢谢!
2 回答
确保您正在设置RelativeLayout HorizontalOptions和VerticalOptions以填充屏幕
正确计算标签X约束
view.X + view.Width + 20
- >view.X + view.Width + 10
标签高度不正确,因为你没有设计它的方式 .
这是工作示例:
没有什么可以阻止你在相对布局中利用堆栈的布局 .
例如 .
当然,你需要修改 Value 观,但基本前提是存在 .