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

那么为什么本篇博客的存在的意义是什么呢?

首先我们回顾下百分比布局库的用法,提供了PercentRelativeLayoutPercentFrameLayout供大家在编写的时候,对于以下属性:

layout_widthPercentlayout_heightPercent

layout_marginPercentlayout_marginLeftPercent

layout_marginTopPercentlayout_marginRightPercent

layout_marginBottomPercentlayout_marginStartPercentlayout_marginEndPercent

可以使用百分比进行设置宽、高、边距,的确给我们在适配上提供了极大的便利,但是在使用过程中,觉得存在一些场景无法得到满足。什么场景呢?下面我举几个例子。

  1. 当使用图片时,无法设置宽高的比例

比如我们的图片宽高是200*100的,我们在使用过程中我们设置宽高为20%、10%,这样会造成图片的比例失调。为什么呢?因为20%参考的是屏幕的宽度,而10%参考的是屏幕的高度。

  1. 很难使用百分比定义一个正方形的控件

比如,我现在界面的右下角有一个FloatingActionButton,我希望其宽度和高度都为屏幕宽度的10%,很难做到。

  1. 一个控件的margin四个方向值一致

有些时候,我设置margin,我希望四边的边距一致的,但是如果目前设置5%,会造成,上下为高度的5%,左右边距为宽度的5%。

综合上述这些问题,可以发现目前的percent-support-lib并不能完全满足我们的需求,所以我们考虑对其进行扩展。说白了,我们就希望在布局的时候可以自己设定参考看度还是高度,比如上述2,我们对于宽高可以写成10%w,10%w。也就是在不改变原库的用法的前提下,添加一些额外的支持。


二 扩展的功能

目前我初步对该库进行了改写,github地址:android-percent-support-extend,对于官方库,做了如下的改变:

  1. 不改变原有库的用法

  2. 添加了PercentLinearLayout

  3. 支持百分比指定特定的参考值,比如宽度或者高度。

例如:app:layout_heightPercent="50%w", app:layout_marginPercent="15%w",

app:layout_marginBottomPercent="20%h".

  1. 支持通过app:layout_textSizePercent设置textView的textSize

  2. 对于外层套ScrollView的问题,目前可以在PercentLinearLayout的外层使用ScrollView,不过对于宽度的百分比参考的就是android.R.id.content的高度(因为,无法参考父控件的高度,父控件的高度理论上依赖于子View高度,且模式为UNSPECIFIED)。

对于如何导入,也是相当的简单,android studio的用户,直接:

dependencies {

//…

compile ‘com.zhy:percent-support-extends:1.0.1’

}

不需要导入官方的percent-support-lib了。

对于的三个类分别为:

com.zhy.android.percent.support.PercentLinearLayout

com.zhy.android.percent.support.PercentRelativeLayout

com.zhy.android.percent.support.PercentFrameLayout

对于eclipse的用户:github上自行下载源码,就几个类和一个attrs.xml,也可以在bintray.com/percent-support-extends 下载相关文件。

下面看几个具体的示例。


三 具体的示例

Demo 1

xml:

<?xml version="1.0" encoding="utf-8"?>

<com.zhy.android.percent.support.PercentFrameLayout

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”

android:orientation=“vertical”>

<com.zhy.android.percent.support.PercentFrameLayout

android:layout_width=“0dp”

android:layout_height=“0dp”

android:layout_gravity=“center”

android:background=“#ff44aacc”

app:layout_heightPercent=“50%w”

app:layout_widthPercent=“50%w”>

<com.zhy.android.percent.support.PercentFrameLayout

android:layout_width=“0dp”

android:layout_height=“0dp”

android:layout_gravity=“center”

android:background=“#ffcc5ec7”

app:layout_heightPercent=“50%w”

app:layout_widthPercent=“50%w”>

<TextView

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:layout_gravity=“center”

android:background=“#ff7ecc16”

android:gravity=“center”

android:text=“margin 15% of w”

app:layout_marginPercent=“15%w”

/>

</com.zhy.android.percent.support.PercentFrameLayout>

</com.zhy.android.percent.support.PercentFrameLayout>

<TextView android:layout_width=“0dp”

android:layout_height=“0dp”

android:layout_gravity=“bottom|right”

android:background=“#44ff0000”

android:gravity=“center”

android:text=“15%w,15%w”

app:layout_heightPercent=“15%w”

app:layout_marginPercent=“5%w”

app:layout_widthPercent=“15%w”/>

</com.zhy.android.percent.support.PercentFrameLayout>

Demo 2

xml:

<?xml version="1.0" encoding="utf-8"?>

<com.zhy.android.percent.support.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”

android:clickable=“true”>

<TextView

android:id=“@+id/row_one_item_one”

android:layout_width=“0dp”

android:layout_height=“0dp”

android:layout_alignParentTop=“true”

android:background=“#7700ff00”

android:text=“w:70%,h:20%”

android:gravity=“center”

app:layout_heightPercent=“20%”

app:layout_widthPercent=“70%”/>

<TextView

android:id=“@+id/row_one_item_two”

android:layout_width=“0dp”

android:layout_height=“0dp”

android:layout_toRightOf=“@+id/row_one_item_one”

android:background=“#396190”

android:text=“w:30%,h:20%”

app:layout_heightPercent=“20%”

android:gravity=“center”

app:layout_widthPercent=“30%”/>

<ImageView

