Android百分比布局支持库(android-percent-support)
Android中提供了五种布局,其中用的最多的就是:LinearLayout, RelativeLayout 和 FrameLayout这三种布局,在对某一界面进行布局时最先想到也是通过这三种来布局的,不过当某一界面过于复杂时,往往会有多层嵌套,可能嵌套层数过深超过5层,比如,当我们有一个需求是这样的:界面中的一个按钮的长度需要是屏幕宽度的一半,而且需要在任何屏幕下都是屏幕宽度的一半,这个需求我们往往想到的就是用LinearLayout的weightSum属性和child的layout_weight属性结合来用来达到效果,你可能这样实现:
单独给这个Button包一层LinearLayout来设置weightSum和layout_weight:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:weightSum="1">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Button"/>
</LinearLayout>
</RelativeLayout>
最后也达到了效果:
不过这样未免太浪费了吧,如果我的视图更复杂一些,比如要实现下面这样的布局,都是需要按照屏幕的宽高来给控件设置宽高比例,那么如果真要实现那么你的layout文件一定非常大。
竖屏:
横屏:
可以看到,这的确是适配屏幕的,是按照屏幕的宽高来对控件进行不同比例的设置。
要完成上面这样的布局,如果采用通常方法来布局那么将非常复杂,庆幸的是Google推出了一种新的布局库,叫百分比布局库,可以在sdk目录下就可以找到对应的jar包,然后把它添加到项目中即可使用,即:
所以,我们采用百分比布局来实现上面这种布局。
百分比布局库android-percent-support
百分比布局库中提供了两种布局可以设置百分比:PercentRelativeLayout、PercentFrameLayout,为什么没有LinearLayout呢?因为LinearLayout可以根据weightSum和layout_weight这两个属性来对child进行很方便的布局适配。
这两个百分比布局都有以下九个布局属性,值都是用百分比来表示宽度、高度、margin值,使用时候需要父布局为百分比布局,child控件才可以使用这九个布局属性:
- app:layout_heightPercent
- app:layout_widthPercent
- app:layout_marginPercent
- app:layout_marginTopPercent
- app:layout_marginBottomPercent
- app:layout_marginLeftPercent
- app:layout_marginRightPercent
- app:layout_marginStartPercent
- app:layout_marginEndPercent
使用如下:
<android.support.percent.PercentRelativeLayout 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">
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="width=50%"
app:layout_widthPercent="50%" />
</android.support.percent.PercentRelativeLayout>
上面这样就设置了Button的宽度是父布局宽度的一半,所以在不同屏幕下也永远是父布局宽度的一般,这样很方便的实现了而不需要用LinearLayout设置weightSum和layout_weight来控制。
用百分比布局实现上图布局
其实PercentRelativeLayout 和PercentFrameLayout就是多了九个布局属性的RelativeLayout 和FrameLayout,用法完全和这两个布局一样,不过只有父布局是百分比布局(PercentRelativeLayout和PercentFrameLayout )的时候,child才能使用百分比布局属性进行布局,否则无效,如:
<android.support.percent.PercentRelativeLayout 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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="width=50%"
app:layout_widthPercent="50%" />
</LinearLayout>
</android.support.percent.PercentRelativeLayout>
这样设置Button的宽度并不会是父布局宽度的一半,因为child用百分比只对父布局是百分比布局才有效。
好了,现在就直接贴下上面这幅图的布局xml:
<android.support.percent.PercentRelativeLayout 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">
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="width=50%"
app:layout_widthPercent="50%" />
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_below="@id/button"
android:background="#E91E63"
android:text="height=20%,width=50%"
app:layout_heightPercent="20%"
app:layout_marginLeftPercent="50%"
app:layout_widthPercent="50%" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_below="@id/button"
android:background="#9C27B0"
android:text="height=20%,width=50%"
app:layout_heightPercent="20%"
app:layout_widthPercent="50%" />
<android.support.percent.PercentRelativeLayout
android:id="@+id/percent1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textView2">
<TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#03A9F4"
android:text="height=20%,width=25%"
app:layout_heightPercent="20%"
app:layout_widthPercent="25%" />
<TextView
android:id="@+id/textView4"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_toRightOf="@id/textView3"
android:background="#CDDC39"
android:text="height=20%,width=50%"
app:layout_heightPercent="20%"
app:layout_widthPercent="50%" />
<TextView
android:id="@+id/textView5"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_toRightOf="@id/textView4"
android:background="#FF5722"
android:text="height=20%,width=25%"
app:layout_heightPercent="20%"
app:layout_widthPercent="25%" />
</android.support.percent.PercentRelativeLayout>
<android.support.percent.PercentRelativeLayout
android:id="@+id/percent2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_below="@id/percent1"
android:background="#FFCCBC"
android:gravity="center_horizontal"
app:layout_widthPercent="50%">
<TextView
android:id="@+id/textView6"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#03A9F4"
android:text="height=20%,width=25%"
app:layout_heightPercent="20%"
app:layout_marginTopPercent="10%"
app:layout_widthPercent="50%" />
<TextView
android:id="@+id/textView7"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_below="@id/textView6"
android:background="#CDDC39"
android:text="height=20%,width=50%"
app:layout_heightPercent="20%"
app:layout_marginTopPercent="10%"
app:layout_widthPercent="50%" />
</android.support.percent.PercentRelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/percent1"
android:layout_toRightOf="@id/percent2"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="10">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2.5"
android:text="B" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2.5"
android:text="B" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:weightSum="1">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Button" />
</LinearLayout>
</LinearLayout>
</android.support.percent.PercentRelativeLayout>
Android百分比布局支持库(android-percent-support)的更多相关文章
- Android百分比布局支持库介绍——com.android.support:percent(转)
转载自http://www.apkbus.com/forum.php?mod=viewthread&tid=244752&extra=&_dsign=0b699c42 在此之前 ...
- Android 自带图标库 android.R.drawable
在xml文件中调用. android:title="@string/secure_connect"android:orderInCategory="100"an ...
- Android - 加入Android的OpenCV依赖库(Android Dependencies) 问题
加入Android的OpenCV依赖库(Android Dependencies) 问题 本文地址: http://blog.csdn.net/caroline_wendy 假设想要加入OpenCV的 ...
- Android 百分比布局库(percent-support-lib) 解析与扩展
转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/46695347: 本文出自:[张鸿洋的博客] 一.概述 周末游戏打得过猛,于是周 ...
- Android百分比布局方案
百分比布局让其中的控件在指定高度,宽度,margin时使用屏幕宽高的百分比,不使用dp,px.这样一套布局可以适应多个屏幕,方便适配.如: app:layout_heightPercent=" ...
- Android百分比布局成功导入及简单使用
最近学习第一行代码第二版这本书,里面有介绍百分比布局的使用,经过一番摸索,终于是成功导入了百分比布局 就是这样,appcompat是25.3.1,那么百分比布局percent也是25.3.1 这样便是 ...
- [原]ffmpeg编译android 硬解码支持库 libstagefright
最近花了一天时间将ffmpeg/tools/build_stagefright执行成功,主要是交叉编译所需要的各种动态库的支持没链接上,导致各种报错,基本上网络上问到的问题我都碰到了,特此记录下来. ...
- android xml 布局文件中 android:ems="10"
宽度为10个字符的宽度 xml中 android:ems属性 ,作为EditText 默认生成 的属性,其含义是需要编辑的 字符串长度 .设置为10时,最多编辑 10个em ,一个em单位是 两个in ...
- android support Percent支持库开发
Android的布局支持百分比的设置进行开发,来学习如何去实现它,不过看起来会像网页的设置,比如宽度的设置属性是`layout_widthPercent`.在此之前,我们一般都会设置Linearlay ...
随机推荐
- DOS界面下的翻译软件制作
准备 素材 依赖 接口 地址 参数 返回值解析 编码及测试 功能代码 运行脚本 环境变量 结果展示 英语转汉语 汉语转英语 总结 昨天看到一篇关于Linux下的桌面词典的文章,于是就想实现一个Wind ...
- 游戏引擎cocos2d-android使用大全
做手机游戏需要三个核心的类,即:SurfaceView,SurfaceHolder,Thread.帧数要在30帧左右是最好的. cocos2d游戏引擎 封装好的框架,可直接使用 cocos2d-and ...
- activiti实战系列 activiti连线
11:连线 11.1:流程图 注意:如果将流程图放置在和java类相同的路径,需要配置: 11.2:部署流程定义+启动流程实例 11.3:查询我的个人任务 11.4:完成任务 说明: 1)使用流程变量 ...
- Xcode8之后,苹果列出了最新App被拒十大原因
开发者在开发应用程序之前,熟悉苹果审核应用的技术.内容以及设计准则是非常重要的,可以大大降低应用审核被拒的可能性. 最近,苹果通过一个专门的页面给出了截止2016年10月10日应用提交审核被拒的十大原 ...
- windows下安装nginx (转载自:http://blog.163.com/njut_wangjian/blog/static/1657964252013327103716818/)
1. 到nginx官网上下载相应的安装包,http://nginx.org/en/download.html:下载进行解压,将解压后的文件放到自己心仪的目录下,我的解压文件放在了d盘根目录下,如下图 ...
- Web开发学习之路--Springmvc+Hibernate之初体验
本来想继续学习android的,可是用到了android和服务器交互,需要实现个login的功能,苦于没有这么个环境,那就只能自己来搭建了.既然已经基本上可以玩web了,那么接下来使用web开源的框架 ...
- Android中JNI编程详解
前几天在参加腾讯模拟考的时候,腾讯出了一道关于JNI的题,具体如下: JNI本身是一个非常复杂的知识,但是其实对于腾讯的这道题而言,如果你懂JNI,那么你可能会觉得这道题非常简单,就相当于C语言中的h ...
- tomcat请求路由映射核心组件Mapper
Mapper组件的核心功能是提供请求路径的路由映射,根据某个请求路径通过计算得到相应的Servlet(Wrapper).这节看下Mapper的实现细节,包括Host容器.Context容器.Wrapp ...
- Java 实现的各种经典的排序算法小Demo
由于有上机作业,所以就对数据结构中常用的各种排序算法都写了个Demo,有如下几个: 直接插入排序 折半插入排序 希尔排序 冒泡排序 快速排序 选择排序 桶排序 Demo下载地址 下面谈一谈我对这几个排 ...
- Android 官方命令深入分析之Android Debug Bridge(adb)
作者:宋志辉 Android Debug Brideg(adb)是一个多用途的命令行工具.可以与Android虚拟机进行通信或连接真机.它同样提供了访问设备shell的高级命令行操作的权限.它是一个包 ...