2014-07-18 59 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