全屏
PopupWindow mPopupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
设置高度MATCH_PARENT后,不管怎么设置mPopupWindow.showAsDropDown(view);或者mPopupWindow.showAtLocation(view,Gravity.NO_GRAVITY,0,view.getHeight());都会显示在全屏显示在屏幕左上角...
处理方式 修改高度WRAP_CONTENT
注意:在安卓7.0上请看下面要注意的地方
/**
- 在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);
}
遮罩处理
设置WRAP_CONTENT,遮罩不显示,设置父控件背景色无效,只能添加新的View
popwindow layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@null"/>
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/gold"/>
</LinearLayout>
item layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="52dp"
android:minHeight="52dp"
android:paddingLeft="15dp"
android:paddingRight="15dp">
<ImageView
android:id="@+id/ivIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:src="@drawable/icon_tc_cfb_red"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/ivIcon"
android:layout_marginLeft="15dp"
android:orientation="vertical">
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="title"/>
<TextView
android:id="@+id/tvContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="content"/>
</LinearLayout>
<ImageView
android:id="@+id/ivMore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:src="@drawable/icon_tc_cfb_red"/>
<View
android:id="@+id/viewLine"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="#EEEEEE"/>
</RelativeLayout>
public void showDialog(Context context,View view) {
View popupView = View.inflate(context, R.layout.gg_detail_dialog, null);
final PopupWindow mPopupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
mPopupWindow.setTouchable(true);
mPopupWindow.setOutsideTouchable(true);
Log.d("ShowGGDetailDialog", "view.getHeight():" + view.getHeight());
// mPopupWindow.showAtLocation(view,Gravity.NO_GRAVITY,0,view.getHeight());
mPopupWindow.showAsDropDown(view);
popupView.setBackgroundColor(colorSet.detailDialogBg);
ListView listView = popupView.findViewById(R.id.listView);
listView.setBackgroundColor(colorSet.backColor);
String[] titleArray = {"备注","备注","备注"};
List<GGDetailInfo> list = new ArrayList<>();
for (int i = 0; i < titleArray.length; i++) {
GGDetailInfo ggDetailInfo = new GGDetailInfo("", titleArray[i], titleArray[i], true);
list.add(ggDetailInfo);
}
listView.setAdapter(new DetailAdapter(context,list));
}
class DetailAdapter extends BaseAdapter{
Context context;
List<GGDetailInfo> listInfo;
public DetailAdapter(Context context ,List<GGDetailInfo> listInfo) {
this.context = context;
this.listInfo = listInfo;
}
@Override
public int getCount() {
return listInfo == null ? 0 : listInfo.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
GGDetailInfo ggDetailInfo = listInfo.get(position);
ViewHolder viewHolder;
if (convertView == null){
convertView = View.inflate(context, R.layout.gg_detail_item, null);
viewHolder = new ViewHolder();
viewHolder.ivIcon = convertView.findViewById(R.id.ivIcon);
viewHolder.ivMore = convertView.findViewById(R.id.ivMore);
viewHolder.tvTitle = convertView.findViewById(R.id.tvTitle);
viewHolder.tvContent = convertView.findViewById(R.id.tvContent);
viewHolder.viewLine = convertView.findViewById(R.id.viewLine);
convertView.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.tvTitle.setText(ggDetailInfo.getTitle());
return convertView;
}
}
private static class ViewHolder{
TextView tvTitle;
TextView tvContent;
}
关于ListView
- 取消ListView的默认分割线android:divider="@null"
- 设置ListView item的高度无效 android:minHeight="52dp"
- 设置ListView item的margin无效 添加相关的padding属性

点击外部popwindow不消失
popupWindow = new PopupWindow(LayoutInflater.from(MainActivity.this).inflate(R.layout.pop_list, null), ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
改成如下:
popupWindow = new PopupWindow(LayoutInflater.from(MainActivity.this).inflate(R.layout.pop_list, null), ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
原因很简单,因为前者这样写得话,popupWindow 把整个window都遮挡了,所以popupWindow.setOutsideTouchable(true)并没有起到作用,所以失效
网友评论