Activity lifecycle - receiving notification that layout is complete

前端 未结 1 1999
北海茫月
北海茫月 2021-02-11 16:35

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

相关标签:
1条回答
  • 2021-02-11 17:27

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


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

    0 讨论(0)
提交回复
热议问题