xamarin.forms - 如何在 Xamarin.Forms 中缩放字体和容器的高度
问题描述
我在 Xamarin.Forms 中正确缩放容器高度和字体大小时遇到问题。
1)第一个问题是我需要在我的一个容器中设置静态高度,但它在不同的设备上看起来不同。我不确切知道,但假设这个问题可能是由屏幕密度引起的。我应该以某种方式缩放容器的高度还是 Xamarin.Forms 应该为我处理这个?
2)第二个问题是我需要设置正确的跨平台字体。有没有办法让 Xamarin.Forms 为我处理它,或者我需要使用 Device.RuntimePlatform 属性来设置它?也如此处所述:https ://docs.microsoft.com/pl-pl/xamarin/xamarin-forms/user-interface/text/fonts - 命名字体大小不是可接受的解决方案。对于 iPod,我需要使用 micro,但在 android 上,文本几乎看不到。
在模拟器上发生了许多奇怪的事情,顶部栏超出了比例,整个外观都被放大了。这是我的错还是 Xamarin 不是我认为的通用平台?
这是我页面的代码:
<StackLayout>
<SearchBar
VerticalOptions="Start"
TextChanged="SearchBar_OnTextChanged"
PlaceholderColor="#C0C0C0"></SearchBar>
<ListView
BackgroundColor="#f2f2f2"
VerticalOptions="Center"
ItemTapped="SelectProcedure"
CachingStrategy="RecycleElement">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell
Text="{Binding Name}"
TextColor="Black"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<StackLayout
BackgroundColor="White"
HeightRequest="80"
HorizontalOptions="FillAndExpand"
VerticalOptions="EndAndExpand">
<Grid
Padding="10,5,10,5"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label
Grid.Column="0"
Grid.Row="0"
VerticalTextAlignment="Center"
VerticalOptions="Center"
HorizontalTextAlignment="Center"
HorizontalOptions="Center"
TextColor="Black"/>
<telerikInput:RadButton
Grid.Column="1"
Grid.Row="0"
Padding="5,0,5,0"
ImageSource="plus.png"/>
</Grid>
</StackLayout>
</StackLayout>
这是我拥有的两个设备和模拟器的屏幕截图:
安卓8.1
iPod 触摸 6
安卓 8.1(模拟器)
解决方案
解决方法:您可以将HeightRequest
网格设置为屏幕高度的百分比(例如 10%)。
<StackLayout
BackgroundColor="White"
HeightRequest="{Binding stackHeight}"
HorizontalOptions="FillAndExpand"
VerticalOptions="EndAndExpand">
在共享项目 App.xaml.cs
public static double ScreenWidth;
public static double ScreenHeight;
在 Android MainActivity.cs
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.SetFlags("Shell_Experimental", "Visual_Experimental", "CollectionView_Experimental");
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
App.ScreenWidth = Resources.DisplayMetrics.WidthPixels/Resources.DisplayMetrics.Density;
App.ScreenHeight =Resources.DisplayMetrics.HeightPixels/Resources.DisplayMetrics.Density;
LoadApplication(new App());
}
在 iOS 中
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
//...
App.ScreenWidth = (int)UIScreen.MainScreen.Bounds.Width;
App.ScreenHeight = (int)UIScreen.MainScreen.Bounds.Height;
//...
}
在代码后面或 ViewModel 中
public double stackHeight { get; private set; }
//...
if(Device.RuntimePlatform=="iOS")
{
stackHeight= App.ScreenHeight/5.0;
}
else
{
stackHeight= App.ScreenHeight/20.0;
}
对于 Xamarin.Forms 中的自定义字体,您可以查看https://xamarinhelp.com/custom-fonts-xamarin-forms/
推荐阅读
- git - 如何限制远程 git repo 中的交互?
- haskell - 为什么 Haskell 的括号函数在可执行文件中工作但在测试中无法清理?
- c++ - 如何将不同多个数组中的值传递给新的单个数组?
- javascript - 如何从 Promise 解析中获取价值?
- php - 如何在 laravel 验证器中验证时间“10:30 AM”
- regex - 如何在 shell 脚本中使用 sed 将所有出现的环境值替换为其当前值
- php - 在 PHP 中使用 Extends 从另一个类调用函数
- asp.net - asp.net 将一个空的 C# 列表放入剃须刀视图并将其填充到里面
- api - .是否可以在 JFrog 中使用 CUrl 命令创建本地存储库?
- yii - 使用上传的文件更新 yii 表单记录,如果我不再次附加文件,则会引发错误