Xamarin Forms使按钮适合网格

问题描述 投票:0回答:1

我有一个只有1列的网格,我希望我的按钮适合网格的大小,我尝试使用StackLayout和Frame,现在我尝试使用Grid,也尝试使用1列和1行,但是结果是一样的,我将附上一张照片以显示会发生什么:

enter image description here

我需要红色按钮进行拉伸,以便它们填充设备的宽度,我尝试在水平选项中使用StartAndExpand,Fill和FillAndExpand属性,但是它们不起作用。使用Fill和FillAndExpand可以正常工作,但是按钮文本居中,然后有一个错误:每次我点击一行时,文本都向左移动,并停留在那里,除非我向下滚动列表视图并再次返回顶部。

这是我的代码:

<ListView x:Name="GroupsList"
                  ItemsSource="{Binding Dishes}"
                  IsGroupingEnabled="True"
                  SeparatorColor="Black"
                  SeparatorVisibility="Default"
                  HasUnevenRows="True">
            <ListView.Behaviors>
                <behaviorsPack:SelectedItemBehavior Command="{Binding BindingContext.SelectedDishCommand, Source={x:Reference DishesPage}}"></behaviorsPack:SelectedItemBehavior>
            </ListView.Behaviors>
            <ListView.GroupHeaderTemplate>
                <DataTemplate>
                    <ViewCell Height="50">
                        <Grid VerticalOptions="FillAndExpand"
                                     BackgroundColor="LightSlateGray">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"></ColumnDefinition>
                            </Grid.ColumnDefinitions>
                            <Button BackgroundColor="DarkRed"
                                    Grid.Column="0"
                                    BorderColor="Transparent"
                                    BorderWidth="0"
                                    Text="{Binding Key}"
                                    TextColor="White"
                                    HorizontalOptions="StartAndExpand"
                                    Command="{Binding BindingContext.SelectGroupHeader, Source={x:Reference DishesPage}}"
                                    CommandParameter="{Binding Key}"
                                    ImageSource="next_disclosure"
                                    ContentLayout="Right"></Button>
                        </Grid>
                    </ViewCell>
            </DataTemplate>
            </ListView.GroupHeaderTemplate>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ContentView Padding="2, 5, 5, 0">
                            <Frame Padding="2"
                                   HasShadow="False"
                                   BackgroundColor="White">
                                <StackLayout Orientation="Horizontal">
                                    <Label Margin="10"
                                           Text="{Binding Name}"
                                           TextColor="Black"
                                           FontSize="Medium"
                                           HorizontalOptions="StartAndExpand"></Label>
                                </StackLayout>
                            </Frame>
                        </ContentView>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

我希望任何人都可以帮助我。

xaml xamarin xamarin.forms
1个回答
0
投票
自定义

按钮:

public class ExtendedButton:Button { public static BindableProperty HorizontalTextAlignmentProperty = BindableProperty.Create<ExtendedButton, Xamarin.Forms.TextAlignment>(x => x.HorizontalTextAlignment, Xamarin.Forms.TextAlignment.Center); public Xamarin.Forms.TextAlignment HorizontalTextAlignment { get { return (Xamarin.Forms.TextAlignment)GetValue(HorizontalTextAlignmentProperty); } set { SetValue(HorizontalTextAlignmentProperty, value); } } } 然后

Android.Project
中的自定义渲染器,如:

[assembly: ExportRenderer(typeof(ExtendedButton), typeof(ExtendedButtonRenderer))] namespace App18.Droid { public class ExtendedButtonRenderer : Xamarin.Forms.Platform.Android.AppCompat.ButtonRenderer { public ExtendedButtonRenderer(Context context) : base(context) { } public new ExtendedButton Element { get { return (ExtendedButton)base.Element; } } protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e) { base.OnElementChanged(e); if (e.NewElement == null) { return; } SetTextAlignment(); } protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { base.OnElementPropertyChanged(sender, e); if (e.PropertyName == ExtendedButton.HorizontalTextAlignmentProperty.PropertyName) { SetTextAlignment(); } } public void SetTextAlignment() { Control.Gravity = Element.HorizontalTextAlignment.ToHorizontalGravityFlags(); } } public static class AlignmentHelper { public static GravityFlags ToHorizontalGravityFlags(this Xamarin.Forms.TextAlignment alignment) { if (alignment == Xamarin.Forms.TextAlignment.Center) return GravityFlags.AxisSpecified; return alignment == Xamarin.Forms.TextAlignment.End ? GravityFlags.Right|GravityFlags.CenterVertical : GravityFlags.Left|GravityFlags.CenterVertical; } } } 最终在页面的axml中:

<ListView.GroupHeaderTemplate>
            <DataTemplate>
                <ViewCell Height="50">
                    <Grid VerticalOptions="FillAndExpand"
                                 BackgroundColor="LightSlateGray">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <local:ExtendedButton BackgroundColor="DarkRed"
                                Grid.Column="0"
                                BorderColor="Transparent"
                                BorderWidth="0"
                                Text="{Binding Key}"
                                TextColor="White"
                                HorizontalTextAlignment="Start" 
                                HorizontalOptions="FillAndExpand"
                                Command="{Binding BindingContext.SelectGroupHeader, Source={x:Reference DishesPage}}"
                                CommandParameter="{Binding Key}"
                                ImageSource="next_disclosure"
                                ContentLayout="Right"></local:ExtendedButton>
                    </Grid>
                </ViewCell>
        </DataTemplate>
</ListView.GroupHeaderTemplate>
© www.soinside.com 2019 - 2024. All rights reserved.