Android进阶系列之Percent Support Library使用详解

总是在为屏幕适配而烦恼,蓝瘦香菇。Google爸爸看着我们这么辛苦,终于在2015年8月推出了一个全新的百分比布局兼容函数库:Android Percent Library。当时我的心情啊,就像看见了千年难得一见的美女。


那么来看看都咋用吧。首先需要在Gradle里加入依赖。

compile ‘com.android.support:percent:24.2.0’

看清楚自己的compileSdkVersion,我的版本是24。根据自己的编译sdk版本来修改compile对应的Support Library。

打开下载后的函数库,发现其中主要包含三个类。

PercentFrameLayout、PercentRelativeLayout、PercentLayoutHelper

大部分的百分比实现逻辑都在PercentLayoutHelper这个类中,首先我们需要知道,PercentFrameLayout、PercentRelativeLayout分贝继承于FrameLayout、RelativeLayout。原有的属性和方法都是可以使用的。同时PercentHelper还对其做了百分比布局的扩展,在xml文件中,增加了如下属性配置。

  • heightPercent :百分比表示高度
  • widthPercent :百分比表示宽度
  • marginBottomPercent :百分比表示底部的间隔
  • marginEndPercent:百分比表示距离最后一个View之间的间隔
  • marginLeftPercent:百分比表示左边的间隔
  • marginPercent :百分比表示View之间的间隔
  • marginRightPercent:百分比表示右边的间隔
  • marginStartPercent:百分比表示距离第一个View之间的间隔
  • marginTopPercent:百分比表示顶部的间隔
举一个简单例子:

  1. <android.support.percent.PercentRelativeLayout  
  2.     xmlns:android=”http://schemas.android.com/apk/res/android”  
  3.     xmlns:tools=”http://schemas.android.com/tools”  
  4.     xmlns:app=”http://schemas.android.com/apk/res-auto”  
  5.     android:id=”@+id/activity_main”  
  6.     android:layout_width=”match_parent”  
  7.     android:layout_height=”match_parent”  
  8.     tools:context=”com.example.administrator.singleinstance.MainActivity”>  
  9.   
  10.     <TextView  
  11.         android:id=”@+id/left”  
  12.         android:layout_width=”0dp”  
  13.         android:layout_height=”0dp”  
  14.         android:layout_alignParentTop=”true”  
  15.         android:background=”@color/colorPrimaryDark”  
  16.         app:layout_heightPercent=”30%”  
  17.         app:layout_widthPercent=”70%”  
  18.         />  
  19.     <TextView  
  20.         android:layout_toRightOf=”@+id/left”  
  21.         android:layout_width=”0dp”  
  22.         android:layout_height=”0dp”  
  23.         android:layout_alignParentTop=”true”  
  24.         android:background=”@color/colorAccent”  
  25.         app:layout_heightPercent=”30%”  
  26.         app:layout_widthPercent=”30%”  
  27.         />  
  28.     <TextView  
  29.         android:layout_below=”@+id/left”  
  30.         android:layout_width=”0dp”  
  31.         android:layout_height=”0dp”  
  32.         android:background=”#000”  
  33.         app:layout_marginTopPercent=”30%”  
  34.         app:layout_marginLeftPercent=”25%”  
  35.         app:layout_heightPercent=”70%”  
  36.         app:layout_widthPercent=”50%”  
  37.         />  
  38.   
  39. </android.support.percent.PercentRelativeLayout>  


这里TextView的height和width可以不指定,但是Android Studio会提示没有设置这两个值。所以可以设置为0dp。percent库新增一个比较特殊的属性,layout_aspectRatio;用于设置View的宽高比。

  1. <TextView  
  2.         android:id="@+id/left"  
  3.         android:layout_width="0dp"  
  4.         android:layout_height="0dp"  
  5.         android:layout_alignParentTop="true"  
  6.         android:background="@color/colorPrimaryDark"  
  7.         app:layout_heightPercent="30%"  
  8.         app:layout_aspectRatio="150%"  
  9.         />  


    
    

