popupwindow里含有listview,显示在控件上方

popupwindow里含有listview,显示在控件上方

int popupHeight = ViewUtils.getListHeight(listView);
public void showPopup(View view){
    if (view == null) return;
    int location[] = new int[2];
    view.getLocationOnScreen(location);
    if (popupWindow != null && !popupWindow.isShowing()){
        int y = location[1] - popupHeight;
        popupWindow.showAtLocation(view,Gravity.NO_GRAVITY,location[0],y);
    }
}

//获取listview的高度

@SuppressLint("NewApi")
public static int getListHeight(ListView list) {
   ListAdapter listAdapter = list.getAdapter();
   if (listAdapter == null) {
      return 0;
   }

   int totalHeight = 0;
   int count = listAdapter.getCount();
   for (int i = 0; i < count; i++) {
      View listItem = listAdapter.getView(i, null, list);
      listItem.measure(0, 0);
      totalHeight += listItem.getMeasuredHeight();
   }

   return totalHeight;

}

你可能感兴趣的:(android)