虽然有很多的布局可以在 Android 应用程序开发的世界供我们使用,但我们总是只用这三种布局:LinearLayout, RelativeLayout and FrameLayout。

不管怎么说在用 RelativeLayout 和 FrameLayout 的时候总有一些问题,因为你不能设置子视图的百分比程度。只有两种方法可能做到,1. 布局在 LinearLayout 里并用它的 layout_weight 布局参数;2. 在 Java 代码中重写 onMeasure 方法来实现。

举个例子,如果我想在 RelativeLayout 里放一个简单的红色矩形,它是在顶部,并且距离左边的 5% 的位置,宽度为屏幕的25%。我们的代码可以这样写:

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:weightSum="20">

android:layout_width="0dp"

android:layout_height="match_parent"

android:layout_weight="1"

/>

android:layout_width="0dp"

android:layout_height="100dp"

android:layout_weight="5"

android:background="#ff0000" />

这是结果:

percentlayout_1

你会注意到这样的代码应该是非常复杂的。与此同时,这些控件也用视图和 LinearLayout 填充着,我们可以把它们看成是一种浪费。

这将不再是一个问题啦,因为在前几天 Android M 宣布了它的名字:棉花糖(Marshmallow),Android 团队推出了许多支持库去帮助开发者与碎片化的战斗!其中之一就是 百分比支持库(Percent Support Library),它具备的能力是用百分比去设置 RelativeLayout 和 FrameLayout 的尺寸!

你好,百分比支持库

这个库是非常容易使用的,因为它就如同 RelativeLayout 和 FrameLayout 一样我们都熟悉,只是有一些额外的功能。

首先,因为百分比支持库是随着 Android Support Library 23 一起的,所请确保你已经在 SDK Manager 中的 Android Support Library 更新了最新的版本。然后在 build.gradle 文件中添加下面这样的依赖:

compile 'com.android.support:percent:23.0.0'

现在,在使用老的 RelativeLayout 和 FrameLayout 做替换,只需要简单的将他们各自切换到 android.support.percent.PercentRelativeLayout 和 android.support.percent.PercentFrameLayout。这里有9个布局参数可以使用:

layout_widthPercent : 用百分比来表示宽度,比如:app:layout_widthPercent="25%"

layout_heightPercent : 用百分比来表示高度

layout_marginPercent : 用百分比来表示 Margin

其余的是用百分比来表示每个 margin 面 layout_marginLeftPercent, layout_marginRightPercent, layout_marginTopPercent, layout_marginBottomPercent, layout_marginStartPercent, layout_marginEndPercent

用 PercentRelativeLayout,上面的代码例子就能这样来写了:

xmlns:app="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent">

app:layout_widthPercent="25%"

android:layout_height="100dp"

app:layout_marginLeftPercent="5%"

android:background="#ff0000" />

这是结果:

percentlayout_2

你可以看到结果是完全是一致的,而且具有更短更清晰的代码,此外,该空间现在没有填充其他布局,这就让性能达到了更好的程度。

实际上这本应该就是 Android 整体的一部分,但不幸的是,事实并非如此。把它填加到原生Android 的RelativeLayout/FrameLayout 已经太迟了,因为用户用的设备都是老的操作系统版本,不可能支持使用这个功能。所以这就是为什么 Android 团队决定把它作为一个支持库来发布,我支持这个主意。

请去尝试一下,它是有助于让你的代码更加干净,更加好 =)

