PercentLayoutHelper

Deprecated

Helper 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:

  1. You need a ViewGroup.LayoutParams subclass in your ViewGroup that implements PercentLayoutHelper.PercentLayoutParams.
  2. In your LayoutParams(Context c, AttributeSet attrs) constructor create an instance of PercentLayoutHelper.PercentLayoutInfo by calling getPercentLayoutInfo. Return this object from public PercentLayoutHelper.PercentLayoutInfo getPercentLayoutInfo() method that you implemented for PercentLayoutHelper.PercentLayoutParams interface.
  3. Override setBaseAttributes with a single line implementation PercentLayoutHelper.fetchWidthAndHeight(this, a, widthAttr, heightAttr);
  4. In your ViewGroup override generateLayoutParams to return your LayoutParams.
  5. In your onMeasure 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);
        }
    }
    
  6. In your onLayout 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();
    }
    

Deprecated

consider using ConstraintLayout and associated layouts instead. The following shows how to replicate the functionality of percentage layouts with a ConstraintLayout. The Guidelines are used to define each percentage break point, and then a Button view is stretched to fill the gap:

<android.support.constraint.ConstraintLayout        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">    <android.support.constraint.Guideline        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/left_guideline"        app:layout_constraintGuide_percent=".15"        android:orientation="vertical"/>    <android.support.constraint.Guideline        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/right_guideline"        app:layout_constraintGuide_percent=".85"        android:orientation="vertical"/>    <android.support.constraint.Guideline        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/top_guideline"        app:layout_constraintGuide_percent=".15"        android:orientation="horizontal"/>    <android.support.constraint.Guideline        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/bottom_guideline"        app:layout_constraintGuide_percent=".85"        android:orientation="horizontal"/>    <Button        android:text="Button"        android:layout_width="0dp"        android:layout_height="0dp"        android:id="@+id/button"        app:layout_constraintLeft_toLeftOf="@+id/left_guideline"        app:layout_constraintRight_toRightOf="@+id/right_guideline"        app:layout_constraintTop_toTopOf="@+id/top_guideline"        app:layout_constraintBottom_toBottomOf="@+id/bottom_guideline" /></android.support.constraint.ConstraintLayout>

Constructors

Link copied to clipboard
constructor(@NonNull host: ViewGroup)

Types

Link copied to clipboard
Container for information about percentage dimensions and margins.
Link copied to clipboard
If a layout wants to support percentage based dimensions and use this helper class, its LayoutParams subclass must implement this interface.

Functions

Link copied to clipboard
open fun adjustChildren(widthMeasureSpec: Int, heightMeasureSpec: Int)
Iterates over children and changes their width and height to one calculated from percentage values.
Link copied to clipboard
open fun fetchWidthAndHeight(params: ViewGroup.LayoutParams, array: TypedArray, widthAttr: Int, heightAttr: Int)
Helper method to be called from setBaseAttributes override that reads layout_width and layout_height attribute values without throwing an exception if they aren't present.
Link copied to clipboard
Constructs a PercentLayoutInfo from attributes associated with a View.
Link copied to clipboard
Iterates over children and checks if any of them would like to get more space than it received through the percentage dimension.
Link copied to clipboard
Iterates over children and restores their original dimensions that were changed for percentage values.