Android 适配无难事 UniversalLayout

不建议使用

虽然该方法带来了开发上的便利,但违背了Google设计上的理念,多种屏幕使用同一种尺寸在平板上会显得字体和图标特别大,还是推荐老老实实的去多写几个布局来适配屏幕。

上一版本的介绍之前写过,的只是源码一片混乱,介绍也不清楚,并且实际应用遇到各种不便决定重写。本来打算是花一个星期的,后来各种原因拖到现在才继续完成,功能基本一致重构再完成也就一两天的事。确实要反思下自己的拖延症了。废话不多说先介绍如何改进和使用。
GitHub地址:https://github.com/brady9308/universal-layout

UniversalLayout 新版特性

添加了代码提示,再也不担心敲错参数名了;
优化参数解析,更加合理智能的参数解析,不再必须附带参数修饰了;
加入新测量方法,还有未完成的功能敬请期待;

UniversalLayout 的导入

  1. 在项目根目录build.gradle添加repositories,注意是项目根目录的,不是项目的build.gradle
    repositories {
        //其他maven库...
        maven { url "https://jitpack.io" }
    }
    
    
  2. 在项目的build.gradle添加dependencies
    dependencies {
        compile 'com.github.brady9308:universal-layout:1.1.0'
    }
    
    

版本历史

  • 1.1.0版本
    重构代码结构,拆分类文件
    添加百分比以自身为比例计算
    添加百分比以父控件比例是否加入padding值计算设置
    添加布局文件参数提示
    修改简化参数名称
    修改基础值默认为自适应、屏幕、宽度
    修改min、max的计算方式支持任意空间设置
    不兼容1.0.x版本,但两个版本可以共存
    
  • 1.0.x版本
    初始版本
    

UniversalLayout 的使用之自动适配

这是推荐的方法先进行介绍,布局会自动根据设计稿的尺寸和屏幕尺寸自动适配,再也不用考虑设计的单位要换算为多少dp和多少sp了。只需要你定义设计稿的尺寸,一切都由布局自动完成。

  1. 在主题存放设计的尺寸

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="layout_widthDesign">640px</item>
        <item name="layout_heightDesign">1136px</item>
    </style>
    

    或者自定义一个Style样式存放设计的尺寸,多个尺寸就定义多个

    <style name="Design320x568">
        <item name="layout_widthDesign">320px</item>
        <item name="layout_heightDesign">568px</item>
    </style>
    <style name="Design640x1136">
        <item name="layout_widthDesign">640px</item>
        <item name="layout_heightDesign">1136px</item>
    </style>
    <style name="LayoutWrapWrap">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
    </style>
    
  2. 针对主题定义的设计尺寸,最简单了不需要特殊属性设置

    <?xml version="1.0" encoding="utf-8"?>
    <silicar.tutu.universal.widget.UniversalLinearLayout
        android:orientation="vertical"
        android:gravity="center"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="fitXY"
            android:src="@mipmap/temp"
            app:layout_widthExt="300"
            app:layout_heightExt="200"/>
    
    </silicar.tutu.universal.widget.UniversalLinearLayout>
    
    640x1136预览效果
  3. 设计换人了,新的设计稿和原来的尺寸不一样怎么办,不急我们也跟着换尺寸,在父控件添加app:childStyle="@style/Design320x568"指定新的设计稿尺寸

    <?xml version="1.0" encoding="utf-8"?>
    <silicar.tutu.universal.widget.UniversalLinearLayout
        android:orientation="vertical"
        android:gravity="center"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:childStyle="@style/Design320x568">
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="fitXY"
            android:src="@mipmap/temp"
            app:layout_widthExt="300"
            app:layout_heightExt="200"/>
    
    </silicar.tutu.universal.widget.UniversalLinearLayout>
    

    需要说明的是,这里只改变子控件的设计稿尺寸,不传递的就是说子控件的子控件是不改变的,必须另外指定

    320x568预览效果
  4. 只修改某个控件的设计尺寸,这个适配方案就这么考虑的,每个子控件都有自己的设计稿尺寸,都能指定设计稿尺寸app:layout_widthDesign="480px" app:layout_heightDesign="800px"

    <?xml version="1.0" encoding="utf-8"?>
    <silicar.tutu.universal.widget.UniversalLinearLayout
        android:orientation="vertical"
        android:gravity="center"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:childStyle="@style/Design320x568">
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="fitXY"
            android:src="@mipmap/temp"
            app:layout_widthDesign="480px"
            app:layout_heightDesign="800px"
            app:layout_widthExt="300"
            app:layout_heightExt="200"/>
    
    </silicar.tutu.universal.widget.UniversalLinearLayout>
    

    说明下属性的优先级,子控件尺寸>父控件尺寸>主题尺寸

    480x800预览效果

UniversalLayout 的使用之百分比

UniversalLayout的就是基于Google官方的PercentLayout扩展重构的。因此百分比的支持是必须的,在原来的基础上又进行了扩展,使用更加方便了。

  1. 以屏幕宽度为参照的百分比,百分比的默认方法

    <?xml version="1.0" encoding="utf-8"?>
    <silicar.tutu.universal.widget.UniversalLinearLayout
        android:orientation="vertical"
        android:gravity="center"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="fitXY"
            android:src="@mipmap/temp"
            app:layout_widthExt="50%"
            app:layout_heightExt="30%"/>
    
    </silicar.tutu.universal.widget.UniversalLinearLayout>
    
    屏幕宽度百分比
  2. 以屏幕高度为参照的百分比

    <?xml version="1.0" encoding="utf-8"?>
    <silicar.tutu.universal.widget.UniversalLinearLayout
        android:orientation="vertical"
        android:gravity="center"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="fitXY"
            android:src="@mipmap/temp"
            app:layout_widthExt="50%h"
            app:layout_heightExt="30%h"/>
    
    </silicar.tutu.universal.widget.UniversalLinearLayout>
    
    屏幕高度百分比
  3. 以父控件宽度为参照的百分比(高度参考项目例子,不举例了)

    <?xml version="1.0" encoding="utf-8"?>
    <silicar.tutu.universal.widget.UniversalLinearLayout
        android:orientation="vertical"
        android:gravity="center"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <silicar.tutu.universal.widget.UniversalLinearLayout
            android:orientation="vertical"
            android:gravity="center"
            android:background="#ccc"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_marginExt="5%">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scaleType="fitXY"
                android:src="@mipmap/temp"
                app:layout_widthExt="50%p"
                app:layout_heightExt="30%p"/>
        </silicar.tutu.universal.widget.UniversalLinearLayout>
    
    </silicar.tutu.universal.widget.UniversalLinearLayout>
    
    父控件宽度百分比
  4. 以自身为参照的百分比

    <?xml version="1.0" encoding="utf-8"?>
    <silicar.tutu.universal.widget.UniversalLinearLayout
        android:orientation="vertical"
        android:gravity="center"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <silicar.tutu.universal.widget.UniversalLinearLayout
            android:orientation="vertical"
            android:gravity="center"
            android:background="#ccc"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_marginExt="5%">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scaleType="fitXY"
                android:src="@mipmap/temp"
                app:layout_widthExt="100%oh"
                app:layout_heightExt="30%ph"/>
        </silicar.tutu.universal.widget.UniversalLinearLayout>
    
    </silicar.tutu.universal.widget.UniversalLinearLayout>
    

    该功能还未完成,存在点小问题50%o,50%ow,50%oh,多数情况还是能使用的

    自身高度百分比

适配的时候建议宽度作为基础,默认也是以宽度为基础,Android的屏幕比不全都是9:16,不同的手机会有不同,Nexus 4(768x1280)和Galaxy Nexus(720x1280)都是4.7寸屏,但比例不一样。

UniversalLayout 动态设置

难免需要用到,比如在ListView中设置item的高度,在代码中使用UniversalLayout有两种情况。

  • 父控件是UniversalLayout并设置了相应属性
        UniversalLinearLayout.LayoutParams params = (UniversalLinearLayout.LayoutParams) codeView.getLayoutParams();
        UniversalLayoutInfo info = params.getUniversalLayoutInfo();
        info.width.value = 0.8f;
        //SampleModel model = (SampleModel) info.width.model;
        //model.setMode(BaseModel.modePercent).setObj(BaseModel.objScreen);
        info.width.model = new SampleModel(BaseModel.modePercent, BaseModel.objScreen, true);
        info.height.value = 50;
        codeView.requestLayout();
  • 普通布局、父控件是UniversalLayout未设置属性
        UniversalValue universalValue = new UniversalValue(300, new SampleModel().getDefaultDesign());
        codeView2.getLayoutParams().width = (int) UniversalDimens.getUniversalDimens(universalValue, UniversalLayoutHelper.getDisplay());
        codeView2.getLayoutParams().height = (int) UniversalDimens.getUniversalDimens(universalValue, UniversalLayoutHelper.getDisplay());
        codeView2.requestLayout();

本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。转载请保留作者及原文链接

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,524评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,869评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,813评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,210评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,085评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,117评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,533评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,219评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,487评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,582评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,362评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,218评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,589评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,899评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,176评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,503评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,707评论 2 335

推荐阅读更多精彩内容