如何获得一个Android窗口小部件的尺寸布局的计算后? [英] How to get an Android widget's size after layout is calculated?

查看:407
本文介绍了如何获得一个Android窗口小部件的尺寸布局的计算后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

紧接着的onCreate ,我想知道有多少是ImageView的高度。那怎么办?

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

注意::如果我称之为的getHeight()的onCreate ,我得到0

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

我也尝试 imageView.postDelayed ,它可以在2.1模拟器,但无法对1.5模拟器(我得到0,太)。

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

最后,我试图创建一个处理程序,然后我叫 handler.postDelayed 10毫秒的延迟。它可以在两个2.1和1.5模拟器,但未能当我开始在Eclipse中调试程序(所以,我的结论是使用迟迟不保证高度的获得发生后ImageView的是布局特德。)

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.)

推荐答案

你得到的0大小的原因是,布局还没有完成,直到活动被完全创建后,即的onCreate(),ONSTART( )和onResume()都经历过。最简单的方法,我知道的,以得到确切大小后的布局已经完成,如通过使用一个点击监听器上的一个按钮来调用你的方法。由于不显示按钮,直到布局完成后,大小必须可以由它的点击监听器被触发的时间。

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.

这只是一种猜测,但我想,这是很难做到的precisely,因为他们不想让人们与布局尺寸搞乱一旦系统刚刚完成布局的屏幕。如果他们提供了一个onLayoutFinished()回调,那么你可以让自己停留在一个循环,如果你修改了布局的。例如,假设:布局完成; onLayoutFinished()调用,修改布局在那里;现有的布局现在是无效的;布局重新进行; onLayoutFinished()再次调用;您的code被再次呼吁 - 等等

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.

另一种方式做到这一点是让一个自定义的视图,并覆盖 onMeasure(INT,INT)方法。该系统将触发该方法获取每个视图的大小;如果你使用的东西,像我下面的例子中,你可以得到的建议的布局尺寸前完成:

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
}

(我写的是建议的大小,因为我认为这是可能的大小后,此基础上布局约束改变。然而,这东西我前一阵子看了模糊的记忆,我从来没有尝试的细节。 )一旦你做到了这一点,你可以使用什么是你想做的大小 - 你甚至可以改变它会使用 setMeasuredDimension(下一页末,newY)

这篇关于如何获得一个Android窗口小部件的尺寸布局的计算后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