【问题标题】:How to scale font and container's height in Xamarin.Forms如何在 Xamarin.Forms 中缩放字体和容器的高度
【发布时间】:2020-02-15 05:52:44
【问题描述】:

我在 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>

这是我拥有的两个设备和模拟器的屏幕截图:

Android 8.1

iPod touch 6

Android 8.1(模拟器)

【问题讨论】:

  • 您是否尝试过使用 PositionProportional 标志的 AbsoluteLayout 将按钮控件设置在底部而不是网格?此外,尝试新的“视觉”属性以获得跨平台和设备的更统一的结果。

标签: xamarin.forms xamarin.android xamarin.ios


【解决方案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/

【讨论】:

  • 我将在下周尝试您的解决方案。我正在使用 Xamarin.Essentials 获取 MainDisplayInfo。
  • 我需要专门为 ios 更改容器的高度。您的解决方案有效,但大屏幕上 10% 的屏幕太多了,但在 ipod 上太小了。
  • 我已经更新了代码,你设置设置为你想要的。
猜你喜欢
  • 2021-02-13
  • 2021-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多