开发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动态大小值,实现屏幕适配的更多相关文章

  1. Android重写getResources规避用户调整系统字体大小影响Android屏幕适配

    Android屏幕适配一直是一个头疼的问题.除此之外还要考虑APP在实际应用场景中,用户千奇百怪的设置,最常见的用户设置行为就是设置手机的字体大小,比如把字体设置成超大或者超小,这对屏幕适配又带来额外 ...

  2. Xamarin.Forms快速入门-深入探讨

    官网链接 项目介绍 以Notes项目为例,The Notes application consists of one solution containing four projects, as sho ...

  3. Xamarin.Forms 3.0的新特性

    近期因为工作关系开始使用Xamarin,翻译了两篇国外的介绍3.0新特性的文章,供大家参考. 第一篇文章来自Xamarin官网,原文地址:https://blog.xamarin.com/xamari ...

  4. Xamarin.Forms——尺寸大小(五 Dealing with sizes)

    如之前所见的大量可视化元素均有自己的尺寸大小: iOS的状态栏高度为20,所以我们需要调整iOS的页面的Padding值,留出这个高度. BoxView设置它的默认宽度和高度为40. Frame的默认 ...

  5. Xamarin.Forms获取设备屏幕大小

    Xamarin.Forms获取设备屏幕大小 可以借助device.Display获取.基本形式如下: var display = device.Display;然后就可以获取屏幕大小.display. ...

  6. Xamarin.Forms之XAML

    官网参考 XAML基础知识 XAML(eXtensible Application Markup Language)可扩展应用程序标记语言,允许开发者在Xamarin.Forms应用中采用标记而不是代 ...

  7. xamarin.forms之使用CarouselView插件模仿网易新闻导航

    在APP中基本都能见到类似网易.今日头条等上边横向导航条,下边是左右滑动的页面,之前做iOS的时候模仿实现过,https://github.com/ywcui/ViewPagerndicator,在做 ...

  8. 搞懂Xamarin.Forms布局,看这篇应该就够了吧

    Xamarin.Forms 布局介绍 什么是布局?可以简单的理解为,我们通过将布局元素有效的组织起来,让屏幕变成我们想要的样子! 我们通过画图的方式来描述一下Xamarin.Forms的布局. 小节锚 ...

  9. Xamarin Forms:小马过河,王者归来

    因为我媳妇的原因,去年下半年从零开始学习Android原生开发,做了一个答题库app.整体给我的感觉是入门难度不大,前期折腾一番,大部分时间都是花在开发上面,其实任何一门语言都是如此. 今年我又有另一 ...

随机推荐

  1. Android: 清除View跳转的历史记录

    Intent intent = new Intent(); intent.setClass(SetActivity.this, RegisterLoginActivity.class); intent ...

  2. 数据结构之中序遍历转兴许遍历(JAVA实现)(二)

    算法流程: 主要分为四步: 1.当前字符为数字或者字母,则直接输出 2.当前字符为).则在栈中匹配输出.一直匹配到),则停止输出(就是将)及其顶上的元素所有弹出来输出) 3.当前字符为操作符.则比較当 ...

  3. 前端project师必需知识点

    1.HTML 2,CSS 3,JavaScript 4.JQuery 5.JSON 6,HTML DOM 7.AJAX 进阶教程: 1.Bootsrap 2,sea.js 3,node.js 4.An ...

  4. Java设计模式菜鸟系列(一)策略模式建模与实现

    转载请注明出处:http://blog.csdn.net/lhy_ycu/article/details/39721563 今天開始咱们来谈谈Java设计模式. 这里会结合uml图形来解说,有对uml ...

  5. nyoj27水池数目 (DFS)

    题目27 题目信息 执行结果 本题排行 pid=27" style="text-decoration:none; color:rgb(55,119,188)">讨论 ...

  6. 负margin使用注意的一个问题

    在项目实力中经经常使用到负margin 如: <div id="test"> <ul> <li>子元素1</li> <li&g ...

  7. YTU 2760: 字符串---首字母变大写

    2760: 字符串---首字母变大写 时间限制: 1 Sec  内存限制: 128 MB 提交: 343  解决: 136 题目描述 输入一行英文句子,将每个单词的第一个字母改成大写字母. 输入 一个 ...

  8. common upload乱码

    request.setCheracterEncoding("utf-8"); DiskFileUpload.setHeaderEncoding("utf-8") ...

  9. [python基础]xml_rpc远程调控supervisor节点进程

    supervisor提供的两种管理方式,supervisorctl和web其实都是通过xml_rpc来实现的. xml_rpc其实就是本地可以去调用远端的函数方法,在python中只需要引入xmlrp ...

  10. C. Searching for Graph(cf)

    C. Searching for Graph time limit per test 1 second memory limit per test 256 megabytes input standa ...