關於自定義Layout或者ViewGroup總結

2021-09-11 14:15:59 字數 2341 閱讀 6808

先看一下**,然後我會根據**具體分析:

package com.yld.startvideodemo;

import android.content.context;

import android.util.attributeset;

import android.view.view;

import android.view.viewgroup;

/** * @author yld

* @create 2019-02-28 9:33

*/public

class

customlayout

extends

viewgroup

public

customlayout

(context context, attributeset attrs)

public

customlayout

(context context, attributeset attrs,

int defstyleattr)

/** *widthmeasurespec 包含了了父view的width屬性的模式和尺寸

*heightmeasurespec 包含了了父view的height屬性的模式和尺寸

*/@override

protected

void

onmeasure

(int widthmeasurespec,

int heightmeasurespec)

else

break

;case layoutparams.wrap_content:

if(widthmode == measurespec.exactly&&widthmode==measurespec.at_most)

else

break

;default

: childwidthspec = measurespec.

makemeasurespec

(layoutparams.width,measurespec.exactly )

;break;}

switch

(layoutparams.height)

else

break

;case layoutparams.wrap_content:

if(heightmode == measurespec.exactly&&heightmode==measurespec.at_most)

else

break

;default

: childheightspec = measurespec.

makemeasurespec

(layoutparams.width,measurespec.exactly )

;break;}

//下面的**需要驗證以後方可使用

usedwidth = measurespec.

getsize

(childwidthspec)

; usedheight = measurespec.

getsize

(childheightspec)

;setmeasureddimension

(childwidthspec,childheightspec);}

}@override

protected

void

onlayout

(boolean changed,

int l,

int t,

int r,

int b)

}}

從**中可以看到主要是重寫了onmeasure()and onlayout()兩個方法

1.其中在onmeasure()方法中通過遍歷父view中所包含的子view,然後對其進行測量

2.對子view進行測量時通過以下方法:

measurespec.makemeasurespec(int measuresize,int measuremode );
在方法中傳入計算結果並為其指定測量模式。

3.最後將測量結果進行暫時儲存。

4.在onlayout()方法中通過遍歷父view中所包含的子view,通過以下**:

childview.layout(int left,int top,int right,int bottom);
根據自己的需求指定子view的顯示位置.

Android 自定義layout 無法顯示

布局 引入自定義layout如下所示 在執行時無法顯示,用ui automator抓取看,自定義控制項繪製上去了,但是沒有效果 如果是這種情況,就要看自定義layout是否是利用inflate的方式動態載入出來的,原寫法 view view.inflate context,r.layout.comm...

yaf 2 layout 自定義目錄結構

2013年3月19日 08 30 45 1call bootstrap methods defined in bootstrap.php23 效果 方案1 在呼叫bootstrap時寫hook函式 方案2 在入口檔案中寫類似上邊的3行 達到的結果是 root modules modulename c...

自定義layout中需要重寫的方法

preparelayout 準備所有view的layoutattribute資訊 collectionviewcontentsize 計算contentsize,顯然這一步得在preparelayout之後進行 layoutattributesforelementsinrect 返回在可見區域的vi...