Use of the android percentage control and the android percentage control
Overview
- Google's official release of this percentage library will certainly be of great help to android screen adaptation. Of course, the specific poor performance must be analyzed based on different use cases.
- The content in this support package includes PercentRelativeLayout, PercentFrameLayout, and PercentLinearLayout.
Run demo first
- Come and have a taste. I split the original project first:
Code Analysis and library implementation
- Custom Attributes. Supported attributes include: percentage width and percentage 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>
- Inherit existing controls and apply custom properties. The layout and drawing of the android view should roughly go through several steps of onMeasure onLayout onDraw, so the application of the percentage attribute must be in the onMeasure process. Take PercentRelativeLayout as an example.
1. The class PercentLayoutInfo is used to store custom percentage attributes, which can be understood as the percentage Attribute model. 2. The LayoutParams class inherits the original layoutparam class and holds the model class of the percentage attribute. Assign a value to the model during construction. 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. apply the model of the percentage attribute in onMeasure. The helper code will not be expanded. @ Override protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {mHelper. adjustChildren (widthMeasureSpec, heightMeasureSpec); super. onMeasure (widthMeasureSpec, heightMeasureSpec); if (mHelper. handlemeasuredstat1_small () {super. onMeasure (widthMeasureSpec, heightMeasureSpec );}}
Usage Library
- First, establish the dependency with the database project
- Use
<?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>
More references
- 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
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.