活动的生命周期 - 收到通知布局完成 [英] Activity lifecycle - receiving notification that layout is complete

查看:169
本文介绍了活动的生命周期 - 收到通知布局完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动,我有3个按键放在旁边对方。我已经使用按键的子类,将调整按钮上的文字为prevent从包装的文字。我想3个按钮共享相同的文字大小。为了做到这一点,我打算检测与最小的文本大小的按钮,并设置其他2个按钮的文字大小。

I have an activity in which I have 3 buttons placed alongside each other. I have used a subclass of Button that will resize the button text to prevent the text from wrapping. I would like the 3 buttons to share the same text size. In order to do this I intend to detect the button with the smallest text size and set the other 2 buttons to that text size.

我的问题是知道什么时候该活动已完成布局它的组件,以便我能够可靠地知道已经发生了文本的大小调整。从Android文档它会出现的最新通知中的生命周期是onResume(),但它似乎布局还没有在这一点完成。有没有接到通知,该活动的布局已经完成的一种方式?

The problem I have is knowing when the Activity has completed laying out its components so that I can reliably know that the resizing of the text has occurred. From the Android documentation it would appear that the latest notification in the lifecycle is onResume() but it appears that the layout hasn't completed at this point. Is there a way of receiving notification that the Activity layout has finished?

推荐答案

我做了使用树观察员类似的东西。您可能必须与它玩了一下,找到该期(县),应附过,但是这将触发一旦视图设置。从那里,你可以得到文字的大小,并进行任何所需的更改。

I've done something similar using Tree Observers. You may have to play around with it a bit to find which view(s) it should be attached too, but this will fire once the view is set. From there, you can get the size of the text, and make whatever changes are needed.

mTestButton.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        // make sure it is not called anymore 
        mTestButton.getViewTreeObserver().removeGlobalOnLayoutListener(this);

        float size = mTestButton.getTextSize();
    }
});


编辑:
该方法#removeGlobalOnLayoutListener一直德precated,但仍然有效。同时使用新的和旧的方法,请参见这个计算器回答



The method #removeGlobalOnLayoutListener has been deprecated, but is still valid. To use both the new and old method, see this stackoverflow answer.

这篇关于活动的生命周期 - 收到通知布局完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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