Android--百分比布局库(percent-support-lib)

万能适配,百分比布局,支持padding 本文是基于http://blog.csdn.net/lmj623565791/article/details/46767825的修改,介绍及使用,请按左侧链接参拜鸿洋大神。 因自己使用中,发现不支持padding,所以自己谢了几行代码,非常简单,但肯定有人懒得写,想直接拿来用,OK,请直接下载来用,包名改改就可以了 https://github.com/zhengvince/PersentLa 阅读详情

android-percent-support-extend

对于android-percent-support的扩展库。

一 为什么要扩展这个库

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

layout_widthPercent、layout_heightPercent、 layout_marginPercent、layout_marginLeftPercent、layout_marginTopPercent、layout_marginRightPercent、layout_marginBottomPercent、layout_marginStartPercent、layout_marginEndPercent。

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

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

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

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

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

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

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

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

二 扩展的功能

对于官方库,做了如下的改变:

  1. 不改变原有库的用法
  2. 添加了PercentLinearLayout
  3. 支持百分比指定特定的参考值,比如宽度或者高度。

    例如:app:layout_heightPercent="50%w", app:layout_marginPercent="15%w",app:layout_marginBottomPercent="20%h".

  4. 支持通过app:layout_textSizePercent设置textView的textSize
  5. 对于外层套ScrollView的问题,目前可以在PercentLinearLayout的外层使用ScrollView,不过对于宽度的百分比参考的就是android.R.id.content的高度(因为,无法参考父控件的高度,父控件的高度理论上依赖于子View高度,且模式为UNSPECIFIED)。
  6. 支持app:layout_maxWidthPercent, app:layout_maxHeightPercent, app:layout_minWidthPercent,app:layout_minWidthPercent的支持。

    (注:用法和maxHeight等一致,如果使用:值会覆盖原本的maxHeight等属性)。

三 用法

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

dependencies {
    //...
    compile 'com.zhy:percent-support-extends:1.0.4'
}

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

对于的三个类分别为:

com.zhy.android.percent.support.PercentLinearLayout
com.zhy.android.percent.support.PercentRelativeLayout
com.zhy.android.percent.support.PercentFrameLayout

支持的属性 :

  • layout_heightPercent
  • layout_widthPercent
  • layout_marginBottomPercent
  • layout_marginEndPercent
  • layout_marginLeftPercent
  • layout_marginPercent
  • layout_marginRightPercent
  • layout_marginStartPercent
  • layout_marginTopPercent
  • layout_textSizePercent

对于值可以取:10%w , 10%h , 10%

对于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.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">

    <TextView
        android:id="@+id/id_test"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_gravity="left|top"
        android:background="#44ff0000"
        android:gravity="center"
        android:onClick="test1"
        android:text="width:30%,height:20%"
        app:layout_heightPercent="20%"
        app:layout_widthPercent="30%"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_gravity="right|top"
        android:background="#4400ff00"
        android:gravity="center"
        android:text="width:70%,height:20%"
        app:layout_heightPercent="20%"
        app:layout_widthPercent="70%"/>


    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_gravity="bottom"
        android:background="#770000ff"
        android:gravity="center"
        android:text="width:100%,height:10%"
        app:layout_heightPercent="10%"
        app:layout_widthPercent="100%"/>


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:scaleType="centerCrop"
        android:src="@drawable/tangyan"
        app:layout_heightPercent="50%h"
        app:layout_widthPercent="50%w"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_heightPercent="10%h"
        app:layout_widthPercent="10%h"
        android:text="10%h,10%h"
        android:gravity="center"
        android:layout_gravity="bottom|right"
        android:background="#44ff0000"
        app:layout_marginBottomPercent="20%h"
        app:layout_marginRightPercent="10%"/>


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


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>
</ScrollView>

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

About me


==> 接下来是原库的用法:

Android Percent Support Lib Sample :triangular_ruler::triangular_ruler::triangular_ruler:

Android Arsenal

I made a sample of the new percent support library.
It will be documented soon on android dev website so it's just a proof of concept :)
You can check official docs reference here

This library provide percentage based layouts, horizontal and vertical at the same time.

simple result

PercentRelativeLayout

complex result

PercentRelativeLayout Complex
from https://gist.github.com/shekarshine/5458f697c02e5619b0e2

How to use :

just add percent support library to your project

dependencies {
    compile 'com.android.support:percent:22.2.0'
}

Supported Layouts :

PercentRelativeLayout
<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">

    <View
        android:id="@+id/top_left"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_alignParentTop="true"
        android:background="#ff44aacc"
        app:layout_heightPercent="20%"
        app:layout_widthPercent="70%" />

    <View
        android:id="@+id/top_right"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/top_left"
        android:background="#ffe40000"
        app:layout_heightPercent="20%"
        app:layout_widthPercent="30%" />


    <View
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_below="@+id/top_left"
        android:background="#ff00ff22"
        app:layout_heightPercent="80%" />
