SoFunction
Updated on 2025-03-11

Solution to the problem of incorrect display position of some PopuWindow on Android 7.0

This article describes the solution to the problem of incorrect display position of some PopuWindow on Android 7.0. Share it for your reference, as follows:

Scenario description:

On andorid7.0 and above system, click on a view. I originally expected a PopuWindow to pop up under the view (call)(view)Method) But the result is that PopuWindow pops up above the view, topped under the system status bar.

Cause analysis:

On android 7.0, if the size of PopuWindow is not actively constrained, for example, set the layout size toMATCH_PARENT,Then PopuWindow will become as large as possible, so that there is no space below the view to fully display PopuWindow, and the view cannot scroll upward. At this time, PopuWindow will actively move upward until it can be fully displayed.

Solution:

Actively constrain the content size of PopuWindow, rewriteshowAsDropDownmethod:

@Override
public void showAsDropDown(View anchor) {
  if(.SDK_INT >= 24){
    Rect visibleFrame = new Rect();
    (visibleFrame);
    int height = ().getDisplayMetrics().heightPixels - ;
    setHeight(height);
  }
  (anchor);
}

For more information about Android related content, please check out the topic of this site:Summary of Android window related operation skills》、《Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.