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.整体给我的感觉是入门难度不大,前期折腾一番,大部分时间都是花在开发上面,其实任何一门语言都是如此. 今年我又有另一 ...
随机推荐
- win7下安装SQLSERVER2000
来自为知笔记(Wiz)
- [JavaEE] JTA, Java Transaction API, Repository for DB opreations
Mainly two things: 1. For all the creating and deleting opreations for the DB, we want to use 'REQUI ...
- 从IBM的计划中分析出中国重新相当然的错误选择吗
<IBM欲用物联网技术解决北京雾霾难题> http://security.zol.com.cn/469/4690141.html 读了上文.分析出的. 因为我没有太多的数据.不好分析. 有 ...
- web中servletcontext和applicationContext
参看:http://www.cnblogs.com/isisbenben/p/5594323.html servletContext和ApplicationContext有什么区别 servletCo ...
- 在C#中实现listbox的项上下移动(winform) 标准
在C#中实现listbox的项上下移动(winform) 收藏人:梅毛子360 2013-10-02 | 阅:1 转:2 | 分享 | 来源 usi ...
- CodeForces - 557D Vitaly and Cycle(二分图)
Vitaly and Cycle time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- openstack 杂记 备忘
- CSS样式适配杂记
1.问:input框的对齐,制作类似百度搜索框的时候,发现IE下前面输入框和后面按钮总是不能对齐. 解答:给input框增加vertical-align:bottom; 2.问:IE下display: ...
- Django day29 视图,路由控制,响应器
一:视图 1.视图的几种方式: (1) 第一种 from rest_framework.mixins import ListModelMixin, CreateModelMixin class Pub ...
- MyEclipse找不到install new software
Window->Preferences->Capabilities-> classic update(勾选即可) 勾选后会出现software updates,下面按照help-&g ...