Android Foundation: get the width and height of LinearLayout

preface

When you see the problem of obtaining the width and height of LinearLayout, you may think of the following solutions:

You will find that it prints 0

That's because the LinearLayout has not been drawn in the oncreate method, so the height obtained is 0,

Or try to put this code into the onresume () method, which is still 0.

Implementation method

If we use to get the width and height of LinearLayout

You can continuously monitor the width and height of LinearLayout through the timer. After drawing, turn off the timer.

Similarly, if you want to pop up the popupwindow immediately after the activity is started, we know that writing the pop-up popupwindow method directly in the oncreate() method of the activity will report an error, because the popupwindow cannot pop up until the activity is fully started.

We can try to implement it in two ways:

1. Using the onwindowfocuschanged method

2. Use handler and runnable to delay

In this way, the problem of obtaining the width and height of LinearLayout is solved.

summary

The above is the whole content of this article. I hope the content of this article can be helpful to Android developers. If you have any questions, you can leave a message.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>