首页 文章

如何使我的滚动视图/网格内的图像与正常大小的屏幕宽度相符?

提问于
浏览
0

我正在使用scrollview / grid!在我的网格中,我有一个图像,按钮和标签 .

在此页面中,我从数据库中收集了我的图像,这意味着图像会根据您事先单击的类别而有所不同 . 这意味着图像的高度/宽度并不总是相同 .

有些看起来不错(它们以正常高度填满整个屏幕)但有些根本没有填满屏幕 . 如果我做了一些 AspectFill 一些图片被砍掉了,我也不想要 . 我也尝试了 Fill ,但这延伸了画面 .

这就是我正在使用的:

<ScrollView HorizontalOptions = "FillAndExpand" VerticalOptions = "FillAndExpand" >
    <StackLayout HorizontalOptions = "FillAndExpand" VerticalOptions = "FillAndExpand">

    <Grid>

        <Grid.RowDefinitions>

            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>

        </Grid.RowDefinitions>

        <StackLayout Grid.Row="0" BackgroundColor = "Red" HorizontalOptions = "FillAndExpand" VerticalOptions = "FillAndExpand">

            <Image  x:Name="theimage" Aspect = "AspectFit"  HorizontalOptions = "FillAndExpand" VerticalOptions = "FillAndExpand" />

            <Button Clicked="clickFunc" TextColor="White" Grid.Row="1" HeightRequest="80" WidthRequest="120" BorderRadius="40" HorizontalOptions="End" VerticalOptions="Start" />

        <StackLayout Grid.Row="1" Padding="10,20,10,0">

            <Label TextColor = "#474747" x:Name="title" Text="" HorizontalOptions="Start" FontFamily="Helvetica Neue" FontAttributes="Bold" />

        </StackLayout>
        </StackLayout>
    </Grid> 
    </StackLayout>
    </ScrollView>

我试图这样做来解决这个问题:

theimage.WidthRequest = App.ScreenWidth;

我从AppDelegete获取ScreenWidth但 theimage 根本没有改变它的宽度 . 如果我尝试在xaml中手动添加一个数字:

WidthRequest = "500"

图像也没有改变,所以当涉及到图像时,我有点想法 .

那么我如何调整我的代码,以便我的图像始终完全布局到屏幕宽度,同时保持正常大小?

1 回答

  • 0

    好吧,是的,您还必须分配Grid.Row和Grid.Column以找到网格的确切位置 . 我已经改变了你的代码:(我改变的值仅用于展示)

    <Grid.RowDefinitions>
    
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="100"/>
    
        </Grid.RowDefinitions>
    
        <StackLayout Grid.Row="0" BackgroundColor = "Red" HorizontalOptions = "FillAndExpand" VerticalOptions = "FillAndExpand">
    
            <Image Grid.Row="1"  x:Name="theimage" Aspect = "AspectFit"  HorizontalOptions = "FillAndExpand" VerticalOptions = "FillAndExpand" />
    
            <Button Clicked="clickFunc" TextColor="White" Grid.Row="1" HeightRequest="80" WidthRequest="120" BorderRadius="40" HorizontalOptions="End" VerticalOptions="Start" />
    
        <StackLayout Grid.Row="1" Padding="10,20,10,0">
    
            <Label TextColor = "#474747" x:Name="title" Text="" HorizontalOptions="Start" FontFamily="Helvetica Neue" FontAttributes="Bold" />
    
        </StackLayout>
        </StackLayout>
    </Grid>
    

    请参阅,XAML docs您如何使用网格 .

    http://www.dailyfreecode.com/code/grid-layout-xaml-212.aspx http://www.c-sharpcorner.com/uploadfile/mahesh/grid-in-wpf/

    Note :对于每行必须提供行定义,同样适用于列定义 .

相关问题