首页 > 解决方案 > 按钮不适合屏幕 xamarin

问题描述

嘿,我必须在我的应用程序中添加 +20 按钮

我用这个代码

var layout = new StackLayout() {
            Orientation = StackOrientation.Horizontal,
            HorizontalOptions = LayoutOptions.Start,
            VerticalOptions = LayoutOptions.Start,
        };


for (int i = 1; i < 20; i++)
        {
            var button = new Button
            {
                Text =  i.ToString(),
                HeightRequest = 45,
                WidthRequest = 45,
                Margin = 5,
                BorderRadius = 100,
                VerticalOptions = LayoutOptions.Center,


            };
            grid.Children.Add(button);

        }
        Content = layout;



    }

所以现在按钮正在远离屏幕

我必须得到类似的东西

标签: xamarinxamarin.forms

解决方案


使用FlexLayout而不是StackLayout

    var layout = new FlexLayout();

    for (int i = 1; i < 20; i++)
    {
        var button = new Button
        {
            Text =  i.ToString(),
            HeightRequest = 45,
            WidthRequest = 45,
            Margin = 5,
            BorderRadius = 100,
            VerticalOptions = LayoutOptions.Center,
        };

        layout.Children.Add(button);

    }

    Content = layout;

推荐阅读