xaml - Xamarin Forms stacklayout 在横向上填充屏幕

标签 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 在横向上采用屏幕宽度;但只有在肖像上是工作。

最佳答案

当控件是 AbsoluteLayout 的直接子级时设置 HorizontalOptionsVerticalOptions没有效果。所有尺寸都必须来自设置 AbsoluteLayout.LayoutBoundsAbsoluteLayout.LayoutFlags .所以试着改变你的 StackLayout对此:

<AbsoluteLayout BackgroundColor="Silver"
                HorizontalOptions="FillAndExpand">
  <StackLayout Orientation="Vertical" 
               BackgroundColor="Maroon"
               AbsoluteLayout.LayoutBounds="0,0,1,1"
               AbsoluteLayout.LayoutFlags="All">
    ...

那么您可能还需要设置您的 ListView.HorizontalOptionsFillAndExpand但先试试没有那个。

关于xaml - Xamarin Forms stacklayout 在横向上填充屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38726836/

相关文章:

xamarin.forms - 从 Firebase 通知恢复应用程序不起作用(Xamarin Forms)

c# - 如何使用 Xamarin.Forms.iOS 更改呈现器 View 中的 UIPickerView 文本颜色?

android - Lottie.Forms 中不存在类型或命名空间 'Droid'

c# - 在 WPF/MVVM 中动态添加用户控件

wpf - 在WrapPanel上垂直列出项目并利用多列

wpf - WPF xaml 文件开头的 xmlns=[some url] 属性有什么用?

c# - 将 View 模型创建为 DataContext 与创建为 StaticResource 的值(value)是什么

c# - 向自定义控件添加依赖属性导致样式出现奇怪的错误

c# - 在 Xamarin Forms 中应该使用哪个包而不是 Android.Content 来获取类 Intent?

Scrollview 滚动时向上滚动其他内容