我不能指定customView的高度吗? [英] I can not specify a Height of a customView?

查看:68
本文介绍了我不能指定customView的高度吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个CustomView,用于CollectionView的DataTemplate

I created a CustomView to use in CollectionView's DataTemplate

<Frame CornerRadius="10" IsClippedToBounds="True" Padding="0" HasShadow="False" >
        <AbsoluteLayout Padding="0" Margin="0">
            <Image Source="image1.png" AbsoluteLayout.LayoutBounds="0,0,1,1" Aspect="AspectFill" AbsoluteLayout.LayoutFlags="All"></Image>
            <Frame BackgroundColor="#fedb0a" AbsoluteLayout.LayoutBounds="0,0,0.426,0.1177" AbsoluteLayout.LayoutFlags="All" HasShadow="False">
                <StackLayout Orientation="Horizontal" AbsoluteLayout.LayoutBounds="0.682,0.29,AutoSize,0.05885">
                    <Label Text="Hi" FontSize="Medium" VerticalOptions="CenterAndExpand"></Label>
                </StackLayout>
            </Frame>
            <StackLayout AbsoluteLayout.LayoutBounds="0,1,1,0.2" AbsoluteLayout.LayoutFlags="All" BackgroundColor="#95000000" Orientation="Horizontal">
                <StackLayout VerticalOptions="CenterAndExpand" Margin="5,0,5,0">
                    <Frame CornerRadius="20" IsClippedToBounds="True" Padding="0">
                        <Image Source="icon1.png" Aspect="AspectFill" WidthRequest="40" HeightRequest="40"></Image>
                    </Frame>
                </StackLayout>
                <StackLayout VerticalOptions="CenterAndExpand">
                    <Label Text="hopeshow~" FontSize="Large" TextColor="#fedb0a"></Label>
                    <Label Text="2020 year" FontSize="Medium" TextColor="White"></Label>
                </StackLayout>
            </StackLayout>
        </AbsoluteLayout>
    </Frame>

我将其放在我的DataTemplate <vi:MyCustomView HeightRequest="40"></vi:MyCustomView>中,但是似乎HeightRequest没有排序. 该视图会自动放大一个大视图.

I placed it in my DataTemplate <vi:MyCustomView HeightRequest="40"></vi:MyCustomView> but it seems that the HeightRequest is not sequenced. the view autosized a large view.

当我设置一个值时,WidthRequest就可以了.

The WidthRequest is ok when I set a value.

如何处理?

推荐答案

StackLayout AbsoluteLayout 这样的布局不适合其子元素的大小.因此,如果要实现它,可以使用Grid代替它们.

The layout like StackLayout and AbsoluteLayout will not fit the size of its child elements . So if you want to implement it you can use Grid instead of them .

<Grid>
  <Grid.RowDefinitions>
    <RowDefinition Height="*" />
    <RowDefinition Height="*" />
  </Grid.RowDefinitions>
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="*" />
  </Grid.ColumnDefinitions>

 //set them in the same Row and Column and they will be in the same position .
  <Image Source="image1.png" Grid.Row="0" Grid.Column="0"></Image>
  <Frame BackgroundColor="#fedb0a" HasShadow="False"Grid.Row="0" Grid.Column="0">
   //...
  </Frame>

  //...other elements

</Grid>

有关布局的更多详细信息,可以检查 https://docs.microsoft.com/zh-CN/xamarin/xamarin-forms/user-interface/layouts/grid

For more details about layout you can check https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/layouts/grid

这篇关于我不能指定customView的高度吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