我想使用Grid
布局类型在手机屏幕的下角定义一个小方块。
我试图通过在代码隐藏中的OnAppearing
方法中获取列宽来实现它,并使用它来设置行高。
在Xaml我完成了这个:
<Grid x:Name ="iconGrid"
BackgroundColor="Green">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="9*" />
<ColumnDefinition Width="1.9*" />
<ColumnDefinition Width="0.1*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="5*" />
<RowDefinition Height="4.9*" />
<RowDefinition Height="0.1*" />
</Grid.RowDefinitions>
<Frame BackgroundColor="Purple"
Grid.Row ="1"
Grid.Column ="1" />
</Grid>
然后在OnAppearing中我完成了这个:
protected override async void OnAppearing()
{
base.OnAppearing();
iconGrid.RowDefinitions[1].Height = new GridLength(iconGrid.ColumnDefinitions[1].Width.Value);
iconGrid.RowDefinitions[2].Height = new GridLength(iconGrid.ColumnDefinitions[2].Width.Value);
var remainingHeight = iconGrid.Height - iconGrid.RowDefinitions[2].Height.Value - iconGrid.RowDefinitions[2].Height.Value;
iconGrid.RowDefinitions[0].Height = new GridLength(remainingHeight);
Debug.WriteLine("rows: " + iconGrid.RowDefinitions[0].Height + ", " + iconGrid.RowDefinitions[1].Height + ", " + iconGrid.RowDefinitions[2].Height);
}
我的调试输出是:
rows: 811.8.Absolute, 1.9.Absolute, 0.1.Absolute
因此,从调试输出看起来我只是得到列宽的星值并将它们字面上用作行的绝对值,这当然不起作用。
我想要的是获取列的屏幕绝对值,并将它们用作行的绝对值。
因此,简而言之,我需要获得定义为星型的列的实际屏幕点大小。这可能吗?看来它一定是。
想想我迷失了所有你想要的东西而迷失了自己......对不起,关于那个。
从显示器获取绝对值很好..复杂,但是仍然在预发布的路上有一个新的Nuget-package,称为Xamarin.Essentials,它将使您能够获得手机的宽度和高度并基于你的设计应该很容易找出你需要的尺寸。
要安装Nuget软件包Xamarin.Essential,请勾选复选框:Include prerelease。
添加到代码隐藏:
using Xamarin.Essentials;
您可以获取方向,旋转,密度,宽度和高度,并且最后不会随着旋转而改变 - 如果Genymotions Android模拟器可以被认为是。
var metrics = DeviceDisplay.ScreenMetrics;
// Orientation (Landscape, Portrait, Square, Unknown)
var orientation = metrics.Orientation;
// Rotation (0, 90, 180, 270)
var rotation = metrics.Rotation;
// Width (in pixels)
var width = metrics.Width;
// Height (in pixels)
var height = metrics.Height;
// Screen density
var density = metrics.Density;
据我所知,它可以为您提供页面的可用高度,即顶部任何菜单的高度减去。
有关它的更多信息:https://docs.microsoft.com/en-us/xamarin/essentials/?context=xamarin/xamarin-forms
---原帖“相对布局”---
<RowDefinition
RelativeLayout.HeightConstraint="{ConstraintExpression
Type=RelativeToParent,
Property=Height,
Factor=0.1,
Constant=0}" />
<ColumnDefinition
RelativeLayout.WidthConstraint="{ConstraintExpression
Type=RelativeToParent,
Property=Heigth,
Factor=0.1,
Constant=0}"