google给我们的库中常用的少了一个LinearLayout,那怎么办呢,就用PercentLayoutHelper重新定义一个PercentLinearLayout
  1. public class PercentLinearLayout extends LinearLayout  
  2. {  
  3.   
  4.     private PercentLayoutHelper mPercentLayoutHelper;  
  5.   
  6.     public PercentLinearLayout(Context context, AttributeSet attrs)  
  7.     {  
  8.         super(context, attrs);  
  9.   
  10.         mPercentLayoutHelper = new PercentLayoutHelper(this);  
  11.     }  
  12.   
  13.   
  14.     @Override  
  15.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)  
  16.     {  
  17.         mPercentLayoutHelper.adjustChildren(widthMeasureSpec, heightMeasureSpec);  
  18.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  19.         if (mPercentLayoutHelper.handleMeasuredStateTooSmall())  
  20.         {  
  21.             super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  22.         }  
  23.     }  
  24.   
  25.     @Override  
  26.     protected void onLayout(boolean changed, int l, int t, int r, int b)  
  27.     {  
  28.         super.onLayout(changed, l, t, r, b);  
  29.         mPercentLayoutHelper.restoreOriginalParams();  
  30.     }  
  31.   
  32.     @Override  
  33.     public LayoutParams generateLayoutParams(AttributeSet attrs)  
  34.     {  
  35.         return new LayoutParams(getContext(), attrs);  
  36.     }  
  37.   
  38.   
  39.     public static class LayoutParams extends LinearLayout.LayoutParams  
  40.             implements PercentLayoutHelper.PercentLayoutParams  
  41.     {  
  42.         private PercentLayoutHelper.PercentLayoutInfo mPercentLayoutInfo;  
  43.   
  44.         public LayoutParams(Context c, AttributeSet attrs)  
  45.         {  
  46.             super(c, attrs);  
  47.             mPercentLayoutInfo = PercentLayoutHelper.getPercentLayoutInfo(c, attrs);  
  48.         }  
  49.   
  50.         @Override  
  51.         public PercentLayoutHelper.PercentLayoutInfo getPercentLayoutInfo()  
  52.         {  
  53.             return mPercentLayoutInfo;  
  54.         }  
  55.   
  56.         @Override  
  57.         protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr)  
  58.         {  
  59.             PercentLayoutHelper.fetchWidthAndHeight(this, a, widthAttr, heightAttr);  
  60.         }  
  61.   
  62.         public LayoutParams(int width, int height) {  
  63.             super(width, height);  
  64.         }  
  65.   
  66.   
  67.         public LayoutParams(ViewGroup.LayoutParams source) {  
  68.             super(source);  
  69.         }  
  70.   
  71.         public LayoutParams(MarginLayoutParams source) {  
  72.             super(source);  
  73.         }  
  74.   
  75.     }  
  76.   
  77. }  

     
     
  1. <com.example.administrator.singleinstance.PercentLinearLayout  
  2.     xmlns:android=”http://schemas.android.com/apk/res/android”  
  3.     xmlns:tools=”http://schemas.android.com/tools”  
  4.     xmlns:app=”http://schemas.android.com/apk/res-auto”  
  5.     android:orientation=”vertical”  
  6.     android:id=”@+id/activity_main”  
  7.     android:layout_width=”match_parent”  
  8.     android:layout_height=”match_parent”  
  9.     tools:context=”com.example.administrator.singleinstance.MainActivity”>  
  10.   
  11.     <TextView  
  12.         android:id=”@+id/left”  
  13.         android:layout_width=”0dp”  
  14.         android:layout_height=”0dp”  
  15.         android:background=”@color/colorPrimaryDark”  
  16.         app:layout_heightPercent=”30%”  
  17.         app:layout_aspectRatio=”150%”  
  18.         />  
  19.     <TextView  
  20.         android:layout_width=”0dp”  
  21.         android:layout_height=”0dp”  
  22.         android:background=”@color/colorAccent”  
  23.         app:layout_heightPercent=”30%”  
  24.         app:layout_widthPercent=”30%”  
  25.         />  
  26.   
  27.     <TextView  
  28.         android:layout_width=”0dp”  
  29.         android:layout_height=”0dp”  
  30.         android:background=”#000”  
  31.         app:layout_marginTopPercent=”10%”  
  32.         app:layout_marginLeftPercent=”25%”  
  33.         app:layout_heightPercent=”30%”  
  34.         app:layout_widthPercent=”50%”  
  35.         android:id=”@+id/textView” />  
  36.   
  37. </com.example.administrator.singleinstance.PercentLinearLayout>  

基本使用也就这么多了,给个git的项目地址:https://github.com/JulienGenoud/android-percent-support-lib-sample

参考:Android高级进阶 –作者:顾浩鑫

http://www.open-open.com/lib/view/open1435563531716.html  国外博客

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值