可以设置元素的百分比宽度吗?
我遇到的问题是第二个标签将按钮推离屏幕,如何强制标签仅占用可用空间。我已经尝试设置元素的最小宽度。
new StackLayout
{
Orientation = StackOrientation.Horizontal,
Spacing = 0,
Children = {
new Label() { Text = "TITLE", HorizontalOptions = LayoutOptions.Start},
new Label() { Text = "fsdf dsfsd fsdfsdfs ewtrey vjdgyu jhy jgh tyjht rhyrt rgtu gtr ujtrey gt yu tgrt uh tyui y5r rtuyfgtj yrjhrytjtyjy jty t ruy ujh i rt", HorizontalOptions = LayoutOptions.Center, LineBreakMode = LineBreakMode.WordWrap},
new Button() { Text = "wee", HorizontalOptions = LayoutOptions.EndAndExpand}
}
},
答案 0 :(得分:8)
尝试使用OnSizeAllocated(双倍宽度,双倍高度),
代码在您的Xamarin.Forms页面
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height);
Metrics.Instance.Width=width;
Metrics.Instance.Height=height;
}
Singleton Class保存宽度和高度并检查方向更改
public class Metrics
{
private static Metrics _instance;
protected SessionData ()
{
}
public double Width{ get; set; } //Width
public double Height{ get; set; } //Height
}
创建Stacklayout
var StackchildSize = Metrics.Width/3;
new StackLayout
{
Orientation = StackOrientation.Horizontal,
Spacing = 0,
Children = {
new Label() { Text = "TITLE", HorizontalOptions = LayoutOptions.Start
WidthRequest=stackChildSize},
new Label() { Text = "<Your Text>", WidthRequest=stackChildSize,},
new Button() { Text = "wee", HorizontalOptions = LayoutOptions.EndAndExpand,
WidthRequest=stackChildSize,}
}
},