Summary of several ways for Android to obtain view width and height

Notes on Android development art exploration:

When obtaining the height of a view in oncreate() or onresume() of an activity, the width and height information cannot be obtained correctly. This is because the measure process of the view and the life cycle of the activity are not executed synchronously. Therefore, it cannot be guaranteed that when the activity executes oncreate OnStart onresume, a view has been measured. If it has not been measured, Then the width and height obtained is 0. It can be obtained in the following ways:

1、onWindowFocusChanged

Onwindowfocuschanged: the view has been initialized and the width and height have been set. Note that onwindowfocuschanged will be called multiple times. This callback will be executed when the activity gets focus and loses focus, as shown in the following figure:

1. The method executed when the activity first enters

2. When jumping to another activity

3. When returning to the current activity, it can be seen that onwindowfocuschanged will be called when onresume and onpause are executed.

2、view.post(runnable)

A runnable can be delivered to the end of the message queue through post. When the looper calls the runnable, the view has been initialized. Example:

3、ViewTreeObserver

This function can be completed by using many callbacks of viewtreeobserver. For example, using the ongloballayoutlistener interface, when the state of the view tree changes or the visibility of the view inside the view tree changes, the ongloballayout method will be called back. This is a good time to obtain the width and height of the view. It should be noted that with the change of the state of the view tree, Ongloballayout will be called multiple times, for example:

4、view.measure(int widthMeasureSpec,int heightMeasureSpec)

The width and height of the view can be obtained by manually measuring the view. Here, it should be handled according to the situation, according to the layoutparams of the view:

match-parent

The specific width and height cannot be measured, because according to the measure process of view, the construction of such measurespec needs to know the parentsize, that is, the remaining space of the parent container, which we don't know, so we can't measure the size of view.

Specific value (DP / PX)

For example, the width and height are 100px, as follows:

wrap_ content

Measure as follows:

The specsize of view is represented by 30 bit binary, that is, the maximum is 30 1, that is (1 < < 30) - 1. In the maximization mode, it is reasonable for us to construct measurespec with the maximum value that view can support theoretically.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>