概述

  • 谷歌官方推出这个百分比库对android的屏幕适配肯定有很大的帮助,当然具体好不好用还得根据不同的使用场景来分析。
  • 这个支持包里的内容有:百分比相对布局PercentRelativeLayout,百分比帧布局PercentFrameLayout,百分比线性布局PercentLinearLayout。

先跑demo

  • 赶紧来尝鲜吧。我先对原来的工程做了拆分:
    1. lib库工程,仅包含百分比控件的源代码和必要的资源。完整代码路径:https://github.com/cheyiliu/test4XXX/tree/master/androidSupportPercent
    2. demo工程(基于百分比库),同时去掉了原工程对v7 support包的依赖,完整路径: https://github.com/cheyiliu/test4XXX/tree/master/test4androidSupportPercentLayout

代码分析和使用

库的实现

  • 自定义属性,支持的属性包括:百分比宽高和百分比margin
<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="PercentLayout_Layout">        <attr name="layout_widthPercent" format="string" />        <attr name="layout_heightPercent" format="string" />        <attr name="layout_marginPercent" format="string" />        <attr name="layout_marginLeftPercent" format="string" />        <attr name="layout_marginTopPercent" format="string" />        <attr name="layout_marginRightPercent" format="string" />        <attr name="layout_marginBottomPercent" format="string" />        <attr name="layout_marginStartPercent" format="string" />        <attr name="layout_marginEndPercent" format="string" />    </declare-styleable></resources>
  • 继承现有控件并应用自定义属性。android view的布局和绘制要大致经历onMeasure onLayout onDraw几步,那百分比属性的应用肯定是在onMeasure过程中了。以PercentRelativeLayout为例简单过下代码。
1.类PercentLayoutInfo用来存储自定义的百分比属性,可理解为百分比属性model。2.类LayoutParams,继承原有的layoutparam并持有百分比属性model类。在构造时完成对model的赋值。    public static class LayoutParams extends RelativeLayout.LayoutParams implements PercentLayoutHelper.PercentLayoutParams {        private PercentLayoutHelper.PercentLayoutInfo mPercentLayoutInfo;        public LayoutParams(Context c, AttributeSet attrs) {            super(c, attrs);            mPercentLayoutInfo = PercentLayoutHelper.getPercentLayoutInfo(c, attrs);        }3.在onMeasure中完成对百分比属性model的应用,helper的代码就不展开看了。    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        mHelper.adjustChildren(widthMeasureSpec, heightMeasureSpec);        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        if (mHelper.handleMeasuredStateTooSmall()) {            super.onMeasure(widthMeasureSpec, heightMeasureSpec);        }    }

使用库

  • 首先建立和库工程的依赖关系
  • 在layout文件中使用
<?xml version="1.0" encoding="utf-8"?><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" >    <TextView  android:text="percent relative layout1" android:id="@+id/top_left" android:layout_width="0dp" android:layout_height="0dp" android:layout_alignParentTop="true" app:layout_heightPercent="20%" app:layout_widthPercent="70%" android:background="#ff44aacc" />    <View  android:id="@+id/top_right" android:layout_width="0dp" android:layout_height="0dp" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/top_left" app:layout_heightPercent="20%" app:layout_widthPercent="30%" android:background="#ffe40000" />    <View  android:id="@+id/bottom" android:layout_width="match_parent" android:layout_height="0dp" android:layout_below="@+id/top_left" app:layout_heightPercent="80%" android:background="#ff00ff22" /></android.support.percent.PercentRelativeLayout>

效果图



更多参考

  • https://github.com/cheyiliu/test4XXX/tree/master/test4androidSupportPercentLayout
  • https://github.com/cheyiliu/test4XXX/tree/master/androidSupportPercent
  • http://blog.csdn.net/lmj623565791/article/details/46695347
  • https://github.com/hongyangAndroid/android-percent-support-extend
  • https://github.com/JulienGenoud/android-percent-support-lib-sample

更多相关文章

  1. Android(安卓)机器人:使用系统资源
  2. 安卓NDK——原生开发工具包
  3. 【Android】显示网络图片代码分析
  4. Android(安卓)4.2 Bluetooth 分析总结(一)
  5. Android的视频通话的Java代码
  6. Android中怎么让你的layout适应屏幕的大小
  7. Android应用程序绑定服务(bindService)的过程源代码分析
  8. Android(安卓)ViewPager之实现轮播广告效果
  9. Android(安卓)Animation 动画的使用方法和介绍

随机推荐

  1. Android(安卓)SDK 的快速安装方法
  2. Android(安卓)消息处理机制1(从源码分析)
  3. Android实现程序前后台切换效果
  4. Android的无边界程序设计理念
  5. Android(安卓)Wi-Fi Ethernet新IP获取机
  6. Intellij IDEA + Android(安卓)SDK + Gen
  7. android控件布局
  8. Android数据存储---数据备份(Data Backup)(
  9. Android(安卓)NDK开发:NDK概览
  10. 精通Android