简体   繁体   English

Xamarin.Forms - StackLayout标签超出设备宽度

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

Can set percentage widths for elements? 可以设置元素的百分比宽度吗?

The problem ive got is the 2nd label pushes the button off screen, how can you force a label to only take up the space available. 我遇到的问题是第二个标签按下屏幕上的按钮,你怎么能强制标签只占用可用的空间。 I've tried setting minimum width of the elements. 我已经尝试设置元素的最小宽度。

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

Try Using OnSizeAllocated(double width, double height), 尝试使用OnSizeAllocated(双倍宽度,双倍高度),

Code In your Xamarin.Forms Page 代码在您的Xamarin.Forms页面

protected override void OnSizeAllocated(double width, double height)
    {
        base.OnSizeAllocated(width, height);

        Metrics.Instance.Width=width;
        Metrics.Instance.Height=height;
    }

Singleton Class to Save the width and Height and Check orientation change Singleton类保存宽度和高度并检查方向更改

public class Metrics
    {

        private static Metrics _instance;

        protected SessionData ()
        {
        }

        public double Width{ get; set; } //Width

        public double Height{ get; set; } //Height
        }

Creating Stacklayout 创建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