【问题标题】:Xamarin.Forms - StackLayout labels going outside device widthXamarin.Forms - StackLayout 标签超出设备宽度
【发布时间】:2014-09-09 10:51: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}                                      
     }
 },       

【问题讨论】:

  • 你在 StackLayout 上试过 MaxWidth 了吗?
  • 我遇到的问题是我不知道设备的宽度,也不知道如何在运行时获取它。 Femil Shajin 的回答向我展示了如何。
  • 感谢您提出这个问题
  • 你应该总是使用网格来达到这个目的

标签: c# xamarin.forms


【解决方案1】:

尝试使用 OnSizeAllocated(双宽,双高),

Xamarin.Forms 页面中的代码

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
        }

创建堆栈布局

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

},

【讨论】:

    猜你喜欢
    • 2017-08-29
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 2019-10-11
    • 2012-04-03
    • 2020-08-20
    • 1970-01-01
    • 2017-02-08
    相关资源
    最近更新 更多