在onCreate中由于控件还没绘制完成,所有获取到控件的宽高都是0。正确的方式是在onCreate中通过回调实现,如下:
progress=(ProgressBar) v.findViewById(R.id.progress);
progress.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
progress.getViewTreeObserver().removeGlobalOnLayoutListener(this);
LayoutParams params=progress.getLayoutParams();
Log.e("宽,高",params.width.+""+params.height)
}
});
本文详细介绍了在onCreate方法中获取控件宽高的难点及解决策略,通过回调实现获取控件的实际尺寸,适用于Android开发场景。
1196

被折叠的 条评论
为什么被折叠?



