popupWindow在android7.0以上显示全屏的问题

匿名 (未验证) 提交于 2019-12-03 00:40:02

在Android7.0以上版本调用popupWindow的showAsDropDown()方法,始终显示全屏,或者遮蔽标题栏目,
解决方案:

package com.example.view;  import android.content.Context; import android.graphics.Rect; import android.os.Build; import android.util.AttributeSet; import android.view.View; import android.widget.PopupWindow;  /**  * @author che on 2018/7/06.  */ public class CommonPopWindow extends PopupWindow {     public CommonPopWindow (Context context) {         super(context);     }      public CommonPopWindow (View contentView, int width, int height) {         super(contentView, width, height);     }      /**      *  在android7.0上,如果不主动约束PopuWindow的大小,比如,设置布局大小为 MATCH_PARENT,那么PopuWindow会变得尽可能大,      *  以至于 view下方无空间完全显示PopuWindow,而且view又无法向上滚动,此时PopuWindow会主动上移位置,直到可以显示完全。      * 解决办法:主动约束PopuWindow的内容大小,重写showAsDropDown方法:      * @param anchor      */     @Override     public void showAsDropDown(View anchor) {          if (Build.VERSION.SDK_INT >= 24){             Rect visibleFrame = new Rect();             anchor.getGlobalVisibleRect(visibleFrame);             int height = anchor.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;             setHeight(height);         }         super.showAsDropDown(anchor);     }     @Override     public void showAsDropDown(View anchor, int xoff, int yoff) {         if (Build.VERSION.SDK_INT >= 24) {             Rect visibleFrame = new Rect();             anchor.getGlobalVisibleRect(visibleFrame);             int height = anchor.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;             setHeight(height);         }         super.showAsDropDown(anchor,xoff,yoff);     } }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!