Android通用布局UniversalLayout

UniversalLayout 1.1

UniversalLayout使用过程各种不方便的改进,更新中。

UniversalLayout 的产生

读了鸿神几篇关于Android适配的的文章,如果既要使用PercentLayout又要使用AutoLayot,感觉使用起来还是特别麻烦,同时AutoLayout的设计方式使用上不方便,依懒Activity、只能使用一种设计尺寸、预览不方便,于是用自己的空闲时间写了这个库,发现Bug欢迎提出。新手写博客文采不好请见谅,废话不多说开始介绍。

UniversalLayout主要是基于Google percent库进行扩展的,代码的基础有大部分内容直接copy自鸿神的项目(Android 增强版百分比布局库 为了适配而扩展),自己重写太麻烦,修改了原库的一些Bug,同时加入了AutoLayout的支持,实现方式跟AutoLayout不同,AutoLayout使用实在是麻烦。简单的来说这是一个即支持PercenLayout又支持AutoLayout为了方便使用产生的库,偷偷告诉你还可以直接在布局里预览的哦不挑屏幕的。

UniversalLayout 的原理

关于UniversalLayout的原理暂时就不解析了,实在没把握讲明白以后有空再说(基本是不可能的),想了解的先阅读Android 百分比布局库(percent-support-lib) 解析与扩展Android 增强版百分比布局库 为了适配而扩展原理都是一样的,都是基于percent修改来的。

UniversalLayout 的使用之PercentLayout

针对百分比的使用很简单,跟Android 增强版百分比布局库 为了适配而扩展的方法基本一致,无非就是参数名的percent改成universal

<?xml version="1.0" encoding="utf-8"?>
<silicar.tutu.universal.UniversalLinearLayout
    android:orientation="vertical"
    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">

    <TextView
        style="@style/LayoutWrapWrap"
        android:textColor="@android:color/white"
        app:layout_marginTopUniversal="1%"
        android:background="#666"
        android:text="Width 100%sw  Height 8%sw TextSize 5%ws"
        app:layout_textSizeUniversal="5%s"
        app:layout_widthUniversal="100%s"
        app:layout_heightUniversal="8%sw"/>

    <TextView
        style="@style/LayoutWrapWrap"
        app:layout_marginTopUniversal="1%"
        android:background="#666"
        android:textColor="@android:color/white"
        android:text="Width 100%sh  Height 8%sh TextSize 5%sh"
        app:layout_textSizeUniversal="5%sh"
        app:layout_widthUniversal="100%sh"
        app:layout_heightUniversal="8%s"/>

    <silicar.tutu.universal.UniversalLinearLayout
        android:orientation="vertical"
        android:background="#aaa"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_marginUniversal="5%s"
        app:childStyle="@style/Design320x568">

        <TextView
            style="@style/LayoutWrapWrap"
            android:textColor="@android:color/white"
            app:layout_marginTopUniversal="1%"
            android:background="#666"
            android:text="Parent Margin 5%s TextSize 5%s"
            app:layout_textSizeUniversal="5%s"/>

        <TextView
            style="@style/LayoutWrapWrap"
            android:textColor="@android:color/white"
            app:layout_marginTopUniversal="1%"
            android:background="#666"
            android:text="Width 50%w  Height 15%w TextSize 5%w"
            app:layout_textSizeUniversal="5%"
            app:layout_widthUniversal="50%"
            app:layout_heightUniversal="15%w"/>

        <TextView
            style="@style/LayoutWrapWrap"
            android:textColor="@android:color/white"
            app:layout_marginTopUniversal="1%"
            android:background="#666"
            android:text="Width 50%sw  Height 15%sw TextSize 5%sw"
            app:layout_textSizeUniversal="5%s"
            app:layout_widthUniversal="50%s"
            app:layout_heightUniversal="15%sw"/>

        <TextView
            style="@style/LayoutWrapWrap"
            app:layout_marginTopUniversal="1%"
            android:background="#666"
            android:textColor="@android:color/white"
            android:text="Width 50%h  Height 15%h TextSize 5%h"
            app:layout_textSizeUniversal="5%h"
            app:layout_widthUniversal="50%h"
            app:layout_heightUniversal="15%"/>

        <TextView
            style="@style/LayoutWrapWrap"
            app:layout_marginTopUniversal="1%"
            android:background="#666"
            android:textColor="@android:color/white"
            android:text="Width 50%sh  Height 15%sh TextSize 5%sh"
            app:layout_textSizeUniversal="5%sh"
            app:layout_widthUniversal="50%sh"
            app:layout_heightUniversal="15%s"/>

    </silicar.tutu.universal.UniversalLinearLayout>

</silicar.tutu.universal.UniversalLinearLayout>

适配的时候建议宽度作为基础,Android的屏幕比不一定都是9:16,不同的手机会有不同的显示尤其是魅族这类奇葩机型

Nexus 4

Nexus 4预览效果(768x1280)

Galaxy Nexus

Galaxy Nexus 预览效果(720x1280)

可见不同比例的手机显示的效果不同

UniversalLayout 的使用之AutoLayout

