如图
代码
private void openPopupWindow(View v) {
//如果已经显示了,就不要显示了
if (popupWindow != null && popupWindow.isShowing()) {
return;
}
//自定义显示按钮
View view = LayoutInflater.from(this).inflate(R.layout.avatar_menu_layout, null);
popupWindow = new PopupWindow(view, RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
//设置背景
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#99000000")));
//设置点击弹窗外隐藏自身
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
//弹出动画
popupWindow.setAnimationStyle(R.style.PopupWindow);
//显示位置
popupWindow.showAtLocation(v, Gravity.BOTTOM, 0, navigationHeight);
popupWindow.setOnDismissListener(() -> setBackgroundAlpha(1.0f));
initPopEvent(view);
//设置背景色
setBackgroundAlpha(0.5f);
}
相关代码
- 布局代码
<?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="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="vertical">
<TextView
android:id="@+id/take_photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:background="@drawable/selector_circle_conner_item_bg"
android:gravity="center"
android:padding="16dp"
android:text="@string/take_photo_text"
android:textColor="@color/text_666666"
android:textSize="15sp" />
<TextView
android:id="@+id/take_photo_from_album"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:background="@drawable/selector_circle_conner_item_bg"
android:gravity="center"
android:padding="16dp"
android:text="@string/select_from_album_text"
android:textColor="@color/text_666666"
android:textSize="15sp" />
<TextView
android:id="@+id/save_photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:background="@drawable/selector_circle_conner_item_bg"
android:gravity="center"
android:padding="16dp"
android:text="@string/save_image_text"
android:textColor="@color/text_666666"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cancel"
android:layout_margin="8dp"
android:background="@drawable/selector_circle_conner_item_bg">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="16dp"
android:text="@string/btn_cancel"
android:textColor="@color/text_666666"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
string.xml
<string name="avatar_title_text">个人头像</string>
<string name="take_photo_text">拍照</string>
<string name="select_from_album_text">从相册选择</string>
<string name="save_image_text">保存图片</string>
<string name="take_avatar_text">头像拍照</string>
- style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--进出动画-->
<style name="PopupWindow">
<item name="android:windowEnterAnimation">@anim/window_in</item>
<item name="android:windowExitAnimation">@anim/window_out</item>
</style>
</resources>
- windown_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="200"
android:fromYDelta="100%"
android:interpolator="@android:anim/accelerate_interpolator"
android:toYDelta="0" />
<alpha
android:duration="200"
android:fromAlpha="0"
android:toAlpha="1" />
</set>
- window_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="200"
android:fromYDelta="0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toYDelta="100%" />
<alpha
android:duration="200"
android:fromAlpha="1"
android:toAlpha="0" />
</set>
点击事件
private void initPopEvent(View view) {
view.findViewById(R.id.take_photo).setOnClickListener(v -> {
//跳转到拍照界面,拍完照片回来,上传照片
if (popupWindow != null) {
popupWindow.dismiss();
}
});
view.findViewById(R.id.take_photo_from_album).setOnClickListener(v -> {
if (popupWindow != null) {
popupWindow.dismiss();
}
});
view.findViewById(R.id.save_photo).setOnClickListener(v -> {
//TODO:
});
view.findViewById(R.id.cancel).setOnClickListener(v -> {
if (popupWindow != null) {
popupWindow.dismiss();
}
});
}
设置背景透明度
public void setBackgroundAlpha(float alpha) {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = alpha;
getWindow().setAttributes(lp);
}
以上,复制粘贴,就够用了。
来个弹窗,水个文章。