手抄报 安全手抄报 手抄报内容 生活知识 生活百科 英语手抄报 清明节手抄报 节约用水手抄报 知识问答

安卓popupwindow怎么适配

时间:2026-02-24 20:37:29

1、这里我直接给大家代码吧!// 将popupWindow显示在anchor下方

public void showAsDropDown(PopupWindow popupWindow, View anchor) {

 if (Build.VERSION.SDK_INT < 24) {

  popupWindow.showAsDropDown(anchor);

 } else {

  // 适配 android 7.0

  int[] location = new int[2];

  // 获取控件在屏幕的位置

  anchor.getLocationOnScreen(location);

  popupWindow.showAtLocation(anchor, Gravity.NO_GRAVITY, 0, location[1] + anchor.getHeight());

 }

}

安卓popupwindow怎么适配

2、if (Build.VERSION.SDK_INT < 24) {

 mPopupWindow = new FixedPopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT,

     ViewGroup.LayoutParams.MATCH_PARENT);

} else {

 int[] location = new int[2];

安卓popupwindow怎么适配

3、 // 获取控件在屏幕的位置

 anchor.getLocationOnScreen(location);

 int screenHeight = getScreenHeightPixels(context);

 mPopupWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT,

     screenHeight - (location[1] + anchor.getHeight()));

}

安卓popupwindow怎么适配

4、import android.graphics.Rect;

import android.os.Build;

import android.view.View;

import android.widget.PopupWindow;

/**

 * Created by smart on 2018/5/15.

 */

public class FixedPopupWindow extends PopupWindow {


 public FixedPopupWindow(View contentView, int width, int height){

  super(contentView, width, height);

安卓popupwindow怎么适配

5、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; //屏幕高度减去 anchor 的 bottom

   setHeight(h);// 重新设置PopupWindow高度

  }

  super.showAsDropDown(anchor);

 }

 ...

}

安卓popupwindow怎么适配

© 2026 手抄报圈
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com