【问题标题】:reduce spacing between ListView Items in Xamarin forms减少 Xamarin 表单中 ListView 项之间的间距
【发布时间】:2017-04-12 12:15:18
【问题描述】:

我在 xamarin 便携式应用程序中有这个列表,我似乎无法删除或至少减少项目之间的间距,也无法禁用项目选择。

<ListView x:Name="ListGroups" 
           ItemsSource="{Binding Source={x:Static local:Stash.Groups}}" 
           HorizontalOptions="Center" >
   <ListView.ItemTemplate>
    <DataTemplate >
      <ViewCell>
             <Label Text="{Binding Name}" TextColor="Aqua" FontSize="10" HorizontalOptions="Center" />
      </ViewCell>
    </DataTemplate>
   </ListView.ItemTemplate>
 </ListView>

然后是一些标签

 <Label Text="If you feel you're missing"
            FontSize="15"
            VerticalOptions="Center"
            HorizontalOptions="Center"
            TextColor="White"
            />
     <Label Text="a group or two, please contact"
               FontSize="15"
            VerticalOptions="Center"
            HorizontalOptions="Center"
            TextColor="White"
            />
     <Label Text="your manager"
             FontSize="15"
            VerticalOptions="Center"
            HorizontalOptions="Center"
            TextColor="White"
            />


     </StackLayout>

ImageBackgroundcolor

【问题讨论】:

    标签: c# xaml xamarin xamarin.forms cross-platform


    【解决方案1】:

    你可以试试

    HasUnevenRows = "true"
    

    对于“禁用选择”,您可以按照这些操作

    Disabling selection

    SelectionDemoList.ItemSelected += (sender, e) => {
        ((ListView)sender).SelectedItem = null;
    };
    

    请注意,在 Windows Phone 上,包括 SwitchCell 在内的某些单元格不会更新其视觉状态以响应选择。

    要降低 ListView 的高度,您可以使用 Grid。这是一个样本

    <?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="TestRelativeLayout.MyPage2">
        <ContentPage.Content>
            <StackLayout>
                        <StackLayout>
                                            <Grid VerticalOptions = "FillAndExpand">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="50" />
                        <RowDefinition/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="10*" />
                        <ColumnDefinition Width = "1*"/>
                        </Grid.ColumnDefinitions>
                <Entry Placeholder="MyEntry" Grid.Column = "0" Grid.Row="0" Grid.ColumnSpan = "2"/>
                <Image Source="icon.png" Grid.Column="1" Grid.Row = "0" Margin="0,0,20,0"/>
                    </Grid>
            </StackLayout>
                                    <Grid VerticalOptions = "FillAndExpand">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="150" />
                        <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                            <ListView x:Name="ListGroups" 
                        Grid.Row = "0"
                        Grid.Column = "0"
               ItemsSource="{Binding myList}" 
               HorizontalOptions="Center"
                    VerticalOptions="Start"
                    BackgroundColor = "Red">
       <ListView.ItemTemplate>
        <DataTemplate >
          <ViewCell>
                 <Label Text="{Binding Name}" TextColor="Aqua" FontSize="10" HorizontalOptions="Center" />
          </ViewCell>
        </DataTemplate>
       </ListView.ItemTemplate>
     </ListView>
                <StackLayout Grid.Row = "1" Grid.Column = "0" >
                <Label Text="If you feel you're missing"
                FontSize="15"
                VerticalOptions="StartAndExpand"
                HorizontalOptions="Center"
                TextColor="Black"
                />
         <Label Text="a group or two, please contact"
                   FontSize="15"
                VerticalOptions="StartAndExpand"
                HorizontalOptions="Center"
                TextColor="Black"
                />
         <Label Text="your manager"
                 FontSize="15"
                VerticalOptions="StartAndExpand"
                HorizontalOptions="Center"
                TextColor="Black"
                />
                </StackLayout>
                </Grid>
    
            </StackLayout>
        </ContentPage.Content>
    </ContentPage>
    

    你有这个(在模拟器/Android上)

    【讨论】:

    • 它实际上工作得很好,谢谢,你有禁用选择的解决方案吗? @亚历山德罗·卡利亚罗
    • ListView_OnItemSelected(object sender, SelectedItemChangedEventArgs e) { (sender as ListView).SelectedItem = null; }
    • 但现在我在列表下有一个未使用的空间,我如何删除它,它不会去@Alessandro Caliaro
    • 我认为你可以玩“VerticalOptions”。类似“开始”的东西......没有“展开”
    • 在列表视图中你的意思是@AlessandroCaliaro