首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >字体大小在所有平台上相等

字体大小在所有平台上相等
EN

Stack Overflow用户
提问于 2017-07-12 09:18:16
回答 2查看 3.2K关注 0票数 2

我想要所有平台统一的字体大小,因为ios和android的默认字体大小对于微、小型、中型和large.For都有不同的值,例如ios中的defualt为17,而在android中为15 fontsize,而在ios中为17,而在android中为18。如何使其对xamarin.forms中的所有平台统一呢?

EN

回答 2

Stack Overflow用户

发布于 2017-07-12 10:03:28

这对我起了作用(Xamarin.Forms 2.3.3.175):

In app.xaml:

代码语言:javascript
复制
 <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:

代码语言:javascript
复制
<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/

票数 3
EN

Stack Overflow用户

发布于 2017-07-13 07:18:45

您必须根据给定的标签字体大小来计算屏幕的高度和宽度。

下面的代码将有助于在移动屏幕高度和宽度的基础上设计移动屏幕。

在此基础上设计,它将适用于所有移动分辨率。

PCL:

App.cs

代码语言:javascript
复制
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

#屏幕高度和宽度区域

代码语言:javascript
复制
    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

代码语言:javascript
复制
#region For Screen Height & Width

  App.screenWidth = (int)UIScreen.MainScreen.Bounds.Width;
      App.screenHeight = (int)UIScreen.MainScreen.Bounds.Height;

#端区

PCL

如果您使用MVVM模式,则必须在ScreeenHeight中获取这些ScreenWidth和ViewModel,然后给出视图和布局的高度和宽度。

代码语言:javascript
复制
         // The below two lines will use to get the MOBILE SCREEN HEIGHT && WIDTH in ViewModel
          int heightScreen=App.screenHeight;
          int widthScreen=App.screenHeigh

XAML设计:

UserLogin.xaml

代码语言:javascript
复制
   <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

代码语言:javascript
复制
  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#设计:

代码语言:javascript
复制
 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
                  }
          }
   }

这是另一种方式,但我认为这是不正确的方式

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45053522

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档