安卓自动布局适配所有机型

一、概述

安卓机型千差万别,包括手机、平板以及其它开发板,有主流的16:9也有普通的4:3、2:1等,美工在ui设计上通常是给出一套设计图,如何简单快速的适配到所有机型通常都让开发者感到困扰,不同大小的设备要能做到等比例缩放,不同比例的设备在缩放时又要保证图像不变形又要有一定的特殊变换使其美观。
为了快速满足以上需求,我们定制了并开源了一套布局
源码地址:https://github.com/SuperDo-Magina/AndroidAutoLayout

二、效果

(注:以下为设置ui图为1080*1920效果)
下面让我们来看看不同的设置在不同机型上呈现的效果

布局一
<com.superdo.magina.autolayout.widget.AutoRelativeLayout
    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">

    <View
        app:auto_width="540"
        app:auto_height="960"
        android:id="@+id/v1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#F00"/>

    <View
        app:auto_width="540"
        app:auto_height="960"
        android:id="@+id/v2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FF0"
        android:layout_toEndOf="@id/v1"/>

    <View
        app:auto_width="540"
        app:auto_height="960"
        android:id="@+id/v3"
        android:layout_below="@id/v1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#F0F"/>

</com.superdo.magina.autolayout.widget.AutoRelativeLayout>
布局一.png
布局二
<com.superdo.magina.autolayout.widget.AutoRelativeLayout
    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">

    <View
        app:auto_width="540"
        app:auto_width_extra="0.5"
        app:auto_height="960"
        android:id="@+id/v1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#F00"/>

    <View
        app:auto_width="540"
        app:auto_height="960"
        app:auto_width_extra="0.5"
        android:id="@+id/v2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FF0"
        android:layout_toEndOf="@id/v1"/>

    <View
        app:auto_width="540"
        app:auto_height="960"
        app:auto_width_extra="0.5"
        android:id="@+id/v3"
        android:layout_below="@id/v1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#F0F"/>

</com.superdo.magina.autolayout.widget.AutoRelativeLayout>
布局二.png

从上诉两个布局效果不难看出我们的布局设置是从手机上切割出最大的16:9区域进行布局适配的,另外还可以根据需要,对特殊比例的机型进行调整布局样式大小比例等,我们可以简单的在xml中就完成对所有机型的适配。更多复杂操作请看以下api详细说明。

三、用法说明

1、集成框架

1、 添加 JitPack 仓库
在更目录下的 build.gradle 添加以下代码

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

2、添加依赖

dependencies {
    compile 'com.github.SuperDo-Magina:AndroidAutoLayout:1.x.x'
}

2、初始化

宽高值取决与美工给你的ui设计图(如720*1280即width=720,height=1280),在使用时控件的大小即可以直接设置为ui标注大小(特殊说明:height>width)

AutoLayout.init(this, width, height);

设置布局方向(可选,默认为竖向,可在设置不同方向布局前修改)


AutoLayout.setScreenOrientation(AutoLayout.ScreenOrientation.LANDSCAPE/AutoLayout.ScreenOrientation.PORTRAIT);

可用父布局:AutoFrameLayout、AutoLinearLayout、AutoRelativeLayout
(使用这几个布局只会影响布局的宽高和边距,其余使用与FrameLayout、LinearLayout、RelativeLayout相同)

属性说明

名称 数据类型 说明 备注
(子布局属性)
auto_width integer 宽度 当该属性设置时会使layout_width属性失效
auto_height integer 高度 当该属性设置时会使layout_height属性失效
auto_width_extra float 额外的宽度
auto_height_extra float 额外的高度
auto_margin_left integer 左边距
auto_margin_top integer 上边距
auto_margin_right integer 右边距
auto_margin_bottom integer 下边距
auto_margin_left_extra float 额外的左边距
auto_margin_top_extra float 额外的上边距
auto_margin_right_extra float 额外的右边距
auto_margin_bottom_extra float 额外的下边距
auto_width_height_ratio float 宽高比 默认设置为‘相对宽’
auto_ratio_refer_to refer_to_width/refer_to_height 相对于宽/高 当设置为‘相对宽’时,高度的值将取决于宽和宽高比
父布局属性
auto_padding_left integer 左边距
auto_padding_top integer 上边距
auto_padding_right integer 右边距
auto_padding_bottom integer 下边距
auto_padding_left_extra float 额外的左边距
auto_padding_top_extra float 额外的上边距
auto_padding_right_extra float 额外的右边距
auto_padding_bottom_extra float 额外的下边距

3、其它

为了应对更加复杂的情况,我们提供了一套工具能够在运行时修改布局的样式,同样你也可以通过该工具获取手机尺寸参数

四、已知问题

1.在使用xml布局时无法预览
2.在使用xml布局时无法智能提示能够使用的属性(declare-styleable 的命名无法与自定义控件统一)

五、联系方式

624996437@qq.com
在使用上遇到任何问题或者有什么好的建议都可以通过上诉邮箱与我联系

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

推荐阅读更多精彩内容