即使在应用间距和填充之后,Xamarin Form仍保留BoxView和Button之间的空间

时间:2017-07-13 22:11:00

标签: xaml xamarin xamarin.forms

我在Xamarin Forms中有以下ContentPage。它有一个网格。我已经为网格应用了ColumnSpacing="0" RowSpacing="0" Padding="0"。我已经为BoxView和Button应用了Margin="0"。 BoxView和Button之间仍有空间。

避免这种不必要的间距的最佳方法是什么?

XAML

<Grid x:Name="controlGrid" ColumnSpacing="0" RowSpacing="0" Padding="0">
    <Grid.RowDefinitions>
        <RowDefinition Height="4*" />
        <RowDefinition Height="4*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>


    <!--Row 1-->
    <views:SKCanvasView PaintSurface="OnSecondPainting"  Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" >
    </views:SKCanvasView>

    <!--Row 2-->
    <Grid x:Name="secondGrid"  Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" ColumnSpacing="0" RowSpacing="0" Padding="0">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <BoxView Grid.Row="0" Grid.Column="0" BackgroundColor="Red" HeightRequest="1" VerticalOptions="End" HorizontalOptions="FillAndExpand" Margin="0" />
        <BoxView Grid.Row="0" Grid.Column="0" BackgroundColor="Green" HeightRequest="1" VerticalOptions="Start" HorizontalOptions="FillAndExpand" Margin="0"/>
        <BoxView Grid.Row="0" Grid.Column="0" BackgroundColor="Blue" WidthRequest="1" VerticalOptions="FillAndExpand" HorizontalOptions="End" Margin="0"/>
        <BoxView Grid.Row="0" Grid.Column="0" BackgroundColor="Orange" WidthRequest="1" VerticalOptions="FillAndExpand" HorizontalOptions="Start" Margin="0"/>

        <BoxView Grid.Row="0" Grid.Column="1" BackgroundColor="Crimson" HeightRequest="1" VerticalOptions="End" HorizontalOptions="FillAndExpand" Margin="0" />
        <BoxView Grid.Row="0" Grid.Column="1" BackgroundColor="Cyan" HeightRequest="1" VerticalOptions="Start" HorizontalOptions="FillAndExpand" Margin="0"/>
        <BoxView Grid.Row="0" Grid.Column="1" BackgroundColor="Blue" WidthRequest="1" VerticalOptions="FillAndExpand" HorizontalOptions="End" Margin="0"/>
        <BoxView Grid.Row="0" Grid.Column="1" BackgroundColor="Orange" WidthRequest="1" VerticalOptions="FillAndExpand" HorizontalOptions="Start" Margin="0"/>


        <Button x:Name="ClickMe1" Text="ClickMe 1" Grid.Row="0" Grid.Column="0"  Margin="0" />
        <Button x:Name="ClickMe2" Text="ClickMe 2" Grid.Row="0" Grid.Column="1" Margin="0" />
    </Grid>

</Grid>

截图

enter image description here

1 个答案:

答案 0 :(得分:1)

按钮具有默认填充。因此,当您尝试将按钮对齐在一起时,总会有空格。要么你必须给-ve margin而不是按钮你必须使用stacklayout / boxview和手势来点击命令。

检查一下:

        <Grid x:Name="controlGrid" ColumnSpacing="0" RowSpacing="0" Padding="0">
        <Grid.RowDefinitions>
            <RowDefinition Height="4*" />
            <RowDefinition Height="4*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <!--Row 2-->
        <Grid x:Name="secondGrid"  Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" ColumnSpacing="0" RowSpacing="0" Padding="0">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <BoxView Grid.Row="0" Grid.Column="0" BackgroundColor="Red" HeightRequest="1" VerticalOptions="End" HorizontalOptions="FillAndExpand" Margin="0" />
            <BoxView Grid.Row="0" Grid.Column="0" BackgroundColor="Green" HeightRequest="1" VerticalOptions="Start" HorizontalOptions="FillAndExpand" Margin="0"/>
            <BoxView Grid.Row="0" Grid.Column="0" BackgroundColor="Blue" WidthRequest="1" VerticalOptions="FillAndExpand" HorizontalOptions="End" Margin="0"/>
            <BoxView Grid.Row="0" Grid.Column="0" BackgroundColor="Orange" WidthRequest="1" VerticalOptions="FillAndExpand" HorizontalOptions="Start" Margin="0"/>

            <BoxView Grid.Row="0" Grid.Column="1" BackgroundColor="Crimson" HeightRequest="1" VerticalOptions="End" HorizontalOptions="FillAndExpand" Margin="0" />
            <BoxView Grid.Row="0" Grid.Column="1" BackgroundColor="Cyan" HeightRequest="1" VerticalOptions="Start" HorizontalOptions="FillAndExpand" Margin="0"/>
            <BoxView Grid.Row="0" Grid.Column="1" BackgroundColor="Blue" WidthRequest="1" VerticalOptions="FillAndExpand" HorizontalOptions="End" Margin="0"/>
            <BoxView Grid.Row="0" Grid.Column="1" BackgroundColor="Orange" WidthRequest="1" VerticalOptions="FillAndExpand" HorizontalOptions="Start" Margin="0"/>
            <StackLayout  Grid.Row="0"  Grid.Column="0" HorizontalOptions="FillAndExpand" Margin="1" VerticalOptions="FillAndExpand" BackgroundColor="Gray">
                <StackLayout.GestureRecognizers>
                    <TapGestureRecognizer Command="{Binding Button1Command}"/>
                </StackLayout.GestureRecognizers>
                <Label Text="Click Me1" HorizontalOptions="Center" VerticalOptions="CenterAndExpand"/>
            </StackLayout>
            <StackLayout Grid.Row="0"  Grid.Column="1" HorizontalOptions="FillAndExpand" Margin="1" VerticalOptions="FillAndExpand"  BackgroundColor="Gray">
                <StackLayout.GestureRecognizers>
                    <TapGestureRecognizer Command="{Binding Button1Command}"/>
                </StackLayout.GestureRecognizers>
                <Label Text="Click Me2"  HorizontalOptions="Center" VerticalOptions="CenterAndExpand"/>
            </StackLayout>
        </Grid>
    </Grid>

enter image description here