Preface
In many cases, we have the need to get the size (width and height) of the view in the activity. Faced with this situation, many students immediately responded: You still need to say such a simple question? Are you stupid? . Then immediately write down the methods such as getWidth(), getHeight(), etc., and leave proudly. But is this the truth? Practice has proved that we cannot get the width and height of the View in this way.
When we get the width and height of a View component in the onCreate() method, we will only get 0 if we call the getWidth(), getHeight(), getMeasuredWidth(), getMeasuredHeight(), getMeasuredHeight() methods directly. What is the reason for this? Let's take a look together below
Implementation method
1. Use Measure View
The width and height measured by this method may be inconsistent with the true width and height after the view is drawn.
int width = (0, ); int height = (0, ); (width, height); (); // Get width(); // Get height
2. Use ViewTreeObserver. OnPreDrawListener to listen for events
Calling the listening event when the view is about to be drawn will be called multiple times, so the listening event must be removed after obtaining the width and height of the view.
().addOnPreDrawListener( new () { @Override public boolean onPreDraw() { ().removeOnPreDrawListener(this); (); // Get width (); // Get height return true; } });
3. Use ViewTreeObserver. OnGlobalLayoutListener to listen for events
Calling the event when the layout changes or the visual state of a view changes, it will be called multiple times. Therefore, it is necessary to execute the remove method after obtaining the width and height of the view to remove the listening event.
().addOnGlobalLayoutListener( new () { @Override public void onGlobalLayout() { if (.SDK_INT >= 16) { () .removeOnGlobalLayoutListener(this); } else { () .removeGlobalOnLayoutListener(this); } (); // Get width (); // Get height } });
4. Rewrite the onSizeChanged method of View
Calling this method when the size of the view changes will be called multiple times, so you need to consider disabling the code after obtaining the width and height.
This implementation method needs to inherit View and is called many times, so it is not recommended to use it.
@Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { (w, h, oldw, oldh); (); // Get width (); // Get height}
5. Rewrite the onLayout method of View
This method will be called many times. After obtaining the width and height, you need to consider disabling the code.
This implementation method needs to inherit View and is called many times, so it is not recommended to use it.
@Override protected void onLayout(boolean changed, int l, int t, int r, int b) { (changed, l, t, r, b); (); // Get width (); // Get height}
6. Use listening events (API >= 11)
Calling the event when the layout of the view changes will be called multiple times, so you need to execute the remove method after obtaining the width and height of the view to remove the listening event.
( new () { @Override public void onLayoutChange(View v, int l, int t, int r, int b, int oldL, int oldT, int oldR, int oldB) { (this); (); // Get width (); // Get height } });
7. Use () method
Methods in the Runnable object will be triggered after the view's measurement, layout and other events are completed.
The UI event queue will process events in order. After setContentView() is called, the event queue will contain a message that requires relayout, so any Runnable object posted to the queue will be executed after the Layout changes.
This method will only be executed once, and the logic is simple, so it is recommended to use it.
(new Runnable() { @Override public void run() { (); // Get width (); // Get height } });
The above is the reprinted content, personal learning and collection records
Below is your own study record.
First of all, the first method, which has been used before, is indeed inaccurate. It is guessed that it should be because the parameters are not used well, because the parameters only use the measurement method that is not specified by UNSPECIFIED. Generally, Wrap_Content is the measurement method.
Here is a more useful method to collect Android UtilCode.
public static int[] measureView(final View view) { lp = (); if (lp == null) { lp = new ( .MATCH_PARENT, .WRAP_CONTENT ); } int widthSpec = (0, 0, ); int lpHeight = ; int heightSpec; if (lpHeight > 0) { heightSpec = (lpHeight, ); } else { heightSpec = (0, ); } (widthSpec, heightSpec); return new int[]{(), ()}; }
Then when I am making a custom view, I need to create a view in the add code. I cannot get the width and height using the above method because I am using ScrollView. For example, in customization, the method that should select the last post should be used most.
In addition, many of them are used, which should be the third method, which is generally used externally, such as the operation that needs to be performed after the Recyclerview is drawn.
Summarize
The above is the entire content of this article. I hope that the content of this article has a certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.