android 百分比控制項的使用,android百分比控制項
概述
- Google官方推出這個百分比庫對android的螢幕適配肯定有很大的協助,當然具體好不好用還得根據不同的使用情境來分析。
- 這個支援包裡的內容有:百分比相對布局PercentRelativeLayout,百分比幀布局PercentFrameLayout,百分比線性布局PercentLinearLayout。
先跑demo
程式碼分析和使用庫的實現
- 自訂屬性,支援的屬性包括:百分比寬高和百分比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
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。