捕捉布局API 11日之前调整 [英] Capture Layout resize before API 11

查看:74
本文介绍了捕捉布局API 11日之前调整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我动态创建一个委员会,由那些在相对布局动态设置的意见。在我的onCreate我把我的板类的构造函数创建的一切:

I am dynamically creating a board, made of views that are dynamically set in a relative layout. In my onCreate i call the constructor of my Board class that creates everything:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);
    RelativeLayout boardLayout = (RelativeLayout) findViewById(R.id.layoutBoard);
    board = new Board(this, boardLayout);

}

在我的主板I类设置的规则为图层,我还需要设置的显示我的意见的大小,我估计他们从 boardLayout.getWidth的值开始() boardLayout.getHeight()

In my Board class I set the rules for the layer and I also need to set the size of the views I display, and I calculate them starting from the values of boardLayout.getWidth() and boardLayout.getHeight()

现在的问题是,执行屏幕上的所有实际平局仅的所述onCreate方法的结束,所以在创建,该层的宽度和高度的时刻是零

Now the problem is that all the actual draw on the screen is performed only after the end of the onCreate method, so at the moment of the creation, the width and height of the layer are zero.

所以,我决定用设置大小:

So I decided to set the size using:

    boardLayout.addOnLayoutChangeListener(new RelativeLayout.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v, int left, int top, int right,
                int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                board.setParentLayoutSize(left, top, right, bottom);
        }
    });

不幸的是这种方法只能从API 11,我一定要坚持10 API作为一个最大值。 我怎么能执行呢?有没有办法听布局的变化,而无需使用这种方法吗?

Unfortunately this method is available only from API 11 and I must stick to API 10 as a maximum. How could I perform this? Is there a way to listen to layout changes without using this method?

感谢您!

推荐答案

最优雅的解决方案是扩展RelativeLayout的类,那么你可以只覆盖onSizeChanged:

The most elegant solution is to extend RelativeLayout class, then you can just override onSizeChanged:

protected void onSizeChanged (int w, int h, int oldw, int oldh)

如果您需要了解大小刚过充气,你可以尝试手动测量观点:这么叫措施(...),比布局(...),然后getMeasuredWidth()/ getMeasuredHeight应该返回正确的大小。但是,这是更靠谱,不要使用它,如果你不知道它是如何工作的。

If you need to know the size just after inflate you can try to measure view manually: so call measure(...) than layout(...) and then getMeasuredWidth()/getMeasuredHeight should return the correct size. But this is more tricky, don't use it if you're not sure how it works.

这篇关于捕捉布局API 11日之前调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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