android relativelayout 比例,百分比布局支持库:RelativeLayout 和 FrameLayout 的尺寸用 % 来表示...相关推荐

  1. android软件百分比怎么实现,Android自实现百分比布局

    在开发中,组件布局是大家每日开发必须要面对的工作,对于Android来说提供五种常用布局,分别是:LinearLayout(线性布局) TableLayout(表格布局) RelativeLayout ...

  2. Android屏幕适配之百分比布局LinearLayout、RelativeLayout、FrameLayout

    谷歌的百分比库已废弃,转用约束布局,本博文仅供参考思路.新方法请参考:https://blog.csdn.net/qq_21480607/article/details/98790567 一.首先说一 ...

  3. Android百分比布局支持和垂直TextView

    In this tutorial, we'll discuss and implement Android Percent Layout Support Library. Furthermore, w ...

  4. android百分比扩展枯,Android 增强版百分比布局库 为了适配而扩展

    一 概述 上周一我们发布了Android 百分比布局库(percent-support-lib) 解析与扩展中对percent-support这个库进行了解析和添加了PercentLinearLayo ...

  5. Android 增强版百分比布局库 为了适配而扩展

    转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/46767825: 本文出自:[张鸿洋的博客] 一 概述 上周一我们发布了Andr ...

  6. Android 百分比布局(支持AndroidX)

    Android 百分比布局 添加依赖 代码使用 添加依赖 implementation 'com.android.support:percent:25.2.0' 我的项目是AndroidX的,依然用这 ...

  7. 【Android 应用开发】 Android 各种版本简介 ( Support 支持库版本 | Android Studio 版本 | Gradle 版本 | jcenter 库版本 )

    初学者遇到 Android Studio, 导入工程后, 会出现各种奇葩错误, 如果管理好各个插件, gradle, SDK, SDK Tools, 各种官方依赖库 的版本, 会将错误大大的减少; 这 ...

  8. android百分比布局失效,Android 百分比布局库【原创】

    为了解决android手机适配问题,我们经常想如果可以按照百分比的方式进行界面布局,这样适配各种屏幕就简单多了吧!现在谷歌正式提供百分比布局支持库(android-support-percent-li ...

  9. Android 第七课 4种基本布局之FrameLayout和百分比布局

    FrameLayout(帧布局),她没有方便的定位方式,所有的控件都会默认摆放在布局的左上角. 修改activity_main.xml中的代码,如下: <?xml version="1 ...

最新文章

  1. 数据库中的select 1,select count(1),order by 1
  2. centos 安装 MatConvNet (gpu)
  3. CSS3 动画插件 aos.js
  4. 服务器性能和活动监视
  5. printf函数讲解
  6. 数据可视化必备的高逼格图表特效,学会只需要五分钟
  7. TGARS 2019: What, Where, and How to Transfer in SAR Target Recognition Based on Deep CNNs ——学习笔记
  8. 最近,华为应用市场上线了一个服务
  9. Mysql 按 create_time 排序导致的问题
  10. 极大似然估计(MLE)、最大后验估计(MAE)
  11. 按键精灵python脚本_Python 假装自己是按键精灵
  12. 电动汽车的数据记录 BMS
  13. 在线购物系统 实验七 顺序图
  14. 计算机组成原理计算题整理
  15. aect17定义_【多选题】AECT关于教育技术的17定义认为,教育技术的目的是()? A. 提高绩效的 B. 促进知识理解 C. 调整改善学习及表现 D. 促进学习...
  16. 国家集训队论文分类整理[转]
  17. SpringBoot修改启动横幅标语(banner)
  18. 基片集成波导原理_双膜基片集成波导(SIW)带通滤波器的设计与HFSS仿真
  19. Adaptive Server Anywhere 数据库配置ODBC数据源
  20. 解析几何:第四章 空间中的直线与平面(1)空间直线的方向、平面方程、空间直线方程

热门文章

  1. mosquitto -- 权限配置
  2. 实施工程师的必要条件
  3. horizon服务主要模块_Horizon组件安装详解 - 远程连接OpenStack服务
  4. 麻省理工的服务器位置,麻省理工学院的地理位置是怎样的?
  5. 数据泄露事件频发,病毒入侵猖獗,装上MCK就能杜绝
  6. Bootstrap-导航条-栅格系统-Carousel(轮播效果)-标签页
  7. java 启动方式 java -jar xx.jar
  8. 《巴黎协定》生效 越南和印尼有望至2020年各新增5GW光伏容量
  9. 《明解C语言》笔记及课后习题答案 【第四章】
  10. 书写php代码能使用的正确形式,最新PHP技术笔试练习题