Android layoutParams getWidth() getHeight()



这些方法在动态检索时返回正确的值。在onCreate()中,但唯一的例外是当xml布局文件中指定维度为未定义时,比如"包装内容"等等。是否有一种方法来检索这样的值??

这将返回正确的宽度和高度。onCreate在子视图布局完成之前被调用。所以宽度和高度还没有计算出来。得到高度和宽度。把它放到onCreate方法

LinearLayout layout = (LinearLayout)findViewById(R.id.YOUD VIEW ID);
ViewTreeObserver vto = layout.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
        hs.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
        int width  = layout.getMeasuredWidth();
        int height = layout.getMeasuredHeight(); 
    } 
});

最新更新