如何在Xamarin.Forms中制作方形按钮网格?

时间:2019-05-06 22:28:38

标签: c# xaml xamarin grid

我正在尝试创建一个Xamarin.Forms内容页面,该页面类似于我为Android创建的活动: enter image description here

所以基本上,我希望有一个方形按钮网格,可以通过添加更多行来轻松扩展它。我的内容页面看起来像这样(到目前为止只有1行):

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="TravelGuide.MainPage">
    <ContentPage.Content>
        <ScrollView>
            <Grid x:Name="MainGrid">
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
            <Button Text="Text..."
            x:Name="btn1"
            VerticalOptions="CenterAndExpand"
            HorizontalOptions="Center"
            HeightRequest="{Binding Width, Source={x:Reference btn1}}"
            Grid.Row="0"
            Grid.Column="0"/>

            <Button Text="Text2..."
            x:Name="btn2"
            VerticalOptions="CenterAndExpand"
            HorizontalOptions="Center"
            HeightRequest="{Binding Width, Source={x:Reference btn2}}"
            Grid.Row="0"
            Grid.Column="1"/>

            <Button Text="Text3..."
             x:Name="btn3"
            VerticalOptions="CenterAndExpand"
            HorizontalOptions="Center"
            HeightRequest="{Binding Width, Source={x:Reference btn3}}"
            Grid.Row="0"
            Grid.Column="2"/>
        </Grid>
    </ScrollView>
</ContentPage.Content>

我快到了,但是按钮会根据其文本长度调整大小,我想防止这种情况-所有按钮的大小都应相等。该怎么做?

2 个答案:

答案 0 :(得分:0)

自设置好

<ColumnDefinition Width="*" />

,当您有3列时,每列的宽度将设置为屏幕宽度的1/3。

<ScrollView>
        <Grid x:Name="MainGrid">
            <Grid.RowDefinitions>
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="0.33*" />
                <ColumnDefinition Width="0.33*" />
                <ColumnDefinition Width="0.33*" />
            </Grid.ColumnDefinitions>
            <Button Text="aaaaaaaaaaaaaaaaaa"
            x:Name="btn1"
            VerticalOptions="CenterAndExpand"
            HorizontalOptions="Center"
            HeightRequest="{Binding Width, Source={x:Reference btn1}}"
            Grid.Row="0"
            Grid.Column="0"/>

            <Button Text="Text2..."
            x:Name="btn2"
            VerticalOptions="CenterAndExpand"
            HorizontalOptions="Center"
            WidthRequest="{Binding Width, Source={x:Reference btn1}}"
            HeightRequest="{Binding Width, Source={x:Reference btn2}}"
            Grid.Row="0"
            Grid.Column="1"/>

            <Button Text="Text3..."
             x:Name="btn3"
            VerticalOptions="CenterAndExpand"
            HorizontalOptions="Center"
            WidthRequest="{Binding Width, Source={x:Reference btn1}}"
            HeightRequest="{Binding Width, Source={x:Reference btn3}}"
            Grid.Row="0"
            Grid.Column="2"/>

            <Button Text="aaaaaaaaaaaaaaaaa"
            x:Name="btn4"
            VerticalOptions="CenterAndExpand"
            HorizontalOptions="Center"
                    WidthRequest="{Binding Width, Source={x:Reference btn1}}"
            HeightRequest="{Binding Width, Source={x:Reference btn4}}"
            Grid.Row="1"
            Grid.Column="0"/>

            <Button Text="Text5..."
            x:Name="btn5"
            VerticalOptions="CenterAndExpand"
            HorizontalOptions="Center"
                    WidthRequest="{Binding Width, Source={x:Reference btn1}}"
            HeightRequest="{Binding Width, Source={x:Reference btn5}}"
            Grid.Row="1"
            Grid.Column="1"/>

            <Button Text="Text6..."
             x:Name="btn6"
            VerticalOptions="CenterAndExpand"
            HorizontalOptions="Center"
                    WidthRequest="{Binding Width, Source={x:Reference btn1}}"
            HeightRequest="{Binding Width, Source={x:Reference btn6}}"
            Grid.Row="1"
            Grid.Column="2"/>

        </Grid>
    </ScrollView>

enter image description here

答案 1 :(得分:0)

我最终成功了,解决方法是将按钮的“水平选项”更改为“填充”:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="TravelGuide.MainPage">
    <ContentPage.Content>
        <ScrollView Padding="5">
            <Grid x:Name="MainGrid">
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Button Text="Text..."
                x:Name="btn1"
                VerticalOptions="CenterAndExpand"
                HorizontalOptions="Fill"
                HeightRequest="{Binding Width, Source={x:Reference btn1}}"
                Grid.Row="0"
                Grid.Column="0"/>

                <Button Text="Text2..."
                x:Name="btn2"
                VerticalOptions="CenterAndExpand"
                HorizontalOptions="Fill"
                HeightRequest="{Binding Width, Source={x:Reference btn2}}"
                Grid.Row="0"
                Grid.Column="1"/>

                <Button Text="Text3..."
                 x:Name="btn3"
                VerticalOptions="CenterAndExpand"
                HorizontalOptions="Fill"
                HeightRequest="{Binding Width, Source={x:Reference btn3}}"
                Grid.Row="0"
                Grid.Column="2"/>

                <Button Text="Text3222222..."
                 x:Name="btn4"
                VerticalOptions="CenterAndExpand"
                HorizontalOptions="Fill"
                HeightRequest="{Binding Width, Source={x:Reference btn3}}"
                Grid.Row="1"
                Grid.Column="0"/>
            </Grid>
        </ScrollView>
    </ContentPage.Content>
</ContentPage>

另一种方法是遵循@Lucas Zhang-MSFT上面的说明。