我想要所有平台统一的字体大小,因为ios和android的默认字体大小对于微、小型、中型和large.For都有不同的值,例如ios中的defualt为17,而在android中为15 fontsize,而在ios中为17,而在android中为18。如何使其对xamarin.forms中的所有平台统一呢?
发布于 2017-07-12 10:03:28
这对我起了作用(Xamarin.Forms 2.3.3.175):
In app.xaml:
<ResourceDictionary>
<OnPlatform x:Key="SmallTextSize" x:TypeArguments="x:Double"
iOS="12.0" Android="12.0" WinPhone="14.0" />
<Style TargetType="MyCustomLabel">
<Setter Property="TextColor" Value="#3399FF" />
<Setter Property="FontSize" Value="{DynamicResource SmallTextSize}"/>
</Style>
</ResourceDictionary>In view.xaml:
<StackLayout>
<c:MyCustomLabel Text="{Binding TextField1}"/>
<c:MyCustomLabel Text="{Binding TextField2}"
FontAttributes="Bold"/>
</StackLayout>第一个"MyCustomLabel“将从样式中被指定为FontSize=SmallTextSize和TextColor,第二个"MyCustomLabel”将被分配为FontAttributes=Bold,但它将保留由样式分配的FontSize=SmallTextSize。
如果您正在使用上面建议的x:TypeArguments=“字体”,覆盖FontAttributes也会重置在样式中分配的FontSize。
https://jfarrell.net/2015/02/07/platform-specific-styling-with-xamarin-forms/
发布于 2017-07-13 07:18:45
您必须根据给定的标签字体大小来计算屏幕的高度和宽度。
下面的代码将有助于在移动屏幕高度和宽度的基础上设计移动屏幕。
在此基础上设计,它将适用于所有移动分辨率。
PCL:
App.cs
public class App : Application
{
public static int screenHeight, screenWidth;
public App()
{
MainPage = new UserLogin();
//The root page of your application
}
protected override void OnStart()
{
// Handle when your app starts
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}
}Xamarin.Android:
MainActivity.cs
#屏幕高度和宽度区域
var pixels = Resources.DisplayMetrics.WidthPixels;
var scale = Resources.DisplayMetrics.Density;
var dps = (double)((pixels - 0.5f) / scale);
var ScreenWidth = (int)dps;
App.screenWidth = ScreenWidth;
//RequestedOrientation = ScreenOrientation.Portrait;
pixels = Resources.DisplayMetrics.HeightPixels;
dps = (double)((pixels - 0.5f) / scale);
var ScreenHeight = (int)dps;
App.screenHeight = ScreenHeight;端区
Xamarin.iOS
AppDelegate.cs
#region For Screen Height & Width
App.screenWidth = (int)UIScreen.MainScreen.Bounds.Width;
App.screenHeight = (int)UIScreen.MainScreen.Bounds.Height;#端区
PCL
如果您使用MVVM模式,则必须在ScreeenHeight中获取这些ScreenWidth和ViewModel,然后给出视图和布局的高度和宽度。
// The below two lines will use to get the MOBILE SCREEN HEIGHT && WIDTH in ViewModel
int heightScreen=App.screenHeight;
int widthScreen=App.screenHeighXAML设计:
UserLogin.xaml
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:local="clr-
SreenSizeDemo;assembly=SreenSizeDemo" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="SreenSizeDemo.UserLogin">
<StackLayout>
<Label x:Name="lblTitle" HorizontalOptions="FillAndExpand"/>
<Image x:Name="imgProfile" Source="Logo.png" />
<StackLayout>
<ContentPage>UserLogin.xaml.cs
public partial class UserLogin : ContentPage
{
// Here you get the MOBILE SCREEN HEIGHT && WIDTH
int heightScreen=App.screenHeight;
int widthScreen=App.screenHeight;
public UserLogin()
{
lblTitle.FontSize=height=Screen/36.8;
//The above value is equal to fontsize =20
imgProfile.HeightRequest=heightScreeen/10;
imgProfile. WidthRequest=heightScreeen/10;
}
}C#设计:
public class UserLogin: ContentPage
{
// Here you get the MOBILE SCREEN HEIGHT && WIDTH
int heightScreen=App.screenHeight;
int widthScreen=App.screenHeight;
public UserLogin()
{
Label lab = new Label()
{
FontSize = heightScreen/ 36.8
//the above value is equal to fontsize =20
};
Image imgProfile=new Image()
{
Source="Logo.png",
HeightRequest=heightScreeen/10,
WidthRequest=heightScreeen/10
}
}
}这是另一种方式,但我认为这是不正确的方式
https://stackoverflow.com/questions/45053522
复制相似问题