Class PercentLayoutHelper
- java.lang.Object
-
- com.batch.android.messaging.view.percent.PercentLayoutHelper
-
public class PercentLayoutHelper extends java.lang.ObjectHelper for layouts that want to support percentage based dimensions.This class collects utility methods that are involved in extracting percentage based dimension attributes and applying them to ViewGroup's children. If you would like to implement a layout that supports percentage based dimensions, you need to take several steps:
- You need a
ViewGroup.LayoutParamssubclass in your ViewGroup that implementsPercentLayoutHelper.PercentLayoutParams. - In your
LayoutParams(Context c, AttributeSet attrs)constructor create an instance ofPercentLayoutHelper.PercentLayoutInfo. Return this object frompublic PercentLayoutHelper.PercentLayoutInfo getPercentLayoutInfo()method that you implemented forPercentLayoutHelper.PercentLayoutParamsinterface. - Override
ViewGroup.LayoutParams.setBaseAttributes(TypedArray, int, int)with a single line implementationPercentLayoutHelper.fetchWidthAndHeight(this, a, widthAttr, heightAttr); - In your ViewGroup override
ViewGroup.generateLayoutParams(AttributeSet)to return your LayoutParams. - In your
View.onMeasure(int, int)override, you need to implement following pattern:protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { mHelper.adjustChildren(widthMeasureSpec, heightMeasureSpec); super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (mHelper.handleMeasuredStateTooSmall()) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } } - In your
ViewGroup.onLayout(boolean, int, int, int, int)override, you need to implement following pattern:protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); mHelper.restoreOriginalParams(); }
- You need a
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classPercentLayoutHelper.PercentLayoutInfoContainer for information about percentage dimensions and margins.static interfacePercentLayoutHelper.PercentLayoutParamsIf a layout wants to support percentage based dimensions and use this helper class, itsLayoutParamssubclass must implement this interface.
-
Constructor Summary
Constructors Constructor Description PercentLayoutHelper(android.view.ViewGroup host)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadjustChildren(int widthMeasureSpec, int heightMeasureSpec)Iterates over children and changes their width and height to one calculated from percentage values.static voidfetchWidthAndHeight(android.view.ViewGroup.LayoutParams params, android.content.res.TypedArray array, int widthAttr, int heightAttr)Helper method to be called fromViewGroup.LayoutParams.setBaseAttributes(android.content.res.TypedArray, int, int)override that reads layout_width and layout_height attribute values without throwing an exception if they aren't present.booleanhandleMeasuredStateTooSmall()Iterates over children and checks if any of them would like to get more space than it received through the percentage dimension.voidrestoreOriginalParams()Iterates over children and restores their original dimensions that were changed for percentage values.
-
-
-
Method Detail
-
fetchWidthAndHeight
public static void fetchWidthAndHeight(android.view.ViewGroup.LayoutParams params, android.content.res.TypedArray array, int widthAttr, int heightAttr)Helper method to be called fromViewGroup.LayoutParams.setBaseAttributes(android.content.res.TypedArray, int, int)override that reads layout_width and layout_height attribute values without throwing an exception if they aren't present.
-
adjustChildren
public void adjustChildren(int widthMeasureSpec, int heightMeasureSpec)Iterates over children and changes their width and height to one calculated from percentage values.- Parameters:
widthMeasureSpec- Width MeasureSpec of the parent ViewGroup.heightMeasureSpec- Height MeasureSpec of the parent ViewGroup.
-
restoreOriginalParams
public void restoreOriginalParams()
Iterates over children and restores their original dimensions that were changed for percentage values. Calling this method only makes sense if you previously calledadjustChildren(int, int).
-
handleMeasuredStateTooSmall
public boolean handleMeasuredStateTooSmall()
Iterates over children and checks if any of them would like to get more space than it received through the percentage dimension.If you are building a layout that supports percentage dimensions you are encouraged to take advantage of this method. The developer should be able to specify that a child should be remeasured by adding normal dimension attribute with
wrap_contentvalue. For example he might specify child's attributes asapp:layout_widthPercent="60%p"andandroid:layout_width="wrap_content". In this case if the child receives too little space, it will be remeasured with width set toWRAP_CONTENT.- Returns:
- True if the measure phase needs to be rerun because one of the children would like to receive more space.
-
-