android 百分比控件的使用
來自: http://blog.csdn.net/cheyiliu/article/details/46770375
概述
- 谷歌官方推出這個百分比庫對android的屏幕適配肯定有很大的幫助,當然具體好不好用還得根據不同的使用場景來分析。
- 這個支持包里的內容有:百分比相對布局PercentRelativeLayout,百分比幀布局PercentFrameLayout,百分比線性布局PercentLinearLayout。
先跑demo
- 趕緊來嘗鮮吧。我先對原來的工程做了拆分:
- lib庫工程,僅包含百分比控件的源代碼和必要的資源。完整代碼路徑:https://github.com/cheyiliu/test4XXX/tree/master/androidSupportPercent
- 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
</ul> </div>
本文由用戶 anng1241 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!