getWidth()与getHeight()方法返回是0
上代码:
方法一:
int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
ssidtext.measure(w, h);
int width =ssidtext.getMeasuredWidth();
int height =ssidtext.getMeasuredHeight();
方法二:
ViewTreeObserver vto = ssidtext.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
int height = ssidtext.getMeasuredHeight();
int width = ssidtext.getMeasuredWidth();
return true;
}
});
另外还搜索到一个方法:
-----------------------------------------------------------
先贴到这里。正在做试验。(试验完毕,这种方法得到的控件宽是正确的。)
ViewTreeObserver vto = mArrowUp.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mArrowUp.getViewTreeObserver().removeGlobalOnLayoutListener(this);
Log.e("Width",Integer.toString(mArrowUp.getWidth()));
}
});
具体用哪一种可以根据需求自己选择
posted on 2012-06-14 18:33
汪杰 阅读(502)
评论(0) 编辑 收藏 引用 所属分类:
Java