Xamarin在横向上形成stacklayout填充屏幕

时间:2016-08-02 17:22:05

标签: xaml xamarin.forms

当设备处于横向时,如何进行stacklayout获取所有屏幕宽度?  in Landscape mode stacklayout does not fill parent

这是我的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"
         xmlns:local="clr-namespace:ListView"
         x:Class="ListView.MainPage">
<ContentPage.Content>
        <AbsoluteLayout BackgroundColor="Silver" HorizontalOptions="FillAndExpand">
        <StackLayout Orientation="Vertical" HorizontalOptions="StartAndExpand" BackgroundColor="Maroon" VerticalOptions="StartAndExpand">
            <Button Text="Reload data" Clicked="reloadData" x:Name="btnReload"/>
            <ListView x:Name="listView">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout BackgroundColor="#eee" Orientation="Vertical">
                                <StackLayout Orientation="Horizontal"  VerticalOptions="FillAndExpand">
                                    <Image Source="{Binding Imagen}" HeightRequest="100" WidthRequest="120"/>
                                    <Label Text="{Binding Titulo}" TextColor="Gray"/>
                                    <Label Text="{Binding Fecha}"  HorizontalOptions="EndAndExpand"/>
                                </StackLayout>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
        <ActivityIndicator BackgroundColor="Green" AbsoluteLayout.LayoutBounds=".5,.5,.2,.2" AbsoluteLayout.LayoutFlags="All" Color="Blue"
                            IsRunning="True" IsVisible="False" x:Name="overlay"/>
    </AbsoluteLayout>
</ContentPage.Content>

我希望stacklayout在横向上获取屏幕宽度;但只有肖像就是工作。

1 个答案:

答案 0 :(得分:3)

如果控件是AbsoluteLayout设置的HorizontalOptions设置VerticalOptionsAbsoluteLayout.LayoutBounds的直接子项,则无效。所有尺寸必须来自设置AbsoluteLayout.LayoutFlagsStackLayout。因此,请尝试将<AbsoluteLayout BackgroundColor="Silver" HorizontalOptions="FillAndExpand"> <StackLayout Orientation="Vertical" BackgroundColor="Maroon" AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All"> ... 更改为:

ListView.HorizontalOptions

然后,您可能还需要将FillAndExpand设置为JComponent,但不要先尝试。