您的位置:首页 > 移动开发 > Android开发

Android7.0上PopupWindow的showAsDropDown位置问题

2017-06-03 11:00 537 查看
自定义PopupWindow

package com.example.zhh.myapplicationdialog;

import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

/**
* Android7.0上PopupWindow的showAsDropDown位置问题
* 自定义PopupWindow
* 重写showAsDropDown方法
* 注意构造方法的写法
*/
public class MyPopupWindow extends PopupWindow{
Context mContext;
private WindowManager mWindowManager;
public MyPopupWindow(View contentView, int width, int height, boolean focusable) {
super();
if (contentView != null) {
mContext = contentView.getContext();
mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
}

setContentView(contentView);
setWidth(width);
setHeight(height);
setFocusable(focusable);
}

@Override
public void showAsDropDown(View anchor) {
if(Build.VERSION.SDK_INT == 24) {
Rect rect = new Rect();
anchor.getGlobalVisibleRect(rect);
int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
setHeight(h);
}
super.showAsDropDown(anchor);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: