获取RelativeLayout尺寸

发布于 2024-11-07 07:23:07 字数 428 浏览 0 评论 0原文

如何获取特定布局(如线性布局或相对布局)的测量值,并将其layout_width和layout_height设置为wrap_content?使用此方法

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

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

返回 null。我正在尝试从相对布局中创建位图图像,有点像屏幕截图。

How do I get the measurements of a certain layout like linearlayout or relativelayout with its layout_width and layout_height set to wrap_content? using this method

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

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

returns null. I'm trying to create a bitmap image out of the relativelayout, kinda like a screenshot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱*していゐ 2024-11-14 07:23:07

不要在 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() 之后调用此方法。

Don't do it on the onCreate() method. Try this:

@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();
    }
}

This method is called after onCreate(), when the sizes are already known.

自在安然 2024-11-14 07:23:07

您可以在视图上使用 getMeasuredWidth() 和 getMeasuredHeight() 。

请记住,必须首先布局视图。你只是不能在 onCreate 中执行此操作,因为尺寸尚不清楚。你必须等到它已经布局。

You can use getMeasuredWidth() and getMeasuredHeight() on your view.

Remember the view has to have been layout first. You just CANNOT do it in onCreate, because the sizes are not known yet. You have to wait until it has been layout.

oО清风挽发oО 2024-11-14 07:23:07

公共类 AndroidResizeView 扩展 Activity {

TextView textMyTextView;
Button myButton;

RelativeLayout rlayout;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.hightk);

    rlayout = (RelativeLayout) findViewById(R.id.rlayout);

    textMyTextView = (TextView) findViewById(R.id.textView1);

    myButton = (Button) findViewById(R.id.button1);

    myButton.setOnClickListener(new Button.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            updateSizeInfo();
        }
    });
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    // TODO Auto-generated method stub
    super.onWindowFocusChanged(hasFocus);
    updateSizeInfo();
}

private void updateSizeInfo() {

    textMyTextView.setText(String.valueOf("Width: "
            + rlayout.getWidth() + "height ::" + rlayout.getHeight()));

}

}

public class AndroidResizeView extends Activity {

TextView textMyTextView;
Button myButton;

RelativeLayout rlayout;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.hightk);

    rlayout = (RelativeLayout) findViewById(R.id.rlayout);

    textMyTextView = (TextView) findViewById(R.id.textView1);

    myButton = (Button) findViewById(R.id.button1);

    myButton.setOnClickListener(new Button.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            updateSizeInfo();
        }
    });
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    // TODO Auto-generated method stub
    super.onWindowFocusChanged(hasFocus);
    updateSizeInfo();
}

private void updateSizeInfo() {

    textMyTextView.setText(String.valueOf("Width: "
            + rlayout.getWidth() + "height ::" + rlayout.getHeight()));

}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文