我有一个LinearLayout
内部LinearLayout
,孩子linearlayout
有它的layout params
位置height
,width
定义为
LinearLayout linearLayout=new LinearLayout(context);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(50,50);
linearLayout.setLayoutParams(lp);
现在视图中创建后,我试图从获得一个视图的高度getHeight()
和getMeasuredHeight()
,但是这两种方法返回0。?
int heightofView=linearLayout.getHeight();
int heightofview=linearLayout.getMeasuredHeight();
为什么这两种方法都返回0的视图高度?这些方法何时返回实际结果?如何获得我创建的视图的高度?
试试这个代码。
imageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int width = imageView.getWidth();
int height = imageView.getHeight();
//you can add your code here on what you want to do to the height and width you can pass it as parameter or make width and height a global variable
imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句