Most visited

Recently visited

PercentRelativeLayout

public class PercentRelativeLayout
extends RelativeLayout

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.RelativeLayout
         ↳ android.support.percent.PercentRelativeLayout


支持基于百分比的尺寸和边距的子类RelativeLayout 您可以通过使用带有“后缀百分比”的属性来指定维度或边缘。 遵循这个例子:

 <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">
     <ImageView
         app:layout_widthPercent="50%"
         app:layout_heightPercent="50%"
         app:layout_marginTopPercent="25%"
         app:layout_marginLeftPercent="25%"/>
 </android.support.percent.PercentFrameLayout>
 
The attributes that you can use are: It is not necessary to specify layout_width/height if you specify layout_widthPercent. However, if you want the view to be able to take up more space than what percentage value permits, you can add layout_width/height="wrap_content". In that case if the percentage size is too small for the View's content, it will be resized using wrap_content rule.

您还可以通过仅设置宽度或高度使一个维度成为另一维度的一部分,并且使用layout_aspectRatio自动计算第二维度的维度。 例如,如果你想达到16:9的宽高比,你可以写:

     android:layout_width="300dp"
     app:layout_aspectRatio="178%"
 
This will make the aspect ratio 16:9 (1.78:1) with the width fixed at 300dp and height adjusted accordingly.

Summary

Nested classes

class PercentRelativeLayout.LayoutParams

 

Inherited XML attributes

From class android.widget.RelativeLayout
From class android.view.ViewGroup
From class android.view.View

Inherited constants

From class android.widget.RelativeLayout
From class android.view.ViewGroup
From class android.view.View

Inherited fields

From class android.view.View

Public constructors

PercentRelativeLayout(Context context)
PercentRelativeLayout(Context context, AttributeSet attrs)
PercentRelativeLayout(Context context, AttributeSet attrs, int defStyle)

Public methods

PercentRelativeLayout.LayoutParams generateLayoutParams(AttributeSet attrs)

根据提供的属性集返回一组新的布局参数。

Protected methods

PercentRelativeLayout.LayoutParams generateDefaultLayoutParams()

返回一组宽度为 WRAP_CONTENT ,高度为 WRAP_CONTENT且无跨越的布局参数。

void onLayout(boolean changed, int left, int top, int right, int bottom)

当这个视图为每个孩子分配一个大小和位置时,从布局调用。

void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

测量视图及其内容以确定测量宽度和测量高度。

Inherited methods

From class android.widget.RelativeLayout
From class android.view.ViewGroup
From class android.view.View
From class java.lang.Object
From interface android.view.ViewParent
From interface android.view.ViewManager
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.accessibility.AccessibilityEventSource

Public constructors

PercentRelativeLayout

PercentRelativeLayout (Context context)

Parameters
context Context

PercentRelativeLayout

PercentRelativeLayout (Context context, 
                AttributeSet attrs)

Parameters
context Context
attrs AttributeSet

PercentRelativeLayout

PercentRelativeLayout (Context context, 
                AttributeSet attrs, 
                int defStyle)

Parameters
context Context
attrs AttributeSet
defStyle int

Public methods

generateLayoutParams

PercentRelativeLayout.LayoutParams generateLayoutParams (AttributeSet attrs)

根据提供的属性集返回一组新的布局参数。

Parameters
attrs AttributeSet: the attributes to build the layout parameters from
Returns
PercentRelativeLayout.LayoutParams an instance of ViewGroup.LayoutParams or one of its descendants

Protected methods

generateDefaultLayoutParams

PercentRelativeLayout.LayoutParams generateDefaultLayoutParams ()

返回一组宽度为 WRAP_CONTENT ,高度为 WRAP_CONTENT且没有跨越的布局参数。

Returns
PercentRelativeLayout.LayoutParams a set of default layout parameters or null

onLayout

void onLayout (boolean changed, 
                int left, 
                int top, 
                int right, 
                int bottom)

当这个视图为每个孩子分配一个大小和位置时,从布局调用。 带孩子的派生类应该覆盖这个方法,并调用他们每个孩子的布局。

Parameters
changed boolean: This is a new size or position for this view
left int: Left position, relative to parent
top int: Top position, relative to parent
right int: Right position, relative to parent
bottom int: Bottom position, relative to parent

onMeasure

void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

测量视图及其内容以确定测量宽度和测量高度。 此方法由measure(int, int)调用, measure(int, int)子类覆盖以提供其内容的准确和有效的度量。

合同:覆盖此方法时,您必须致电setMeasuredDimension(int, int)来存储此视图的测量宽度和高度。 不这样做会触发IllegalStateException ,由measure(int, int)引发。 调用超类' onMeasure(int, int)是一种有效的用法。

Measure的基类实现默认为背景大小,除非MeasureSpec允许更大的大小。 子类应覆盖onMeasure(int, int)以提供更好的内容度量。

如果此方法被覆盖,则子类的责任是确保测量的高度和宽度至少为视图的最小高度和宽度( getSuggestedMinimumHeight()getSuggestedMinimumWidth() )。

Parameters
widthMeasureSpec int: horizontal space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.
heightMeasureSpec int: vertical space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.

Hooray!