簡體   English   中英

Xamarin.Forms - StackLayout標簽超出設備寬度

[英]Xamarin.Forms - StackLayout labels going outside device width

可以設置元素的百分比寬度嗎?

我遇到的問題是第二個標簽按下屏幕上的按鈕,你怎么能強制標簽只占用可用的空間。 我已經嘗試設置元素的最小寬度。

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

嘗試使用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,}                                      
 }

},

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM