Xamarin.forms 如何缩放字体和容器';s高度(以Xamarin.形式表示)
我无法正确缩放Xamarin.Forms中容器的高度和字体大小 1) 第一个问题是,我需要在我的一个容器中设置静态高度,但在不同的设备上它看起来不同。我不知道确切情况,但假设问题可能是由屏幕密度引起的。我应该以某种方式缩放容器的高度还是Xamarin.表单应该为我处理这个问题 2) 第二个问题是,我需要设置适当的跨平台字体。有没有办法让Xamarin.Forms为我处理它,或者需要我使用Device.RuntimePlatform属性来设置它?同样如这里所说:-命名字体大小是不可接受的解决方案。对于iPod,我需要使用micro,但在android上,文本几乎看不见 在emulator上,会发生许多奇怪的事情,顶部栏的比例太大,整个外观都被放大了。这是我的错还是Xamarin不是我想的那样通用的平台 以下是我的页面代码:Xamarin.forms 如何缩放字体和容器';s高度(以Xamarin.形式表示),xamarin.forms,xamarin.android,xamarin.ios,Xamarin.forms,Xamarin.android,Xamarin.ios,我无法正确缩放Xamarin.Forms中容器的高度和字体大小 1) 第一个问题是,我需要在我的一个容器中设置静态高度,但在不同的设备上它看起来不同。我不知道确切情况,但假设问题可能是由屏幕密度引起的。我应该以某种方式缩放容器的高度还是Xamarin.表单应该为我处理这个问题 2) 第二个问题是,我需要设置适当的跨平台字体。有没有办法让Xamarin.Forms为我处理它,或者需要我使用Device.RuntimePlatform属性来设置它?同样如这里所说:-命名字体大小是不可接受的解决方案。
<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>
以下是我拥有的两台设备和emulator的屏幕截图:
安卓8.1
iPodtouch 6
Android 8.1(仿真器)
解决方案:您可以将
HeightRequest
网格设置为屏幕高度的百分比(例如10%)
在Android MainActivity.cs中
在iOS中
在代码隐藏或视图模型中
对于Xamarin.Forms中的自定义字体,您可以检查我将在下周尝试您的解决方案。我正在使用Xamarin.Essentials获取MainDisplayInfo。我需要专门为ios更改容器的高度。您的解决方案可行,但大屏幕上10%的屏幕太多,但ipod上的屏幕太小。我已经更新了代码,您可以随意设置。您是否尝试过使用位置比例标志的绝对布局来设置底部的按钮控件,而不是网格?此外,请尝试新的“可视化”属性,以在不同平台和设备上获得更统一的结果。
<StackLayout
BackgroundColor="White"
HeightRequest="{Binding stackHeight}"
HorizontalOptions="FillAndExpand"
VerticalOptions="EndAndExpand">
public static double ScreenWidth;
public static double ScreenHeight;
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());
}
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
//...
App.ScreenWidth = (int)UIScreen.MainScreen.Bounds.Width;
App.ScreenHeight = (int)UIScreen.MainScreen.Bounds.Height;
//...
}
public double stackHeight { get; private set; }
//...
if(Device.RuntimePlatform=="iOS")
{
stackHeight= App.ScreenHeight/5.0;
}
else
{
stackHeight= App.ScreenHeight/20.0;
}