如何获取某个布局(如LinearLayout或RelativeLayout)的测量值,并将其布局宽度和布局高度设置为包装内容?使用这种方法
layout.measure(MeasureSpec.makeMeasureSpec(0,
MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0,
MeasureSpec.UNSPECIFIED));
layout.layout(0, 0,
layout.getMeasuredWidth(),
layout.getMeasuredHeight());
返回空值。我正试着用relativelayout创建一个位图图像,有点像截图。
最佳答案
不要对oncreate()方法执行此操作。试试这个:
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus == true) {
LinearLayout myLinearLayout = (LinearLayout) findViewById(R.id.myLinearLayout);
int layoutWidth = myLinearLayout.getWidth();
int layoutHeight = myLinearLayout.getHeight();
}
}
当大小已知时,在oncreate()之后调用此方法。