C# Forms-堆栈布局标签超出设备宽度
可以设置元素的百分比宽度吗 我遇到的问题是第二个标签按下屏幕上的按钮,你怎么能强迫标签只占用可用空间呢。我试过设置元素的最小宽度C# Forms-堆栈布局标签超出设备宽度,c#,xamarin.forms,C#,Xamarin.forms,可以设置元素的百分比宽度吗 我遇到的问题是第二个标签按下屏幕上的按钮,你怎么能强迫标签只占用可用空间呢。我试过设置元素的最小宽度 newstacklayout { 方向=堆叠方向。水平, 间距=0, 儿童={ 新标签(){Text=“TITLE”,horizontalpoptions=LayoutOptions.Start}, 新标签(){Text=“fsdf dsfsd fsdfs ewtrey vjdgyu jhy jgh tyjht rhyrt rgtu gtr ujtrey gt yu
newstacklayout
{
方向=堆叠方向。水平,
间距=0,
儿童={
新标签(){Text=“TITLE”,horizontalpoptions=LayoutOptions.Start},
新标签(){Text=“fsdf dsfsd fsdfs ewtrey vjdgyu jhy jgh tyjht rhyrt rgtu gtr ujtrey gt yu tgrt uh tyui y5r rtuyfgtj yrjhrytjty jtyjty ruy ujh i rt”,水平选项=布局选项。中心,LineBreakMode=LineBreakMode.WordWrap},
新建按钮(){Text=“wee”,horizontalpoptions=LayoutOptions.EndAndExpand}
}
},
尝试使用OnSizeAllocated(双倍宽度,双倍高度)
Xamarin.Forms页面中的代码
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height);
Metrics.Instance.Width=width;
Metrics.Instance.Height=height;
}
public class Metrics
{
private static Metrics _instance;
protected SessionData ()
{
}
public double Width{ get; set; } //Width
public double Height{ get; set; } //Height
}
单例类保存宽度和高度并检查方向更改
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height);
Metrics.Instance.Width=width;
Metrics.Instance.Height=height;
}
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,}
}
var StackchildSize=Metrics.Width/3;
新堆栈布局
{
方向=堆叠方向。水平,
间距=0,
儿童={
新标签(){Text=“TITLE”,水平选项=布局选项。开始
WidthRequest=stackChildSize},
新标签(){Text=”“,WidthRequest=stackChildSize,},
新建按钮(){Text=“wee”,horizontalpoptions=LayoutOptions.EndAndExpand,
WidthRequest=stackChildSize,}
}
},您在StackLayout上尝试过MaxWidth吗?我遇到的问题是我不知道设备的宽度,也不知道如何在运行时获取它。Femil Shajin的回答告诉了我怎么做。谢谢你问这个问题,你应该一直使用网格来达到这个目的