计算布局后如何获取 Android 小部件的大小?

发布于 2024-09-01 18:05:34 字数 658 浏览 11 评论 0原文

我有一个布局,它以相对尺寸指定小部件的大小,例如:

<LinearLayout ... layout_height="fill_parent">
     <ImageView ... layout_height="wrap_content" />
     <TextView ... layout_height="120dp" />
</LinearLayout>

onCreate 之后,我想知道 ImageView 的高度是多少。怎么做呢?

注意:如果我在onCreate中调用getHeight(),我会得到0。

我也尝试过imageView.postDelayed ,它可以在 2.1 模拟器上运行,但在 1.5 模拟器上失败(我也有 0)。

最后,我尝试创建一个 Handler,然后以 10 毫秒的延迟调用 handler.postDelayed。它适用于 2.1 和 1.5 模拟器,但当我在 eclipse 调试器中启动程序时失败(因此,我得出的结论是,使用延迟并不能保证在对 imageview 进行布局之后获取高度。)

I have a layout which specifies sizes of widgets in relative dimension, for example:

<LinearLayout ... layout_height="fill_parent">
     <ImageView ... layout_height="wrap_content" />
     <TextView ... layout_height="120dp" />
</LinearLayout>

Immediately after onCreate, I want to know how much is the height of the ImageView. How to do that?

Note: If I call getHeight() in onCreate, I get 0.

I have also tried imageView.postDelayed, it works on 2.1 emulator but fails on 1.5 emulator (I got 0, too).

Finally, I tried to create a Handler, and then I call handler.postDelayed with 10 ms delay. It works in both 2.1 and 1.5 emulator, but failed when I start the program in eclipse debugger (So, I conclude that using the delay doesn't guarantee that the getting of the height happens after the imageview is layout-ted.)

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

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

发布评论

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

评论(2

苦妄 2024-09-08 18:05:34

大小为 0 的原因是,直到活动完全创建后布局才完成,即 onCreate()、onStart() 和 onResume() 都已完成。据我所知,获得准确大小的最简单方法是在布局完成后调用您的方法,例如在按钮上使用单击侦听器。由于按钮在布局完成之前不会显示,因此在触发其单击侦听器时,尺寸必须可用。

这只是一个猜测,但我想这很难精确地做到,因为他们不希望人们在系统刚刚完成屏幕布局后就弄乱布局尺寸。如果他们提供了“onLayoutFinished()”回调,那么如果您修改了其中的布局,您可能会陷入循环。例如,想象一下:布局完成;调用 onLayoutFinished() 并修改其中的布局;现有布局现在无效;重新布局; onLayoutFinished() 再次调用;您的代码再次被调用 - 等等。

另一种方法是创建一个自定义视图并重写 onMeasure(int, int) 方法。系统触发该方法获取每个View的大小;如果您使用类似下面的示例的内容,您可以在布局完成之前获得建议的尺寸:(

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    //getMeasuredHeight() and getMeasuredWidth() now contain the suggested size
}

我写道这是建议的尺寸,因为我认为在此基础上之后尺寸可能会改变然而,这是我不久前读过的一个模糊的记忆,我从未详细尝试过。)一旦你完成了这一点,你就可以使用这个大小来做任何你想做的事情 - 你甚至可以使用 setMeasuredDimension(newX, newY) 更改它将使用的大小。

The reason you're getting a size of 0 is that the layout isn't finished until after the activity is fully created, i.e. onCreate(), onStart() and onResume() have all gone through. The easiest way I know of to get the exact size is to call your method after the layout has finished, such as by using a click listener on a button. Since the button isn't displayed until the layout is finished, the size must be available by the time its click listener is fired.

This is only a guess, but I imagine that this is difficult to do precisely because they don't want people messing with layout sizes once the system has just finished laying out the screen. If they provided a "onLayoutFinished()" callback, then you could get yourself stuck in a loop if you modified the layout in that. For example, imagine: layout is completed; onLayoutFinished() called and you modify the layout in there; the existing layout is now invalid; layout re-done; onLayoutFinished() called again; your code gets called again - and so forth.

Another way to do it is to make a custom View and override the onMeasure(int, int) method. The system triggers that method to get the size of each View; if you use something like my example below, you can get the suggested size before the layout is finished:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    //getMeasuredHeight() and getMeasuredWidth() now contain the suggested size
}

(I wrote that it's the suggested size because I think it's possible for the size to be changed after this based on layout constraints. However, that's a vague memory of something I read a while ago, and I never experimented in detail.) Once you've done that, you can use the size for whatever it is you wanted to do - you can even change the size it will use by using setMeasuredDimension(newX, newY).

塔塔猫 2024-09-08 18:05:34

View 对象提供了名为 onSizeChanged(int w, int h, int oldw, int oldh) 的方法。当布局更改其组件之一的大小时会调用它。如果您重写此方法,则此方法版本中的代码将知道其自身的大小,如以下代码片段所示。

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    bounds = new Rect();
    this.getDrawingRect(bounds);
    // bounds will now contain none zero values
}

The View object provides method called onSizeChanged(int w, int h, int oldw, int oldh). It is called when the layout changes the size of one of its components. If you override this method then the the code in your version of this method will know the size of itself as shown in the following snippet.

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    bounds = new Rect();
    this.getDrawingRect(bounds);
    // bounds will now contain none zero values
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文