1,

tvtest.post(new Runnable() {

@Override

public void run() {

Toast.makeText(MainActivity.this, tvtest.getWidth() +"--" +tvtest.getHeight(), Toast.LENGTH_SHORT).show();

}

});

2,

ViewTreeObservervto2 = ll_sticky_header.getViewTreeObserver();

vto2.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

@Override

public void onGlobalLayout() {

heightHeader = ll_sticky_header.getMeasuredHeight();

}});

View.post()中的run方法和onWindowFocusChanged方法几乎是同时执行,都是在onLayout之后,所以都是可以得到控件的宽高的!!!

3,

@Override

public void onWindowFocusChanged(boolean hasFocus) {

super.onWindowFocusChanged(hasFocus);

mWidth = mTextView.getWidth();

mHeight = mTextView.getHeight();

Log.d(TAG, "onWindowFocusChanged: width = " + mWidth + "  height = " + mHeight);

}

oncreate()→onResume()→onMeasure()→onLayout()→onWidnowFocusChanged()→.....→onDraw()...

4,自定义view

onMeasure()完成才能通过getMeasuredHeight()和getMeasuredWidth()获取宽高

onLayout()完成才能通过getHeight()和getWidth()获取宽高

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