gpt4 book ai didi

c# - Xamarin.Forms 如何使按钮显示在一行中

转载 作者:行者123 更新时间:2023-12-02 16:06:43 25 4
gpt4 key购买 nike

如何让按钮出现在一行

enter image description here


这是我的代码背后

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 andcolumns, which can have proportional or absolute sizes. By default, aGrid contains one row and one column. In addition, a Grid can be usedas 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++;
}

产生类似的东西

enter image description here


第二个选项,虽然不推荐是嵌套Stacklayout:一个Stacklayout>Stacklayout由两个方向设置为水平的按钮组成。这是不好的做法,对性能不利,如Jason Smith said in his famous talk at Evolve 2016.

关于c# - Xamarin.Forms 如何使按钮显示在一行中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69250555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com