Xamarin.Forms如何让按钮出现在一行中

Xamarin.Forms How to make the Buttons appear in one line

如何让按钮出现在一行


这是我背后的代码

namespace RowCounter.Forms
{
    public partial class MainPage : ContentPage
    {
        public int Number = 1;

        public MainPage()
        {
            InitializeComponent();
        }
        public object Children { get; private set; }
        void OnButtonClicked(object sender, EventArgs e)
        {
            Button button = new Button
            {
                Text = "NAME PRODJECT"+"  "+ Number,
                TextColor= Color.Red,
                WidthRequest =450,
                HorizontalOptions = LayoutOptions.Start
            };
            Button button2 = new Button
            {
                Text = "X",
                WidthRequest = 40,
                HorizontalOptions = LayoutOptions.End
            };
            MainStackLayout.Children.Add(button2);
            MainStackLayout.Children.Add(button);            
            Number++;
        }               
    }
}

和我的xaml代码

<StackLayout x:Name ="MainStackLayout" BackgroundColor="#9370db">
    <Button Text="СОЗДАТЬ ПРОЕКТ"    
            FontAttributes="Bold"
            FontSize="25"
            WidthRequest="250"
            HeightRequest="80"
            Clicked="OnButtonClicked"
            TextColor="#ff000000"
            BackgroundColor="#4b0082"/>
</StackLayout> 

请帮帮我

使用 Grid 而不是 Stacklayout。

来自文档:

Grid

The Grid is a layout that organizes its children into rows and columns, which can have proportional or absolute sizes. By default, a Grid contains one row and one column. In addition, a Grid can be used as a parent layout that contains other child layouts.

Stacklayout

A StackLayout organizes child views in a one-dimensional stack

例如,您的代码如下所示

<Grid x:Name="MainGrid" VerticalOptions="StartAndExpand">
      <Grid.RowDefinitions>

      </Grid.RowDefinitions>
      <Button Grid.Column="0"
              Grid.ColumnSpan="2"
              Grid.Row="0"
              Text="СОЗДАТЬ ПРОЕКТ"
              FontAttributes="Bold"
              FontSize="25"
              WidthRequest="250"
              HeightRequest="80"
              Clicked="OnButtonClicked"
              TextColor="#ff000000"
              BackgroundColor="#4b0082"/>
</Grid>

以及背后的代码

public int Number = 1;

public ButtonsPage()
{
    InitializeComponent();
}

public object Children { get; private set; }
void OnButtonClicked(object sender, EventArgs e)
{
    Button button = new Button
    {
        Text = "NAME PRODJECT" + "  " + Number,
        TextColor = Color.Red,
        WidthRequest = 450,
        HeightRequest = 40,
        HorizontalOptions = LayoutOptions.Start
    };
    Button button2 = new Button
    {
        Text = "X",
        WidthRequest = 40,
        HeightRequest = 40,
        HorizontalOptions = LayoutOptions.End
    };
    MainGrid.Children.Add(button, left: 0, top: Number);
    MainGrid.Children.Add(button2, left: 1, top: Number);
    Number++;
}

产生类似

的东西


第二个选项,尽管不推荐将嵌套Stacklayouts:a StacklayoutStacklayout 由两个方向设置为水平的按钮组成。这是不好的做法,不利于性能,因为 Jason Smith said in his famous talk at Evolve 2016.

使用网格。

一个小例子。

 <Grid
    Padding="20"
    ColumnDefinitions="Auto, Auto,Auto"
    RowDefinitions="Auto,Auto">

    <Button
        Grid.Row="1"
        Grid.Column="0"
        BackgroundColor="#4b0082"
        Clicked="Button_Clicked_1"
        FontAttributes="Bold"
        FontSize="16"
        HeightRequest="40"
        Text="NAME PRODJECT 1"
        TextColor="White"
        WidthRequest="200" />

    <Button
        Grid.Row="1"
        Grid.Column="3"
        BackgroundColor="#4b0082"
        Clicked="Button_Clicked"
        FontAttributes="Bold"
        FontSize="16"
        HeightRequest="40"
        HorizontalOptions="End"
        Text="X"
        TextColor="White"
        WidthRequest="40" />

</Grid>

如果您有多个按钮,网格就可以了。如果按钮数量超过5个,手机APP中可能会因为手机尺寸限制,导致按钮显示过度

可选的是使用 FlexLayout(参见:https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.flexlayout?view=xamarin-forms)。如果 space 不够,您可以水平布局按钮并自动分成多行。

示例:

FlexLayout flex = new FlexLayout();
flex.HorizontalOptions = LayoutOptions.Fill;
flex.VerticalOptions = LayoutOptions.StartAndExpand;
flex.Direction = FlexDirection.Row;
flex.Wrap = FlexWrap.Wrap;
//Add buttons
flex.Children.Add(button1);
flex.Children.Add(button2);
        ...

Stacklayout 可以是水平的(方向参数),但为了效率起见,我会使用网格或 table 布局。