Most visited

Recently visited

PercentLayoutHelper

public class PercentLayoutHelper
extends Object

java.lang.Object
   ↳ android.support.percent.PercentLayoutHelper


Helper用于支持基于百分比的尺寸的布局。

该类收集涉及提取基于百分比的维度属性并将其应用于ViewGroup的子项的实用程序方法。 如果您想要实现支持基于百分比的维度的布局,则需要执行以下几个步骤:

  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(Context, AttributeSet). Return this object from public PercentLayoutHelper.PercentLayoutInfo getPercentLayoutInfo() method that you implemented for PercentLayoutHelper.PercentLayoutParams interface.
  3. Override setBaseAttributes(TypedArray, int, int) with a single line implementation PercentLayoutHelper.fetchWidthAndHeight(this, a, widthAttr, heightAttr);
  4. In your ViewGroup override generateLayoutParams(AttributeSet) to return your LayoutParams.
  5. In your 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);
         }
     }
     
  6. In your 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();
     }
     

Summary

Nested classes

class PercentLayoutHelper.PercentLayoutInfo

容器中有关百分比尺寸和边距的信息。

interface PercentLayoutHelper.PercentLayoutParams

如果布局想要支持基于百分比的尺寸并使用此辅助类,则其LayoutParams子类必须实现此接口。

Public constructors

PercentLayoutHelper(ViewGroup host)

Public methods

void adjustChildren(int widthMeasureSpec, int heightMeasureSpec)

迭代孩子并将其宽度和高度更改为从百分比值计算得出的值。

static void fetchWidthAndHeight(ViewGroup.LayoutParams params, TypedArray array, int widthAttr, int heightAttr)

将从 setBaseAttributes(TypedArray, int, int)覆盖的帮助器方法读取layout_width和layout_height属性值,如果它们不存在则不抛出异常。

static PercentLayoutHelper.PercentLayoutInfo getPercentLayoutInfo(Context context, AttributeSet attrs)

根据与视图关联的属性构造PercentLayoutInfo。

boolean handleMeasuredStateTooSmall()

迭代孩子并检查他们中的任何人是否想要获得比通过百分比维度所获得的更多空间。

void restoreOriginalParams()

迭代孩子并恢复为百分比值更改的原始尺寸。

Inherited methods

From class java.lang.Object

Public constructors

PercentLayoutHelper

PercentLayoutHelper (ViewGroup host)

Parameters
host ViewGroup

Public methods

adjustChildren

void adjustChildren (int widthMeasureSpec, 
                int heightMeasureSpec)

迭代孩子并将其宽度和高度更改为从百分比值计算得出的值。

Parameters
widthMeasureSpec int: Width MeasureSpec of the parent ViewGroup.
heightMeasureSpec int: Height MeasureSpec of the parent ViewGroup.

fetchWidthAndHeight

void fetchWidthAndHeight (ViewGroup.LayoutParams params, 
                TypedArray array, 
                int widthAttr, 
                int heightAttr)

将从 setBaseAttributes(TypedArray, int, int)覆盖的帮助器方法会读取layout_width和layout_height属性值,如果它们不存在则不抛出异常。

Parameters
params ViewGroup.LayoutParams
array TypedArray
widthAttr int
heightAttr int

getPercentLayoutInfo

PercentLayoutHelper.PercentLayoutInfo getPercentLayoutInfo (Context context, 
                AttributeSet attrs)

根据与视图关联的属性构造PercentLayoutInfo。 LayoutParams(Context c, AttributeSet attrs)构造函数调用此方法。

Parameters
context Context
attrs AttributeSet
Returns
PercentLayoutHelper.PercentLayoutInfo

handleMeasuredStateTooSmall

boolean handleMeasuredStateTooSmall ()

迭代孩子并检查他们中的任何人是否想要获得比通过百分比维度所获得的更多空间。 如果您正在构建支持百分比维度的布局,则建议您利用此方法。 开发人员应该能够指定通过添加wrap_content值的正常尺寸属性来重新测量孩子。 例如,他可能会将孩子的属性指定为app:layout_widthPercent="60%p"android:layout_width="wrap_content" 在这种情况下,如果孩子收到太少的空间,它将被重新测量,宽度设置为WRAP_CONTENT

Returns
boolean True if the measure phase needs to be rerun because one of the children would like to receive more space.

restoreOriginalParams

void restoreOriginalParams ()

迭代孩子并恢复为百分比值更改的原始尺寸。 如果您以前称为adjustChildren(int, int)调用此方法才有意义。

Hooray!