2014-07-18 57 views
5

可以设置元素的百分比宽度吗?Xamarin.Forms - StackLayout标签超出设备宽度

问题得到的是第二个标签将按钮从屏幕上推下来,你怎么强制标签只占用可用的空间。我试过设置元素的最小宽度。

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

你试过MaxWidth在StackLayout? –

+0

我遇到的问题是我不知道设备的宽度,也不知道如何在运行时得到它。 Femil Shajin的回答已经告诉我如何。 – Cadab

+0

感谢提问这个问题 – Subha

回答

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类保存宽度和高度和方向检查变更

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,}          
} 

},

+0

谢谢工作。 – Cadab

+0

非常感谢... – Subha