其他分享
首页 > 其他分享> > android – 获取RelativeLayout维度

android – 获取RelativeLayout维度

作者:互联网

如何将layout_width和layout_height设置为wrap_content来获取某个布局(如linearlayout或relativelayout)的测量值?使用这种方法

 layout.measure(MeasureSpec.makeMeasureSpec(0,
            MeasureSpec.UNSPECIFIED),
            MeasureSpec.makeMeasureSpec(0,
                    MeasureSpec.UNSPECIFIED));

 layout.layout(0, 0,
            layout.getMeasuredWidth(),
            layout.getMeasuredHeight());

返回null.我正试图从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()已知大小时,将调用此方法.

标签:android,android-layout,android-image
来源: https://codeday.me/bug/20190827/1735810.html