【问题标题】:How to adjust size of Horizontal ListView in Xamarin.Forms?如何在 Xamarin.Forms 中调整水平 ListView 的大小?
【发布时间】:2014-06-26 20:39:55
【问题描述】:

我看到this entry 并尝试一下,是的,滚动方向变为水平。

但是,视图的大小不会根据视图的轮廓进行调整。

结果如下:

即使旋转了,如何调整视图的大小?

【问题讨论】:

    标签: listview xamarin xamarin.forms


    【解决方案1】:

    您可以使用相对布局来调整位置。这是我使用旋转的垂直列表视图实现的水平列表视图的 XAML 示例:

    <?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="ExampleProject.SomePage" >
        <ContentPage.Content>
            <RelativeLayout>
                <ListView x:Name="listView"
                          ItemsSource="{Binding ExampleList}"
                          RowHeight="120"
                          Rotation="270"
                          RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.5, Constant=-60}"
                          RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=-0.5, Constant=60}"
                          RelativeLayout.WidthConstraint="{ConstraintExpression Type=Constant, Constant=120}"
                          RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1}"
                          >
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <ViewCell.View>
                                    <Label Rotation="90"
                                           TranslationX="120" />
                                </ViewCell.View>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
                <StackLayout
                    RelativeLayout.XConstraint="{ConstraintExpression Type=Constant, Constant=0}"
                    RelativeLayout.YConstraint="{ConstraintExpression Type=Constant, Constant=120}"
                    RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1}"
                    RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=1, Constant=-120}"
                    >
                    <!-- Some content below list view -->
                </StackLayout>
            </RelativeLayout>
        </ContentPage.Content>
    </ContentPage>
    

    这应该适用于 iOS 和 Android。

    【讨论】:

    • 你试过了吗?这行得通吗?我不是说应该工作
    • 是的,这是直接从 Xamarin.Forms 项目复制的。我刚刚在安卓和iOS上测试过,不知道在Windows Phone上能不能用。
    【解决方案2】:

    ListView 是在未旋转的情况下测量的。如果你把它做成正方形,它不应该在旋转后从屏幕上流出来。

    【讨论】:

      猜你喜欢
      • 2021-10-22
      • 2020-06-15
      • 2017-10-17
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      • 2021-08-20
      相关资源
      最近更新 更多