我意识到堆栈溢出(getWidth() and getHeight() of View returns 0)上存在类似的问题,引起了很多关注。好像我可能过早地调用了getWidth,但是我怎么能确定它确实有一个实际的宽度呢?我很担心,因为我认为我的大小为零,因为我正在实现一个imageBitmap而不是一个imageResource。
我的onCreate:
// Byte array to bitmap
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
mainImageBitmap = BitmapFactory.decodeByteArray(byteArray, 0,
byteArray.length, options);
// Set bitmap
mainImageView.setImageBitmap(mainImageBitmap);
Log.e("", mainImageView.getWidth() + " ");
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(400, 400); //Want to use getWidth and getHeight here, just hardcoding to 400 for now
layout.setLayoutParams(params);
答案 0 :(得分:2)
示例:
将全局布局附加到您想要的视图 当视图准备就绪时,它将使用宽度和高度
回调此函数在您的情况下尝试使用下一个代码
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);