Xamarin的.窗体- StackLayout标签超出设备宽度

本文关键字:标签 窗体 StackLayout Xamarin | 更新日期: 2023-09-27 18:13:32

可以设置元素的百分比宽度吗?

我的问题是第二个标签把按钮推离屏幕,你怎么能强迫一个标签只占用可用的空间。我试过设置元素的最小宽度。

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

Xamarin的.窗体- StackLayout标签超出设备宽度

尝试使用OnSizeAllocated(double width, double height),

Code In your Xamarin。表单页面

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

}