How to make square buttons grid in Xamarin.Forms?

时光怂恿深爱的人放手 提交于 2020-04-11 18:50:01

问题


I'm trying to create a Xamarin.Forms content page similar to an activity I created for Android:

So basically I want to have a grid of square buttons, which I could easily expand by adding more rows. My content page looks like that (only 1 row so far):

<?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>

I'm almost there, but the buttons resize according to their text length, which I want to prevent - all of them should be of equal size. How to do that?


回答1:


Since you had set

<ColumnDefinition Width="*" />

and the width of each column will be set as 1/3 width of screen when you have 3 columns.

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




回答2:


I eventually made it work, the solution was to change Horizontal Options of the button to Fill:

<?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>

The other way to do it is to follow @Lucas Zhang - MSFT instructions above.



来源:https://stackoverflow.com/questions/56013166/how-to-make-square-buttons-grid-in-xamarin-forms

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!