android:id=“@+id/row_two_item_one”

android:layout_width=“match_parent”

android:layout_height=“0dp”

android:src=“@drawable/tangyan”

android:scaleType=“centerCrop”

android:layout_below=“@+id/row_one_item_one”

android:background=“#d89695”

app:layout_heightPercent=“70%”/>

<TextView

android:layout_width=“0dp”

android:layout_height=“0dp”

android:layout_below=“@id/row_two_item_one”

android:background=“#770000ff”

android:gravity=“center”

android:text=“width:100%,height:10%”

app:layout_heightPercent=“10%”

app:layout_widthPercent=“100%”/>

</com.zhy.android.percent.support.PercentRelativeLayout>

ok,例子都比较简单,主要就一个布局文件,可以看出上述我们可以给宽度、高度,边距等指定参考值为宽度或者高度。这样的话,在保证图片宽、高比例、控件设置为正方形等需求就没问题了。


接下来还有个例子,功能主要是设置TextView对于textSize的百分比设置;以及对于ScrollView的支持。当然了,对于ScrollView的支持,这个理论上是不支持的,因为大家都清楚,如果PercentLinearLayout在ScrollView中,那么高度的模式肯定是UNSPECIFIED,那么理论上来说高度是无限制的,也就是依赖于子View的高度,而百分比布局的高度是依赖于父View的高度的,所有是互斥的。而我们支持是:考虑到编写代码的时候,大多参考的是屏幕高度(android.R.id.content)的高度,所以如果在ScrollView中,编写10%h,这个百分比是依赖于屏幕高度的(不包括ActionBar的高度)。

Demo 3

xml:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView

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”>

<com.zhy.android.percent.support.PercentLinearLayout

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=“wrap_content”

android:orientation=“vertical”>

<TextView

android:layout_width=“0dp”

android:layout_height=“0dp”

android:background=“#ff44aacc”

android:gravity=“center”

android:text=“width:60%,height:5%,ts:3%”

android:textColor=“#ffffff”

app:layout_heightPercent=“5%”

app:layout_marginBottomPercent=“5%”

app:layout_textSizePercent=“3%”

app:layout_widthPercent=“60%”/>

<TextView

android:layout_width=“0dp”

android:layout_height=“0dp”

android:background=“#ff4400cc”

android:gravity=“center”

android:text=“width:70%,height:10%”

android:textColor=“#ffffff”

app:layout_heightPercent=“10%”

app:layout_marginBottomPercent=“5%”

app:layout_widthPercent=“70%”/>

<TextView

android:layout_width=“0dp”

android:layout_height=“0dp”

android:background=“#ff44aacc”

android:gravity=“center”

android:text=“w:80%,h:15%,textSize:5%”

android:textColor=“#ffffff”

app:layout_heightPercent=“15%”

app:layout_marginBottomPercent=“5%”

app:layout_textSizePercent=“5%”

app:layout_widthPercent=“80%”/>

<TextView

android:layout_width=“0dp”

android:layout_height=“0dp”

android:background=“#ff4400cc”

android:gravity=“center”

android:text=“width:90%,height:5%”

android:textColor=“#ffffff”

app:layout_heightPercent=“20%”

app:layout_marginBottomPercent=“5%”

app:layout_widthPercent=“90%”/>

<TextView

android:layout_width=“match_parent”

android:layout_height=“0dp”

android:background=“#ff44aacc”

android:gravity=“center”

android:text=“width:100%,height:25%”

android:textColor=“#ffffff”

app:layout_heightPercent=“25%”

app:layout_marginBottomPercent=“5%”

/>

<TextView

android:layout_width=“match_parent”

android:layout_height=“0dp”

android:background=“#ff44aacc”

android:gravity=“center”

android:text=“width:100%,height:30%”

android:textColor=“#ffffff”

app:layout_heightPercent=“30%”

app:layout_marginBottomPercent=“5%”

/>

</com.zhy.android.percent.support.PercentLinearLayout>

上面的第三个TextView的字体设置的就是5%(默认参考容器高度)。整个PercentLinearLayout在ScrollView中。ok~ 姑且这样,由于源码比较简单,大家可以根据自己的实际需求去修改,前提尽可能不要改变原有的功能。


四 扩展的相关源码

(一) 关于attrs.xml

原库中所有的属性的format为fraction,但是由于我期望的写法有10%w,10%h,10%,没有找到合适的format,就直接定义为string了string我可以自己去解析

<?xml version="1.0" encoding="utf-8"?>

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

最后

在这里小编整理了一份Android大厂常见面试题,和一些Android架构视频解析,都已整理成文档,全部都已打包好了,希望能够对大家有所帮助,在面试中能顺利通过。

image

image

喜欢本文的话,不妨顺手给我点个小赞、评论区留言或者转发支持一下呗

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门即可获取!

-1712153988809)]

[外链图片转存中…(img-x4FBzTm8-1712153988809)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

最后

在这里小编整理了一份Android大厂常见面试题,和一些Android架构视频解析,都已整理成文档,全部都已打包好了,希望能够对大家有所帮助,在面试中能顺利通过。

[外链图片转存中…(img-QEFyVZvb-1712153988811)]

[外链图片转存中…(img-5KBQI2Ce-1712153988811)]

喜欢本文的话,不妨顺手给我点个小赞、评论区留言或者转发支持一下呗

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门即可获取!
  • 30
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值