1.点击PopupWindow让背景变暗
PopupWindow popupWindow = new PopupWindow();
View view= LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_popupwindow, null);
popupWindow.setContentView(view);
popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow .setBackgroundDrawable(null);
popupWindow .setFocusable(true);//解决点击view重新打开popwindow的问题
popupWindow .setOutsideTouchable(true);//点击外部popupwindow消失
popupWindow.showAtLocation(btn, Gravity.BOTTOM, 0, 0);
Button bt1 = view.findViewById(R.id.bt1);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
/**
* 点击popupWindow让背景变暗
*/
final WindowManager.LayoutParams lp = MainActivity.this.getWindow().getAttributes();
lp.alpha = 0.7f;//代表透明程度,范围为0 - 1.0f
MainActivity.this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
MainActivity.this.getWindow().setAttributes(lp);
/**
* 退出popupWindow时取消暗背景
*/
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
lp.alpha = 1.0f;
MainActivity.this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
MainActivity.this.getWindow().setAttributes(lp);
}
});
2.popupwindow(全屏+阴影+动画)
View inflate = LayoutInflater.from(this).inflate(R.layout.layout_pop, null, false);
final PopupWindow popupWindow = new PopupWindow(inflate, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
//点击非菜单部分退出
inflate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
popupWindow.setBackgroundDrawable(null);
popupWindow.setOutsideTouchable(true);
popupWindow.setAnimationStyle(R.style.PopAnimation);
//相对于屏幕的位置
int[] position = new int[2];
btn1_sendNotifation.getLocationOnScreen(position);
TextView txt = inflate.findViewById(R.id.txt);
//获取状态栏高度
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
LinearLayout.LayoutParams par = (LinearLayout.LayoutParams) txt.getLayoutParams();
par.setMargins(position[0], position[1] + btn1_sendNotifation.getHeight() - result, 0, 0);
popupWindow.showAtLocation(btn1_sendNotifation, Gravity.NO_GRAVITY, 0, 0);
3.创建anim文件夹 anim_enter.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1500">
<translate
android:fromXDelta="100%p"
android:fromYDelta="100%p"
android:toXDelta="0"
android:toYDelta="0"
/>
<alpha
android:fromAlpha="0"
android:toAlpha="1.0" />
</set>
4.anim_exit.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1500">
<translate
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="100%p"
android:toYDelta="100%p" />
<alpha
android:fromAlpha="1.0"
android:toAlpha="0" />
</set>
4.layout_pop.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="#cccccc"
android:layout_height="match_parent">
<TextView
android:text="你好"
android:textSize="30sp"
android:textColor="#00DB00"
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
5.开发中常用的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/qb_px_152"
android:layout_gravity="bottom"
android:background="#f5f5f5"
android:orientation="vertical">
<TextView
android:id="@+id/btn_pop_camera"
android:layout_width="match_parent"
android:layout_height="@dimen/qb_px_45"
android:background="@color/colorWhite"
android:gravity="center"
android:text="拍摄"
android:textSize="@dimen/qb_px_18" />
<View
android:layout_width="match_parent"
android:layout_height="@dimen/qb_px_1"
android:background="@color/colorE4" />
<TextView
android:id="@+id/btn_pop_album"
android:layout_width="match_parent"
android:layout_height="@dimen/qb_px_45"
android:background="@color/colorWhite"
android:gravity="center"
android:text="从相册选择上传"
android:textSize="@dimen/qb_px_18" />
<View
android:layout_width="match_parent"
android:layout_height="@dimen/qb_px_1"
android:background="@color/colorE4" />
<View
android:layout_width="match_parent"
android:layout_height="@dimen/qb_px_1"
android:layout_marginTop="@dimen/qb_px_10"
android:background="@color/colorE4" />
<TextView
android:id="@+id/btn_pop_cancel"
android:layout_width="match_parent"
android:layout_height="@dimen/qb_px_45"
android:background="@color/colorWhite"
android:gravity="center"
android:text="取消"
android:textSize="@dimen/qb_px_18" />
<View
android:layout_width="match_parent"
android:layout_height="@dimen/qb_px_1"
android:background="@color/colorE4" />
</LinearLayout>