In Android, there are usually multiple ways to get the width and height of a View. Android's view layout and measurement are performed asynchronously. At some life cycle stages, the width and height of a control may not be determined. The following are several common methods for getting the width and height of a control: - 「Override onMeasure method」If it is a custom View class, you can get the width and height of the control in the onMeasure method. Note that the onMeasure method is called during the measurement phase, at which time the width and height of the control may have been determined by the layout parameter settings of the parent control or measureSpec.
- The two methods getMeasuredWidth() and getMeasuredHeight() return the width and height values determined by the control during the measurement phase. If the control has not been measured (for example, called directly in the onCreate method), the returned value may be 0.
- "Use getWidth() and getHeight()" to return the final width and height values determined by the control during the layout phase. If called before the layout phase (for example, in the onCreate method), it may also return 0.
- 「Use ViewTreeObserver and OnGlobalLayoutListener」This is a reliable way to get the final width and height of the control. You can listen to the global layout changes of the control by adding a ViewTreeObserver to the control and registering an OnGlobalLayoutListener. When the layout of the control is completed, the onGlobalLayout method of OnGlobalLayoutListener will be called, and you can safely get the width and height of the control.
Sample code: View view = findViewById(R.id.view); view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { view.getViewTreeObserver().removeOnGlobalLayoutListener(this); int width = view.getWidth(); int height = view.getHeight(); } }); - 「Use post method」The post method queues a Runnable object to the execution queue of the main thread and executes it after the drawing phase of the control. It is usually used to perform certain operations after the layout and drawing of the control are completed. The post method can be used to delay the acquisition of the width and height of the control.
Sample code: View view = findViewById(R.id.view); view.post(new Runnable() { @Override public void run() { int width = view.getWidth(); int height = view.getHeight(); } }); - 「Using View.LayoutParams」In some cases, you can predict the possible width and height of a control by checking its LayoutParams. This is usually only applicable to controls that use fixed sizes (such as WRAP_CONTENT or MATCH_PARENT) or specific sizes (such as dp or px values). This method does not guarantee that the final width and height of the control are obtained.
- The "onWindowFocusChanged" method will be called multiple times, after the View is initialized, and once when the Activity's window gains and loses focus (for example, when the Activity is onResume).
Sample code: @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if (hasFocus) { int width = view.getMeasuredWidth(); int height = view.getMeasuredHeight(); } } When trying to get the width and height of a control, you should consider the control's life cycle and layout process to ensure that you get the correct value at the right time. |