android 百分比控件的使用

概述谷歌官方推出这个百分比库对android的屏幕适配肯定有很大的帮助,当然具体好不好用还得根据不同的使用场景来分析。这个支持包里的内容有:百分比相对布局PercentRelativeLayout,百分比帧布局PercentFrameLayout,百分比线性布局PercentLinearLayout。先跑demo赶紧来尝鲜吧。我先对原来的工程做了拆分: lib库工程,仅包含百分比控件的源代码和必要的资源。完整代码路径:https://github.com/cheyiliu/test4XXX/tree/master/androidSupportPercentdemo工程(基于百分比库),同时去掉了原工程对v7 support包的依赖,完整路径: https://github.com/cheyiliu/test4XXX/tree/master/test4androidSupportPercentLayout代码分析和使用库的实现自定义属性,支持的属性包括:百分比宽高和百分比margin==========></resources>继承现有控件并应用自定义属性。android view的布局和绘制要大致经历onMeasure onLayout onDraw几步,,那百分比属性的应用肯定是在onMeasure过程中了。以PercentRelativeLayout为例简单过下代码。1.类PercentLayoutInfo用来存储自定义的百分比属性,可理解为百分比属性model。2.类LayoutParams,继承原有的layoutparam并持有百分比属性model类。在构造时完成对model的赋值。..PercentLayoutParams {private PercentLayoutHelper.PercentLayoutInfo mPercentLayoutInfo;public LayoutParams(Context c, AttributeSet attrs) {super(c, attrs);mPercentLayoutInfo = PercentLayoutHelper.getPercentLayoutInfo(c, attrs);}3.在onMeasure中完成对百分比属性model的应用,helper的代码就不展开看了。(int widthMeasureSpec, int heightMeasureSpec) {mHelper.adjustChildren(widthMeasureSpec, heightMeasureSpec);super.onMeasure(widthMeasureSpec, heightMeasureSpec);if (mHelper.handleMeasuredStateTooSmall()) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);}}使用库首先建立和库工程的依赖关系在layout文件中使用==”match_parent”android:layout_height=”match_parent” ><TextView===”70%”android:background=”#ff44aacc” /><View===”30%”android:background=”#ffe40000″ /><View==>效果图

更多参考

命运掌握在自己手中

android 百分比控件的使用

相关文章:

你感兴趣的文章:

标签云: