【问题标题】:How to set the width of the fixed columns in xamarin如何在xamarin中设置固定列的宽度
【发布时间】:2019-04-30 16:20:55
【问题描述】:

我有一个显示化学残留物的 ListView,问题是当我添加特定残留物(“BORRAS”)时,我的列配置如下图所示

我认为有一个命名法来设置列的宽度,我该如何解决这个问题?如何使我的 ListView 看起来同质化?

我附上了我的 ListView.XAML:

<ListView
    SelectionMode="None"
    Margin="5,0,10,10"
    IsRefreshing="{Binding IsRefreshing}"
    HeightRequest="{Binding AltoListaResiduos}"
    SeparatorVisibility="None"  
    HasUnevenRows="true"
    ItemsSource="{Binding ListaResiduos}">

    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout Orientation="Horizontal"
                             VerticalOptions="FillAndExpand">

                    <Grid>
                        <Grid.ColumnDefinitions>
                            <!--NOMBRE RESIDUO-->
                            <ColumnDefinition Width="3*"/>
                            <!--CANTIDAD ESTIMADA-->
                            <ColumnDefinition Width="1.5*"/>
                            <!--NOMBRE ESTIMADA-->
                            <ColumnDefinition Width="1.5*"/>
                            <!--CANTIDAD CONTENEDOR-->
                            <ColumnDefinition Width="1.5*"/>
                            <!--NOMBRE CONTENEDOR-->
                            <ColumnDefinition Width="1.5*"/>
                        </Grid.ColumnDefinitions>

                        <Label Text="{Binding NombreResiduo}"
                               HorizontalOptions="FillAndExpand"                                                      
                               MaxLines="3"
                               VerticalTextAlignment="Center"
                               TextColor="{StaticResource das.color.texto}"
                               VerticalOptions="CenterAndExpand"
                               Grid.Column="0"
                               Margin="4,0">
                            <Label.FontSize>
                                <OnPlatform x:TypeArguments="x:Double" iOS="11" Android="11" />
                            </Label.FontSize>
                        </Label>
                        <Label Text="{Binding formattedCantidadEstimada}" 
                               HorizontalTextAlignment="End"
                               FontSize="Small" 
                               VerticalTextAlignment="Center"
                               TextColor="{StaticResource das.color.texto}"
                               VerticalOptions="CenterAndExpand"
                               Grid.Column="1"
                               Margin="4,0">
                        </Label>

                        <Label Text="{Binding Estimado}" 
                               HorizontalTextAlignment="End"
                               FontSize="Micro" 
                               VerticalTextAlignment="Center"
                               TextColor="{StaticResource das.color.texto}"
                               VerticalOptions="CenterAndExpand"
                               Grid.Column="2"
                               Margin="4,0">
                        </Label>

                        <Label Text="{Binding CantidadContenedor}" 
                               HorizontalTextAlignment="End" 
                               FontSize="Small" 
                               VerticalTextAlignment="Center"
                               TextColor="{StaticResource das.color.texto}"
                               VerticalOptions="CenterAndExpand"
                               Grid.Column="3"
                               Margin="4,0">
                        </Label>
                        <Label Text="{Binding Contenedor}" 
                               HorizontalTextAlignment="End"
                               FontSize="Micro" 
                               VerticalTextAlignment="Center"
                               TextColor="{StaticResource das.color.texto}"
                               VerticalOptions="CenterAndExpand"
                               Grid.Column="4"
                               Margin="4,0">
                        </Label>

                    </Grid>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

我是 Xamarin.Forms 的新手,我希望这个问题对其他人有所帮助,我怎样才能使我的列表看起来与所有的浪费一样? (与名称无关)对我有帮助吗?

【问题讨论】:

  • 您根本不需要StackLayout,因为您正在使用适当的列定义Grid
  • 你有正确的答案,你要写它来标记它是正确的吗? @TaylorD
  • 查看我的回答,以正确理解问题

标签: c# xaml listview xamarin xamarin.forms


【解决方案1】:

您根本不需要StackLayout,因为您正在使用适当的列定义Grid

这应该有助于您的网格正确跨越并填满整个空间

【讨论】:

    【解决方案2】:

    我感觉这种情况正在发生,因为您现在出于某种原因正在使用 VerticalTextAlignment,TextAlignment 属性(H,V)在其中都有奇怪的行为,即使 Xamarin 对 VerticalTextAlignment 说它

    获取或设置 Text 属性的垂直对齐方式。

    我宁愿使用YAlign,因为它没有这种行为,Xamarin 说 YAling 是这样的

    获取或设置标签边界内文本的垂直对齐方式。

    其次,当您使用网格布局定义列和行时,您永远不应该使用AndExpand 类型,因为您已经定义了最大区域并且它不会覆盖它。现在LayoutOptions 文档说的是CenterAndExpand 或 FillAndExpand 做的是

    一个 LayoutOptions 结构,描述了一个居中并展开的元素。

    另外,删除 StackLayout 只需将 Grid 保留在 ViewCell 中即可。

       <Label Text="{Binding Contenedor}" 
                               XAlign="End"
                               FontSize="Micro" 
                               YAlign="Center"
                               TextColor="{StaticResource das.color.texto}"                               
                               Grid.Column="4"
                               Margin="4,0"/>
    

    想了解 Xamarin 表单的最佳实践吗?在这里查看我的博客:https://heartbeat.fritz.ai/techniques-for-improving-performance-in-a-xamarin-forms-application-b439f2f04156

    【讨论】:

      猜你喜欢
      • 2021-12-08
      • 2015-07-13
      • 2020-12-29
      • 2016-06-01
      • 2015-07-05
      • 2014-09-06
      • 2018-01-16
      • 2016-07-13
      • 1970-01-01
      相关资源
      最近更新 更多