Xamarin 中与分辨率相关的字体大小 [英] Resolution-dependent font size in Xamarin

查看:40
本文介绍了Xamarin 中与分辨率相关的字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Xamarin 的新手,正在为我的第一个应用程序苦苦挣扎.我有一个图像,上面有一个入口视图.这样我就创建了一个自定义条目背景.

I am new with Xamarin and struggling with my first application. I have an image with an entry view above it. This way I create a custom entry background.

尽管具有不同分辨率的不同设备,条目的字体大小不会缩放.当我调整设备 A 的字体大小,现在检查设备 B 上的同一个应用程序时,字体大小太大了.

The font size of the entry won't scale with different devices with different resolutions though. When I adjust the font size for device A and now check the same app on device B the font size there is way too big.

我也尝试在 Android 代码中动态设置字体大小,如下所述:http://developer.xamarin.com/recipes/android/resources/device_specific/detect_screen_size/

I as well tried to set the font size dynamically in the Android code as described here: http://developer.xamarin.com/recipes/android/resources/device_specific/detect_screen_size/

但这仍然没有给出我想要的结果.相对布局中的百分比定义也没有.

But this still doesn't give the result I want. Neither does a percent definition as in RelativeLayout.

我只希望我的字体大小相对于屏幕大小保持不变,以便条目始终在背景图像边框内.

I just want my font size to stay the same relative to the screen size so the entry is always within the background image borders.

我希望有人能帮我解决这个问题或给我一个解决方法的提示.

I hope anybody can help me with this or give me a hint for a workaround.

推荐答案

我假设你使用的是 Xamarin.Android 而不是 Xamarin Forms - 你不需要动态更改代码中的字体大小.而是在样式或控件本身中指定 fontSize 并使用sp"而不是 px 或 dp(例如 android:fontSize='16sp'.

I assume you are using Xamarin.Android rather than Xamarin Forms - you should not need to dyamically change the font size in code. Instead specify the fontSize in either a style or in the control itself and use 'sp' rather than px or dp (For example android:fontSize='16sp'.

使用sp"(缩放像素)将根据用户的设备 dpi 适当缩放(您可以看到更深入的解释 此处.

Using 'sp' (scaled pixels) will scale appropriately depending on the user's device dpi (you can see a more indepth explaination here.

这篇关于Xamarin 中与分辨率相关的字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