</android.support.percent.PercentRelativeLayout>
PercentFrameLayout
<android.support.percent.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">
        <!-- ... XML CODE -->
</android.support.percent.PercentFrameLayout>

Stylable :

  • heightPercent
  • widthPercent
  • marginBottomPercent
  • marginEndPercent
  • marginLeftPercent
  • marginPercent
  • marginRightPercent
  • marginStartPercent
  • marginTopPercent

Pre-requisites

  • Android SDK v22
  • Android Build Tools v22.0.1
  • Android Percent Support Repository v22.2.0
  • Android Support v4 Repository v22.2.0

according to the the manifest minsdk is v7 (android 2.1)

License Mit
android-percent-support-extend源码 android-percent-support-extend源码,是一个很不错的Android代码,有兴趣的伙伴们抽时间可以看一下把。 立即下载

相关推荐

Google百分比布局demo

google百分比布局demo,里面有percentFrameLayout, percentLinearLayout, percentRelativityLayout,完美的屏幕适配方案,有需要的可以下载使用

自定义进度条弹框

添加百分比布局依赖 implementation 'com.zhy:percent-support-extends:1.1.1' value文件夹添加 颜色值 <color name="transparent">#00000000</color><!--透明--> <color name="gary">#e8eaef</color><!--灰--> <color name="white">#FFFFFF</c

m0_48440239的博客 355

Android屏幕适配之Google百分比布局库的扩展.rar

Android屏幕适配之Google百分比布局库的扩展在项目中引用 compile 'com.zhy:percent-support-extends:1.1.1'依赖, 主要有三个用于布局的类 com.zhy.android.percent.support.PercentLinearLayout com.zhy.android.percent.support.PercentRelativeLayout com.zhy.android.percent.support.PercentFrameLayout 具体使用看源码

God‘s Eye View 实时 3D 地球上手:20 个图层 18 个免 Key,15 分钟走完

God's Eye View 把开源空间信号做成一颗实时 3D 地球:航班、船舶、卫星、地震、公共摄像头,全是真实数据。免账号免 Key 就能开玩,底图默认 Esri 卫星影像,连不上自动切 OSM。下面按 跑起来 → 看到东西 → 能动什么 → 换看法 的顺序走。 ## 第一次打开:选张任务卡,挑条路跑起来 首次启动不会急着铺信号,而是先停在 MISSION CONTROL 卡片上,给四个选

gitblog_00022的博客 426

边缘计算【智能+安全检测】系列教程--使用OpenCV+GStreamer实现真正的硬解码,完全消除马赛克

通过现有博客的GStreamer的解码方式解码,大多情况应该存在上图马赛克的问题,请勿紧张,其实上述url并没有调用硬件解码,后面有调用硬件解码的ULR,我们来在接下来的介绍中释获。通过RTSP协议连接海康威视摄像头将图片送入AI推理,发现OpenCV的VideoCapture使用的是软解码大量消耗了cpu的资源,并无法做到实时数据处理存在5秒钟的延时。只能把解码这部分送到硬件解码器去处理分摊掉cpu的压力,

Starry-SkyJing的博客 2440

Spring Boot Swagger未授权访问漏洞:原理、风险与修复方案详解

在微服务架构中,API文档管理是开发与运维的重要环节。Swagger作为RESTful API文档生成与调试的标准化工具,通过OpenAPI规范自动生成交互式接口文档,极大提升了开发效率。然而,其默认配置往往缺乏访问控制,导致文档端点直接暴露,形成未授权访问漏洞。从技术原理看,该漏洞源于Spring Boot等框架集成Swagger时,相关端点未纳入安全框架的拦截范围,攻击者可通过扫描常见路径获取完整的接口定义与数据结构。这不仅会造成敏感信息泄露,更可能成为越权操作、数据篡改等深度攻击的入口点。在工程实践中

weixin_42524171的博客 515

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

针对Android程序员,我这边给大家整理了一些资料,包括不限于高级UI、性能优化、架构师课程、NDK、混合式开发(ReactNative+Weex)微信小程序、Flutter等全方面的Android进阶实践技术;希望能帮助到大家,也节省大家在网上搜索资料的时间来学习,也可以分享动态给身边好友一起学习!Android前沿技术大纲全套体系化高级架构视频Android高级架构资料、源码、笔记、视频。

2401_84149505的博客 434

iTunes登录协议全解析:从抓包到加密参数生成(含HTTPDebugger避坑指南)

本文深度解析iTunes登录协议,提供从环境搭建、绕过反调试机制到捕获网络流量的完整实战指南。重点剖析了如何利用HTTPDebugger等工具进行抓包,并详细解读了核心加密参数X-Apple-ActionSignature的生成逻辑,为网络安全研究员和协议分析工程师提供了一份详尽的逆向分析与协议复现教程。

