mPopupWindow.setContentView(mView);
mPopupWindow.setOutsideTouchable(true);//点击空白处不关闭弹窗 true为关闭
mPopupWindow.setFocusable(true);
mPopupWindow.setAnimationStyle(R.style.AnimationRightFade); //设置动画
mPopupWindow.showAtLocation(mView, Gravity.RIGHT,0 ,0);
setBackgroundAlpha(0.5f,mContext);
WindowManager.LayoutParams nomal = ((Activity) mContext).getWindow().getAttributes();
nomal.alpha = 0.5f;
((Activity) mContext).getWindow().setAttributes(nomal);
mPopupWindow.setOnDismissListener(closeDismiss);
}
/**
-
底部显示
-
@param mView
*/
public void showBottomPopupWindow(View mView) {
mPopupWindow = new PopupWindow(mView,
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
mPopupWindow.setContentView(mView);
mPopupWindow.setOutsideTouchable(true);//点击空白处不关闭弹窗 true为关闭
mPopupWindow.setFocusable(true);
mPopupWindow.setAnimationStyle(R.style.AnimationBottomFade); //设置动画
mPopupWindow.showAtLocation(mView, Gravity.BOTTOM, 0, 0);
setBackgroundAlpha(0.5f,mContext);
WindowManager.LayoutParams nomal = ((Activity) mContext).getWindow().getAttributes();
nomal.alpha = 0.5f;
((Activity) mContext).getWindow().setAttributes(nomal);
mPopupWindow.setOnDismissListener(closeDismiss);
}
public static void setBackgroundAlpha(float bgAlpha,Context mContext){
WindowManager.LayoutParams lp = ((Activity) mContext).getWindow().getAttributes();
lp.alpha = bgAlpha;
((Activity) mContext).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
((Activity) mContext).getWindow().setAttributes(lp);
}
/**
-
设置弹窗动画
-
@param animId
-
@return showPopu
*/
public LiWindow setAnim(int animId) {
if (mPopupWindow != null) {
mPopupWindow.setAnimationStyle(animId);
}
return mLiWindow;
}
//弹窗消失时关闭阴影
public PopupWindow.OnDismissListener closeDismiss = new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams nomal = ((Activity)mContext).getWindow().getAttributes();
nomal.alpha = 1f;
((Activity)mContext).getWindow().setAttributes(nomal);
}
};
public void closePopupWindow() {
if (mPopupWindow != null) {
mPopupWindow.dismiss();
}
}
/*
使用方法
- LiWindow liWindow = new LiWindow(MainActivity.this);
View mView = LayoutInflater.from(MainActivity.this).inflate(R.layout.center_layout,null);
liWindow.showCenterPopupWindow(mView);
- */
}
弹窗也是需要布局文件的,现在创建一个新的布局文件,用于显示城市列表。
返回图标:
在项目的layout下创建一个名为window_city_list.xml的布局文件
代码如下:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
android:orientation=“vertical”
android:fitsSystemWindows=“true”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”>
<LinearLayout
android:orientation=“vertical”
android:background=“#FFF”
android:layout_width=“240dp”
android:layout_height=“match_parent”>
<RelativeLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”>
<androidx.appcompat.widget.Toolbar
android:layout_width=“match_parent”
android:layout_height=“?attr/actionBarSize”
app:contentInsetLeft=“16dp”
app:popupTheme=“@style/AppTheme.PopupOverlay”>
<TextView
android:id=“@+id/tv_title”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_gravity=“center”
an