1. 程式人生 > >popupWindow在android7.0以上顯示全屏的問題

popupWindow在android7.0以上顯示全屏的問題

在Android7.0以上版本呼叫popupWindow的showAsDropDown()方法,始終顯示全屏,今天記錄下解決這個問題的方法

自定義popupwindow複寫showAsDropDown()方法解決這個問題

package com.luckongo.tthd.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
Chengguo on 2017/12/20. */
public class PopWindow extends PopupWindow { public PopWindow(Context context) { super(context); } public PopWindow(Context context, AttributeSet attrs) { super(context, attrs); } public PopWindow(Context context, AttributeSet attrs, int defStyleAttr) { super
(context, attrs, defStyleAttr); } public PopWindow(View contentView) { super(contentView); } public PopWindow(int width, int height) { super(width, height); } public PopWindow(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); } }