这才是重点用法跟AutoLayout不同,使用简单方便多了。

  1. 定义一个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布局中加入我们的样式就行了,顺带介绍下工作原理了。很简单的就是把设计的尺寸加入到LayoutParams里,每个UniversalLayout的子控件都有一个自己的设计尺寸。那不是得每个都写尺寸信息之不累死人也会烦死人,所以才用到style,当然不需要每个写,也不需要每个都添加style,只要在继承UniversalLayout的父控件加个childStyle,子控件就会默认使用指定的style,如果子控件设置了Design会覆盖掉默认style,这样一来就算设计换了尺寸有多个尺寸,我们只要改下style就OK了是不是很方便,原理自行阅读源码。

    <silicar.tutu.universal.UniversalLinearLayout
        android:orientation="vertical"
        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">
        <TextView
            style="@style/LayoutWrapWrap"
            android:textColor="@android:color/white"
            app:layout_marginTopUniversal="5a"
            android:background="#666"
            android:text="Design Width 320  Height 568 TextSize 16aw Width 320a"
            app:layout_textSizeUniversal="16a"
            app:layout_widthUniversal="320a"/>
    
        <TextView
            style="@style/Design640x1136"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            app:layout_marginTopUniversal="5a"
            android:background="#666"
            android:text="Design Width 640  Height 1136 TextSize 16aw Width 320a"
            app:layout_textSizeUniversal="16a"
            app:layout_widthUniversal="320a"/>
    
        <TextView
            style="@style/LayoutWrapWrap"
            android:textColor="@android:color/white"
            app:layout_marginTopUniversal="5a"
            android:background="#666"
            android:text="Width 320aw  Height 25aw TextSize 16aw"
            app:layout_textSizeUniversal="16a"
            app:layout_widthUniversal="320a"
            app:layout_heightUniversal="25aw"/>
    
        <TextView
            style="@style/LayoutWrapWrap"
            app:layout_marginTopUniversal="5a"
            android:background="#666"
            android:textColor="@android:color/white"
            android:text="Width 320ah  Height 25ah TextSize 16ah"
            app:layout_textSizeUniversal="16ah"
            app:layout_widthUniversal="320ah"
            app:layout_heightUniversal="25a"/>
    
        <TextView
            style="@style/LayoutWrapWrap"
            android:textColor="@android:color/white"
            app:layout_marginTopUniversal="5a"
            android:background="#666"
            android:text="Width 160aw  Height 60aw TextSize 16aw"
            app:layout_textSizeUniversal="16a"
            app:layout_widthUniversal="160a"
            app:layout_heightUniversal="60aw"/>
    
        <TextView
            style="@style/LayoutWrapWrap"
            app:layout_marginTopUniversal="5a"
            android:background="#666"
            android:textColor="@android:color/white"
            android:text="Width 160ah  Height 60ah TextSize 16ah"
            app:layout_textSizeUniversal="16ah"
            app:layout_widthUniversal="160ah"
            app:layout_heightUniversal="60a"/>
    </silicar.tutu.universal.UniversalLinearLayout>
    
    AutoLayout

    Nexus 4预览效果(768x1280),同样建议以宽度为基准

  3. 在代码中使用UniversalLayout这有两种情况,一种父控件是UniversalLayout的,另一种是普通布局的,没错都能用。

    • 父控件是UniversalLayout

      UniversalLinearLayout.LayoutParams params = (UniversalLinearLayout.LayoutParams) codeView.getLayoutParams();
      UniversalLayoutInfo info = params.getUniversalLayoutInfo();
      info.widthUniversal.value = 0.8f;
      info.widthUniversal.basemode = UniversalLayoutInfo.BaseMode.PERCENT_WIDTH;
      info.heightUniversal.value = 50;
      codeView.requestLayout();
      
    • 普通布局,这功能后来添加的,把计算方法移到UniversalLayoutInfo里,不影响效率,懒得动手改UniversalLayoutHelp,哪位有空fork帮忙改不胜感谢

      UniversalLayoutInfo info2 = new UniversalLayoutInfo();
      info2.widthDesign = 640;
      info2.heightDesign = 1136;
      info2.widthUniversal = new UniversalLayoutInfo.UniversalVal(320, UniversalLayoutInfo.BaseMode.AUTO_WIDTH);
      info2.heightUniversal = new UniversalLayoutInfo.UniversalVal(100, UniversalLayoutInfo.BaseMode.AUTO_WIDTH);
      codeView2.getLayoutParams().width = info2.getUniversalSize(info2, info2.widthUniversal);
      codeView2.getLayoutParams().height = info2.getUniversalSize(info2, info2.heightUniversal);
      codeView2.requestLayout();
      

UniversalLayout 的导入

暂时没心思折腾bintray,先用JitPack代替也不麻烦

  1. 在项目根目录build.gradle添加repositories,注意是项目根目录的,不是项目的build.gradle
    repositories {
        //其他maven库...
        maven { url "https://jitpack.io" }
    }
    
    
  2. 在项目的build.gradle添加dependencies
    dependencies {
        compile 'com.github.brady9308:sic-universal-layout:1.0.1'
        //v7包引用冲突可以这样去掉
        //compile 'com.android.support:appcompat-v7:23.1.1'
        //compile ('com.github.brady9308:sic-universal-layout:1.0.1'){
        //  exclude module:'appcompat-v7'
        //}
    }
    
    

eclipse自己查看项目迁移,没心思折腾

GitHub地址:https://github.com/brady9308/sic-universal-layout

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

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

推荐阅读更多精彩内容