weixin_28314575的博客 890

Android百分比布局终极指南:轻松实现跨设备完美适配

想要让你的Android应用在不同尺寸的设备上都能完美显示吗?android-percent-support-extend扩展库为你提供了简单高效的解决方案。这个强大的百分比布局库不仅保留了官方库的所有功能,还添加了更多实用的特性,让界面适配变得前所未有的简单。😊 ## 为什么选择百分比布局扩展库 传统的Android布局方式在面对多样化屏幕尺寸时常常显得力不从心。android-perce

gitblog_00888的博客 545

【四旋翼】pixhawk2.4.8-地面站配置-APM固件-四旋翼从装机到试飞

因为MP版本和APM版本的适配问题,真的真的是很大一个坑。最好先走别人走过的路,走通了再去自由发挥,不然白白浪费时间。下面提供我试飞成功过的版本。下面提供任意版本固件和地面站的下载方法。

xifan的博客 3万+

代谢组数据分析(二十六):LC-MS/MS代谢组学和脂质组学数据的分析流程

本教程概述了LC-MS/MS在代谢组学和脂质组学数据分析中的应用流程。首先介绍了样本的采集、提取和LC-MS/MS分析,随后详细阐述了质谱数据的预处理、峰值识别、特征注释,以及数据标准化和统计分析的重要性。特别强调了使用XCMS和CAMERA进行数据对齐和加合物识别,以及利用MultiABLER和LipidFinder进行数据过滤、插补和标准化处理。此外,还介绍了如何应用EigenMS算法进行数据标准化,以及如何使用Limma包进行差异分析。整个流程旨在提高数据质量,确保分析结果的准确性和可靠性,为后续的生

专注生信领域 1982

Android 百分比布局库(percent-support-lib) 解析与扩展

源文地址:Android 百分比布局库(percent-support-lib) 解析与扩展 一、概述 周末游戏打得过猛,于是周天熬夜码代码,周一早上浑浑噩噩的发现android-percent-support-lib-sample这个项目,Google终于开始支持百分比的方式布局了,瞬间脉动回来,啊咧咧。对于这种历史性的时刻,不出篇博客难以表达我内心的激动。 还记得不久前,发了

Android移动开发者的专栏 4409

【异常】Claude Code CLI工具 模型加载异常 /init报错排查与完整解决方案 There‘s an issue with the selected model (glm-5). It

此类跨厂商模型配置报错,核心诱因是API生态的兼容性不匹配。优先使用工具原生支持的模型体系,可快速解决90%以上的同类报错;若需跨厂商使用目标模型,需完成API端点、鉴权密钥、模型ID的全链路适配,确保全流程配置合规,即可完成异常修复。

奶爸的编程之路,也就一周冷个三天 3726

Android 百分比布局库适配(percent-support-lib) 解析与扩展

文章目录一、概述二、使用三、源码分析三、实现PercentLinearlayout 一、概述 周末游戏打得过猛,于是周天熬夜码代码,周一早上浑浑噩噩的发现android-percent-support-lib-sample这个项目,Google终于开始支持百分比的方式布局了,瞬间脉动回来,啊咧咧。对于这种历史性的时刻,不出篇博客难以表达我内心的激动。 还记得不久前,发了篇博客:Android 屏幕适配方案,这篇博客以Web页面设计引出一种适配方案,最终的目的就是可以通过百分比控制控件的大小。当然了,存在一些

Venus 的博客 843

Open3D (C++) 计算点云坐标最值及其索引

使用C++版本的Open3D计算点云最值点及其对应的下标索引

点云侠的博客 734

PCL 点云拟合 最小二乘法拟合平面

在点云处理领域,最小二乘法(Least Squares)拟合是一种常用的数学方法,用于估计数据点的最佳拟合曲线或平面。平面拟合广泛应用于点云数据处理中的地面检测、物体表面分析等任务。 本文将介绍如何使用PCL库实现基于最小二乘法的平面拟合算法,通过拟合点云数据中的平面,来提取出数据的平面部分。

纵马踏花向自由 1031

Android 百分比布局库(percent-support-lib)

http://blog.csdn.net/lmj623565791/article/details/46695347;  本文出自:【张鸿洋的博客】 一、概述 周末游戏打得过猛,于是周天熬夜码代码,周一早上浑浑噩噩的发现android-percent-support-lib-sample这个项目,Google终于开始支持百分比的方式布局了,瞬间脉动回来,啊咧咧。对于这

Zhangxuexue_的博客 757

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

上一篇: Android--你应该知道的学习资源 进阶之路贵在坚持
下一篇: Android--ViewDragHelper完全解析 自定义ViewGroup神器
Navan-3
博客等级 码龄16年 40粉丝 272原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值