xamarin.forms模拟rem动态大小值,实现屏幕适配
开发app的时候,比较麻烦的地方,就是处理屏幕适配,比如文字设为12的大小,测试的时候,看得文字挺正常,可是,放到高分辨率设备一看,文字就变得特别小,
怎样实现随着分辨率变大或者变小,所有的size数值,也会等比例变化呢?
首先,在App类,加两个static变量,用来获取屏幕大小
public partial class App : Application
{
public App ()
{
InitializeComponent(); MainPage = new App1.MainPage();
}
public static double ScreenWidth;
public static double ScreenHeight;
然后在android、ios等工程,设置这两个值
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate (Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar; base.OnCreate (bundle);
Resources.DisplayMetrics.ScaledDensity = 1;//告诉android不要把自己大小单位缩放
Resources.DisplayMetrics.Density = 1;
App1.App.ScreenWidth = Resources.DisplayMetrics.WidthPixels;
App1.App.ScreenHeight = Resources.DisplayMetrics.HeightPixels;
//ios
//App.ScreenWidth = UIScreen.MainScreen.Bounds.Width/UIScreen.MainScreen.Scale;
//App.ScreenHeight = UIScreen.MainScreen.Bounds.Height/UIScreen.MainScreen.Scale;
//UWP
//App.ScreenWidth = ApplicationView.GetForCurrentView().VisibleBounds.Width
//上面的公式ApplicationView.GetForCurrentView().VisibleBounds.Width其实不是实际的大小,而是 "真实Width/scale" 得出来的width
//var scale = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel
global::Xamarin.Forms.Forms.Init (this, bundle);
LoadApplication (new App1.App ());
}
}
然后,建一个Helper类
using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms; namespace App1
{
public class Helper
{
public class PIXEL : Dictionary<string, double>
{ public new double this[string key]
{
get
{
return Convert.ToDouble(key) * Helper.flag;
}
set
{ }
}
}
public class MARGIN : Dictionary<string, Thickness>
{
public new Thickness this[string key]
{
get
{
string[] p = key.Split('-');
if (p.Length == 1)
return new Thickness(Convert.ToDouble(p[0]) * Helper.flag);
return new Thickness(Convert.ToDouble(p[0]) * Helper.flag,
Convert.ToDouble(p[1]) * Helper.flag,
Convert.ToDouble(p[2]) * Helper.flag,
Convert.ToDouble(p[3]) * Helper.flag);
}
set
{ }
}
} public static double flag;
public PIXEL px
{
get;
}
public MARGIN m
{
get;
}
public Helper()
{
//计算出屏幕缩放比例,640是你的UI原始设计图的高度
flag = App.ScreenWidth / 640.0;
px = new PIXEL();
m = new MARGIN();
}
} }
在App.xaml里面定义资源
<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:App1"
x:Class="App1.App">
<Application.Resources>
<ResourceDictionary>
<local:Helper x:Key="size"></local:Helper>
</ResourceDictionary>
<!-- Application resource dictionary --> </Application.Resources>
</Application>
然后,实际的窗口页面,就可以这样设置值了
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:App1"
x:Class="App1.MainPage">
<Label Text="Welcome to Xamarin Forms!"
VerticalOptions="Start" BackgroundColor="AliceBlue" Margin="{Binding Source={StaticResource size},Path=m[10-0-0-0]}"
HorizontalOptions="Start" FontSize="{Binding Source={StaticResource size},Path=px[10]}" /> </ContentPage>
这样,就可以实现屏幕适配,而且做界面很方便,所有大小,只要照着UI设计图,用photoshop量出来是多少像素,直接就在xaml里面填多少像素就可以
xamarin.forms模拟rem动态大小值,实现屏幕适配的更多相关文章
- Android重写getResources规避用户调整系统字体大小影响Android屏幕适配
Android屏幕适配一直是一个头疼的问题.除此之外还要考虑APP在实际应用场景中,用户千奇百怪的设置,最常见的用户设置行为就是设置手机的字体大小,比如把字体设置成超大或者超小,这对屏幕适配又带来额外 ...
- Xamarin.Forms快速入门-深入探讨
官网链接 项目介绍 以Notes项目为例,The Notes application consists of one solution containing four projects, as sho ...
- Xamarin.Forms 3.0的新特性
近期因为工作关系开始使用Xamarin,翻译了两篇国外的介绍3.0新特性的文章,供大家参考. 第一篇文章来自Xamarin官网,原文地址:https://blog.xamarin.com/xamari ...
- Xamarin.Forms——尺寸大小(五 Dealing with sizes)
如之前所见的大量可视化元素均有自己的尺寸大小: iOS的状态栏高度为20,所以我们需要调整iOS的页面的Padding值,留出这个高度. BoxView设置它的默认宽度和高度为40. Frame的默认 ...
- Xamarin.Forms获取设备屏幕大小
Xamarin.Forms获取设备屏幕大小 可以借助device.Display获取.基本形式如下: var display = device.Display;然后就可以获取屏幕大小.display. ...
- Xamarin.Forms之XAML
官网参考 XAML基础知识 XAML(eXtensible Application Markup Language)可扩展应用程序标记语言,允许开发者在Xamarin.Forms应用中采用标记而不是代 ...
- xamarin.forms之使用CarouselView插件模仿网易新闻导航
在APP中基本都能见到类似网易.今日头条等上边横向导航条,下边是左右滑动的页面,之前做iOS的时候模仿实现过,https://github.com/ywcui/ViewPagerndicator,在做 ...
- 搞懂Xamarin.Forms布局,看这篇应该就够了吧
Xamarin.Forms 布局介绍 什么是布局?可以简单的理解为,我们通过将布局元素有效的组织起来,让屏幕变成我们想要的样子! 我们通过画图的方式来描述一下Xamarin.Forms的布局. 小节锚 ...
- Xamarin Forms:小马过河,王者归来
因为我媳妇的原因,去年下半年从零开始学习Android原生开发,做了一个答题库app.整体给我的感觉是入门难度不大,前期折腾一番,大部分时间都是花在开发上面,其实任何一门语言都是如此. 今年我又有另一 ...
随机推荐
- OpenFlashChart的图片导出
http://www.cnblogs.com/mcmurphy/archive/2012/10/15/2724894.html http://teethgrinder.co.uk/open-flash ...
- 安卓下载文件怎样更新UI进度
曾经写过几篇关于下载的文章.总的来说是下面几点: 1.维护一个下载进程的Hashmap,key:使用Md5进行处理后的文件下载地址,value为下载的Task. 以防止下载反复.并将信息保存至数据库. ...
- 2015 Multi-University Training Contest 2 1004 Delicious Apples(DP)
pid=5303">题目链接 题意:长度为l 的环,有n棵果树,背包容量为k,告诉你k棵苹果树的id.以及每棵树上结的果子数.背包一旦装满要返回起点(id==0) 清空,问你至少走多少 ...
- Python3基础(九) 错误和异常
本文主要介绍Python中的错误和异常,涉及到简单的异常处理.抛出异常以及清理动作.至于自定义异常类,将在介绍类与继承的时候讲到. 一.定义 常见的两种错误:语法错误 和 异常. 1.语法错误(Syn ...
- 【Cocos2dx游戏开发】CCNotificationCenter传递消息和数据
在开发游戏的时候我们经常需要在层与层之间.场景与场景之间传递数据和消息,Cocos2dx框架应用观察者模式为我们封装了一个CCNotificationCenter类,也叫消息通知中心,它也是一个单例类 ...
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
- 为部门整理的mysql_db使用军规
mysql_db使用军规: 1.禁止开发測试人员在IDC环境手工删除和改动数据 2.全部需求通过DB工具系统提交 3.禁止在IDC环境DB进行測试 4.IDC环境提交的sql语句一定要经过非正式环境验 ...
- NYOJ 330 一个简单的数学题【数学题】
/* 题目大意:求解1/n; 解题思路:写一个输出小数的算法 关键点:怎样处理小数点循环输出 解题人:lingnichong 解题时间:2014-10-18 09:04:22 解题体会:输出小数的算法 ...
- shell 切割文件
[root@hadoop2 xiaole_chk_url]# cat looh.index.splitfile.sh loop_c=0loop_step=10001loop_tag=0str_head ...
- 【POJ 1958】 Strange Towers of Hanoi
[题目链接] http://poj.org/problem?id=1958 [算法] 先考虑三个塔的情况,g[i]表示在三塔情况下的移动步数,则g[i] = g[i-1] * 2 + 1 再考虑四个塔 ...