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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值