Xamarin.forms Xamarin.形状和尺寸
有人能告诉我为什么它会这样做吗?如果你知道怎么做,请告诉我如何定义控件来水平或垂直填充它们的父控件 此代码中的页面绘制了下面的屏幕截图。我完全不明白发生了什么事。我解释说,行/列定义中的*将强制内容与包含控件的内容一样宽,但事实并非如此。事实上,如果我将内部网格中的rowdefinitions宽度增加到10000*它将与容器一样宽,但只是*使其仅宽1位。完全不是直觉 外部网格符合我的预期。它只有1/5宽,占高度的1/2。这就是我所期望的*,4*列定义和*,*行定义 我真的怀疑这里有一只虫子,如果是的话,那就是一只令人讨厌的虫子 编辑: 这一比例与预期一致:Xamarin.forms Xamarin.形状和尺寸,xamarin.forms,Xamarin.forms,有人能告诉我为什么它会这样做吗?如果你知道怎么做,请告诉我如何定义控件来水平或垂直填充它们的父控件 此代码中的页面绘制了下面的屏幕截图。我完全不明白发生了什么事。我解释说,行/列定义中的*将强制内容与包含控件的内容一样宽,但事实并非如此。事实上,如果我将内部网格中的rowdefinitions宽度增加到10000*它将与容器一样宽,但只是*使其仅宽1位。完全不是直觉 外部网格符合我的预期。它只有1/5宽,占高度的1/2。这就是我所期望的*,4*列定义和*,*行定义 我真的怀疑这里有一只虫子,如果
<?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:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:tgb="clr-namespace:TGB.Xamarin.Forms.Controls;assembly=TGB.Xamarin.Forms"
mc:Ignorable="d"
x:Class="TGB.Xamarin.Forms.TestApp.MainPage">
<StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="4*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ContentView BackgroundColor="Green" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ContentView BackgroundColor="Orange" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
</ContentView>
</Grid>
</ContentView>
</Grid>
</StackLayout>
</ContentPage>
这是因为您使用了AbsoluteLayout。因此有必要在其子元素中设置AbsoluteLayout.LayoutBounds和AbsoluteLayout.LayoutFlags 此外,如果要手动设置ContentView的高度,请将行的高度设置为“自动”,在该选项中,它将适合其子元素的大小 因此,您可以像下面这样改进代码
<StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="4*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<AbsoluteLayout BackgroundColor="Green" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<Grid AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ContentView HeightRequest="100" BackgroundColor="Orange" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
</ContentView>
</Grid>
</AbsoluteLayout>
</Grid>
</StackLayout>
你想达到什么效果?你可以提供一个屏幕截图。我正在创建一个自定义滑块。默认滑块仅具有水平方向。我需要一个垂直的。所以我想我将采用绝对布局,并在其中放置一个contentview,我可以在其中滑动。当然,它必须根据容器自动调整大小。我使用Xamarin.Forms已经有一段时间了,但无法直观地解释它应该如何工作。是否希望橙色ContentView以水平方式填充绝对布局?不,我希望橙色与外部容器一样宽,并且具有固定高度,比如说HeighRequest=150
<StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="4*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<AbsoluteLayout BackgroundColor="Green" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<Grid AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ContentView HeightRequest="100" BackgroundColor="Orange" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
</ContentView>
</Grid>
</AbsoluteLayout>
</Grid>
</StackLayout>