代码之家  ›  专栏  ›  技术社区  ›  Judson Abraham

如何自动调整滚动视图的大小以适应xamarin表单中的内容

  •  2
  • Judson Abraham  · 技术社区  · 6 年前

    嗨,我想自动调整滚动视图的大小以适应其内容。在横向模式的所有设备中,它应该滚动到列表视图的末尾。现在我的问题是,我必须根据设备不断更改滚动视图的高度,这是不正确的。这是我的密码。

    if (svMainBody.Children.Contains(grdListBody))
    {
    svMainBody.Children.Remove(grdListBody);
    svMain = new ScrollView { Content = grdListBody };
    svMainBody.Children.Add(svMain);
    }
    else
    {
    svMainBody.Children.Add(svMain);
    }
    rdListViewHeight.Height = height * 0.30;
    }
    }
    else
    {
    if (IsPortrait(this))
    {
    //Fix for scrolling only listview in portrait
    if (svMainBody.Children.Contains(svMain))
    {
    svMainBody.Children.Remove(svMain);
    svMainBody.Children.Add(grdListBody);
    }
    else
    {
    svMainBody.Children.Add(grdListBody);
    }
    
                                if (Settings.Flowsheet.sl1 == "")
                                    Settings.Flowsheet.sl1 = sl1.Height.ToString();
    
                                dstk1Height = Settings.Flowsheet.sl1 == "" ? sl1.Height : Convert.ToDouble(Settings.Flowsheet.sl1);
    
                                if (Device.RuntimePlatform == Device.Android)
                                    rdListViewHeight.Height = height - 160 - sl1.Height - 40;
    
                                else
                                    rdListViewHeight.Height = height - 160 - dstk1Height - 30;
                            }
                            else
                            {
                                //Fix for scrolling entire page in landscape
                                if (svMainBody.Children.Contains(grdListBody))
                                {
                                    svMainBody.Children.Remove(grdListBody);
                                    svMain = new ScrollView { Content = grdListBody };
                                    svMainBody.Children.Add(svMain);
                                }
                                else
                                {
                                    svMainBody.Children.Add(svMain);
                                }
                                rdListViewHeight.Height = height * 3.0;
                            }
    

    这是我的xaml代码

     <StackLayout  x:Name="svMainBody"  Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="13" VerticalOptions="FillAndExpand">
    
                        <Grid x:Name="grdListBody" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="13"  BackgroundColor="#EEEEEE"  VerticalOptions="FillAndExpand">
                            <Grid.RowSpacing>0</Grid.RowSpacing>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"></RowDefinition>
                                <RowDefinition x:Name="rdListViewHeight" Height="Auto"></RowDefinition>
                            </Grid.RowDefinitions>
                            <StackLayout Padding="10,10,10,10"  Grid.Row="0" x:Name="sl1" VerticalOptions="Start">
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"></RowDefinition>
                                        <RowDefinition Height="Auto"></RowDefinition>
                                        <RowDefinition Height="Auto"></RowDefinition>
                                        <RowDefinition Height="Auto"></RowDefinition>
                                        <RowDefinition Height="Auto"></RowDefinition>
                                        <RowDefinition Height="Auto"></RowDefinition>
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto"></ColumnDefinition>
                                        <ColumnDefinition Width="40"></ColumnDefinition>
                                        <ColumnDefinition Width="*"></ColumnDefinition>
                                    </Grid.ColumnDefinitions>
                                    <Label Grid.Row="0" Grid.Column="0" x:Name="lblSetTime" Text="{Binding SetTime}" TextColor="Black" HorizontalOptions="Start"></Label>
                                    <Label Grid.Row="0" Grid.Column="1" x:Name="lblMeetingRoom" Grid.ColumnSpan="2" Text="{Binding MeetingRoom}" TextColor="Black" HorizontalOptions="Start"></Label>
                                    <Label Grid.Row="1" Grid.Column="0" x:Name="lblSetByStrikeBy" Text="{Binding SetByStrikeBy}" TextColor="Black" HorizontalOptions="Start" IsVisible="{Binding IsVisibleLblSetByStrikeBy}"></Label>
                                    <Label Grid.Row="1" Grid.Column="1" x:Name="lblRoomSetter" Grid.ColumnSpan="2" Text="{Binding RoomSetter}" TextColor="Black" HorizontalOptions="Start" IsVisible="{Binding IsVisibleLblRoomSetter}"></Label>
                                    <Label Grid.Row="2" Grid.Column="0" x:Name="lblStartTime" Text="{Binding StartTime}" TextColor="Black" HorizontalOptions="Start"></Label>
                                    <Image  Grid.Row="2" Grid.Column="1" x:Name="imgPromoterScore" Source="" HeightRequest="20" WidthRequest="20" HorizontalOptions="Start"  IsVisible="False" ></Image>
                                    <Label Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" x:Name="lblClientCompany" Text="{Binding ClientCompany}" TextColor="Black" HorizontalOptions="Start"></Label>
                                    <Label Grid.Row="3" Grid.Column="0" x:Name="lblEndTime" Text="{Binding EndTime}" TextColor="Black" HorizontalOptions="Start"></Label>
                                    <Image  Grid.Row="3" Grid.Column="1" x:Name="imgPlannerScore" Source=""  HeightRequest="20" WidthRequest="20" HorizontalOptions="Start"  IsVisible="False"></Image>
                                    <Label Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="2" x:Name="lblClientPlanner" Text="{Binding ClientPlanner}" TextColor="Black" HorizontalOptions="Start"></Label>
                                    <Label Grid.Row="4" Grid.Column="0" x:Name="lblStrikeTime" Text="{Binding StrikeTime}" TextColor="Black" HorizontalOptions="Start"></Label>
                                    <Label Grid.Row="4" Grid.Column="1" x:Name="lblPostAs" Grid.ColumnSpan="2" Text="{Binding PostAs}" TextColor="Black" HorizontalOptions="Start"></Label>
                                    <Label Grid.Row="5" Grid.Column="0" x:Name="lblAction"  Text="{Binding Action}" TextColor="Black" HorizontalOptions="Start"></Label>
                                    <Label Grid.Row="5" Grid.Column="1" x:Name="lblVenuePlanner_PSAVSalesPerson"  Grid.ColumnSpan="2" Text="{Binding VennuePlanner_PSAVSalesPerson}" TextColor="Black" HorizontalOptions="Start"></Label>
                                </Grid>
                            </StackLayout>
    
                            <StackLayout Grid.Row="1"  VerticalOptions="FillAndExpand" Orientation="Vertical">
                                <BoxView  BackgroundColor="Black" HeightRequest="1" HorizontalOptions="FillAndExpand"/>
                                <StackLayout Padding="10,5,10,5" Orientation="Vertical" BackgroundColor="#EEEEEE" x:Name="sl2"  VerticalOptions="FillAndExpand">
                                    <local1:CustomListviewEventDetails
                                        x:Name="lstItems" HasUnevenRows="True" SeparatorVisibility="None" BackgroundColor="#EEEEEE" ItemsSource="{Binding lstViewItemSource}" ItemSelected="lstItems_ItemSelected" VerticalOptions="FillAndExpand"  
                                                           IsGroupingEnabled="True">
                                        <local1:CustomListviewEventDetails.GroupHeaderTemplate>
                                            <DataTemplate>
                                                <local1:CustomViewCellFS>
                                                    <StackLayout HorizontalOptions="FillAndExpand" >
                                                        <!--Padding="20,0,0,0" VerticalOptions="CenterAndExpand"-->
                                                        <!--HorizontalOptions="CenterAndExpand"-->
                                                        <Label Text="{Binding SectionName}" FontSize="15"  FontAttributes="Bold" TextColor="White" VerticalOptions="CenterAndExpand" VerticalTextAlignment="Center" XAlign="Center" />
                                                    </StackLayout>
                                                </local1:CustomViewCellFS>
                                            </DataTemplate>
                                        </local1:CustomListviewEventDetails.GroupHeaderTemplate>
                                        <local1:CustomListviewEventDetails.ItemTemplate>
                                            <DataTemplate>
                                                <ViewCell>
                                                    <ViewCell.View>
    
    
    
                                                        <StackLayout Padding="0,0,0,0">
                                                            <Grid RowSpacing="0">
                                                                <Grid.RowDefinitions>
                                                                    <RowDefinition Height="Auto"></RowDefinition>
                                                                    <RowDefinition Height="Auto"></RowDefinition>
                                                                    <RowDefinition Height="Auto"></RowDefinition>
                                                                    <RowDefinition Height="5"></RowDefinition>
                                                                </Grid.RowDefinitions>
                                                                <Grid.ColumnDefinitions>
                                                                    <ColumnDefinition Width="0.1*"></ColumnDefinition>
                                                                    <ColumnDefinition Width="0.9*"></ColumnDefinition>
                                                                </Grid.ColumnDefinitions>
                                                                <!--Label Grid.Row="0" Grid.ColumnSpan="2" Text="{Binding SectionName}" FontSize="15" TextColor="Black" HorizontalOptions="C
                                                                enterAndExpand"></Label>-->
                                                                <!--<Label Grid.Row="1" Grid.ColumnSpan="2" Text="{Binding PackageName}" FontSize="15" TextColor="Black" HorizontalOptions="StartAndExpand"></Label>
                                                                -->
                                                                <Label Grid.Row="0" Grid.Column="0" Text="{Binding PackageQuantity}" FontSize="15"  TextColor="Black" HorizontalOptions="Start" IsVisible="{Binding PackageNameVisibility}" FontAttributes="Bold" >
                                                                </Label>
                                                                <Label Grid.Row="0" Grid.Column ="1" Text="{Binding PackageName}" FontSize="15"  TextColor="Black" HorizontalOptions="Start" HorizontalTextAlignment="Start" IsVisible="{Binding PackageNameVisibility}" FontAttributes="Bold" ></Label>
                                                                <Label Grid.Row="1" Grid.Column="0" Text="{Binding Quantity}" FontSize="15" TextColor="Black" HorizontalOptions="Start" HorizontalTextAlignment="Start"  >
                                                                </Label>
                                                                <Label  Grid.Row="1" Grid.Column="1" Text="{Binding Item}" FontSize="15"  TextColor="Black" HorizontalOptions="StartAndExpand" HorizontalTextAlignment="Start" Margin="{Binding MargProp}" >
                                                                </Label>
                                                                <!--<Label Grid.Row="2" Grid.Column="0" Text="{Binding SectionQuantity}" FontSize="15" TextColor="Black" HorizontalOptions="Start" HorizontalTextAlignment="Start" IsVisible="{Binding SectionItemVisibility}" >
                                                                </Label>
                                                                <Label  Grid.Row="2" Grid.Column="1" Text="{Binding SectionItem}" FontSize="15" TextColor="Black" HorizontalOptions="StartAndExpand" HorizontalTextAlignment="Start" IsVisible="{Binding SectionItemVisibility}" >
                                                                </Label>-->
                                                                <BoxView x:Name="txt" Grid.Row="3" Grid.ColumnSpan="2" HeightRequest="5" ></BoxView>
    
                                                            </Grid>
                                                        </StackLayout>
                                                    </ViewCell.View>
                                                </ViewCell>
                                            </DataTemplate>
                                        </local1:CustomListviewEventDetails.ItemTemplate>
                                    </local1:CustomListviewEventDetails>
                                </StackLayout>
                            </StackLayout>
                        </Grid>
    
                    </StackLayout>
    

    我正在将高度设置为3.0,但我希望高度是自动的。

    2 回复  |  直到 6 年前
        1
  •  1
  •   ico2k2    6 年前

    我不太明白。

    • 如果你把 ListView 在a中 ScrollView :坏主意。 列表视图 本机支持滚动。

    • 如果你想要 卷轴视图 获取儿童的尺寸,如 LinearLayout 具有 android:layout_height="wrap_content" :我认为只有像您这样通过编程才能实现。

        2
  •  1
  •   Judson Abraham    6 年前

    我已通过指定

     rdListViewHeight.Height = Content.Height;
    

    而不是

     rdListViewHeight.Height = height * 3.0;