最近項目里總是用到PopupWindow,功能不是很復雜但是寫起來很繁瑣、代碼很亂,所以就自己封裝了一個SimplePopupWindow,基于Builder模式。
功能
- 可點擊空白地方或者返回鍵使PopupWindow消失
- 可設置并調(diào)節(jié)半透明黑色背景,并且PopupWindow消失后背景色自動恢復
- 不會被軟鍵盤覆蓋
- 可設置是否同時彈出軟鍵盤
- 可設置進出動畫
一個簡單帶動畫的PopupWindow彈出
image
只需四行代碼加一個接口回調(diào)
SimplePopupWindow.with(MainActivity.this)
.setView(R.layout.popup_window_main) //設置你的視圖資源文件
.setAnimationStyle(R.style.anim_simple_popup_window) //設置你的動畫資源文件
.show(new SimplePopupWindow.Callback() {
@Override
public void getView(View view, PopupWindow popupWindow) {
//view是你資源文件生成的視圖
}
});
NOTE:后面放出動畫的資源文件
帶半透明灰色背景
image
SimplePopupWindow.with(MainActivity.this)
.setView(R.layout.popup_window_main)
.setBackgroundAlpha(0.4f) //設置你的背景透明度
.show(new SimplePopupWindow.Callback() {
@Override
public void getView(View view, PopupWindow popupWindow) {
}
});
API文檔
| 方法 | 用途 |
|---|---|
| with(Activity activity) | 初始化 |
| show(Callback callback) | 彈出PopupWindow |
| setView(int ResId) | 設置視圖的資源文件 |
| setLocation(int Lacation) | 設置彈出位置 |
| setBackgroundAlpha(float bgAlpha) | 設置黑色背景透明度 |
| setAnimationStyle(int animationStyle) | 設置動畫的資源文件 |
| setAutoPopupInput(boolean isShowInput) | 設置是否自動彈出軟鍵盤 |
Note:
- 不要在Activity的onCreate()方法中直接show(),因為此時Activity的視圖開沒有繪制完成。
- 一定要在show()之前調(diào)用setView()。
- layout布局文件要設置固定高度或wrap_content
SimplePopupWindow代碼
//直接新建SimplePopupWindow類拷貝過去
public class SimplePopupWindow {
private Activity mActivity;
private View mView;
private boolean mIsShowInput;
private int animationStyle;
private int LOCATION = -1;
public static SimplePopupWindow with(Activity activity) {
return new SimplePopupWindow(activity);
}
public SimplePopupWindow(Activity activity) {
mActivity = activity;
animationStyle = -1;
}
//設置彈出位置,默認底部彈出
public void setLocation(int Lacation){
LOCATION = Lacation;
}
//視圖資源,必須設置
public SimplePopupWindow setView(int ResId) {
mView = LayoutInflater.from(mActivity).inflate(ResId, null);
return this;
}
//彈出PopupWindow
public void show(Callback callback) {
if (mView == null){
Toast.makeText(mActivity,"請設置View", Toast.LENGTH_SHORT).show();
return;
}
PopupWindow window = new PopupWindow(mView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
if (animationStyle != -1) //如果設置了動畫,則啟用動畫
window.setAnimationStyle(animationStyle);
window.setBackgroundDrawable(new ColorDrawable());
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); //不會被軟鍵盤覆蓋
window.setOnDismissListener(new PopupWindow.OnDismissListener() { //彈窗消失時恢復背景色
@Override
public void onDismiss() {
setBackgroundAlpha(1f);
}
});
callback.getView(mView,window);
if (LOCATION != -1)
window.showAtLocation(mActivity.getWindow().getDecorView().findViewById(android.R.id.content), LOCATION, 0, 0);
else
window.showAtLocation(mActivity.getWindow().getDecorView().findViewById(android.R.id.content), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
showInPut();
}
//設置黑色半透明背景,建議設置0.4f,默認不設置
public SimplePopupWindow setBackgroundAlpha(float bgAlpha) {
WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
lp.alpha = bgAlpha;
if (bgAlpha == 1) {
mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不移除該Flag的話,在有視頻的頁面上的視頻會出現(xiàn)黑屏的bug
} else {
mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代碼主要是解決在華為手機上半透明效果無效的bug
}
mActivity.getWindow().setAttributes(lp);
return this;
}
//設置是否自動彈出軟鍵盤,默認為否
public SimplePopupWindow setAutoPopupInput(boolean isShowInput) {
mIsShowInput = isShowInput;
return this;
}
//設置進出動畫
public SimplePopupWindow setAnimationStyle(int animationStyle){
this.animationStyle = animationStyle;
return this;
}
//顯示軟鍵盤
private void showInPut(){
if (mIsShowInput){
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager)mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
}, 0);
}
}
//接口回調(diào)
public interface Callback {
void getView(View view, PopupWindow popupWindow);
}
}
動畫資源文件
transition/out.xml (新建transition或anim目錄)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="250"
android:fromYDelta="0.0"
android:toYDelta="100%" />
</set>
transition/in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="250"
android:fromYDelta="100.0%"
android:toYDelta="0.0" />
</set>
values/styles.xml
<style name="anim_simple_popup_window">
<item name="android:windowEnterAnimation">@transition/in</item>
<item name="android:windowExitAnimation">@transition/out</item>
</style>