PopupWindow
- 注意点:
-
设置PopupWindow的宽高,容易忘记,没有设置宽高在某些情况下会不展示。
-
基础设置少不了
pop.setBackgroundDrawable(new BitmapDrawable());
pop.setFoucusable(true);
- 撸一个通用的PopupWindow
public class BasicPopupWindow extends PopupWindow {
private Context mContext;
private int layoutId;
private ViewController controller;
private int animStyleId;
private boolean isOutsideTouchable;
private BasicPopupWindow(Context context){
this.mContext = context;
initDefault();
}
private void initDefault()
{
setBackgroundDrawable(new ColorDrawable());
setFocusable(true);
setTouchable(true);
setOutsideTouchable(true);
setClippingEnabled(true); //是否允许超出屏幕范围
}
private void init() {
//宽高设置放在builder中
View contentView = LayoutInflater.from(mContext).inflate(layoutId,null);
controller.handleView(contentView);
setContentView(contentView);
}
public void showAtBottom(View anchorView) {
if(null == anchorView){
return;
}
if(!isShowing()){
backgroundDimAmount(0.5f, (Activity) mContext);
showAtLocation(anchorView, Gravity.BOTTOM,0,0);
}
}
public void showAtCenter(View anchorView)
{
if(null == anchorView){
return;
}
if(!isShowing()){
backgroundDimAmount(0.5f, (Activity) mContext);
showAtLocation(anchorView,Gravity.CENTER,0,0);
}
}
//1 全黑 0 正常 设置昏暗度
public void backgroundDimAmount(float dimAmount, Activity activity)
{
if(null == activity){
return;
}
Window window = activity.getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.dimAmount = dimAmount;
window.setAttributes(lp);
}
//设置透明度
private void backgroundAlpha(float alpha, Activity activity)
{
if(null == activity){
return;
}
Window window = activity.getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.alpha = alpha;
window.setAttributes(lp);
}
public void setViewController(ViewController viewController)
{
this.controller = viewController;
}
public void setLayoutId(int layoutId)
{
this.layoutId = layoutId;
}
public interface ViewController {
void handleView(View view);
}
public static class Builder{
private int layoutId;
private Context mContext;
private ViewController viewController;
private int width;
private int height;
private int animStyleId;
private boolean isOutsideTouchable = true;
//必须
public void setLayoutId(@LayoutRes int layoutId) {
this.layoutId = layoutId;
}
//必须
public void setContext(Context context){
this.mContext = context;
}
//必须
public void setViewController(ViewController viewController) {
this.viewController = viewController;
}
public void setWidth(int width) {
this.width = width;
}
public void setOutsideTouchable(boolean outsideTouchable) {
this.isOutsideTouchable = outsideTouchable;
}
public void setHeight(int height){
this.height = height;
}
public void setAnimStyle(@AnimRes int animStyleId){
this.animStyleId = animStyleId;
}
public BasicPopupWindow build(){
BasicPopupWindow popWindow = new BasicPopupWindow(mContext);
popWindow.setViewController(viewController);
popWindow.setLayoutId(layoutId);
if(0 == width){
width = ViewGroup.LayoutParams.MATCH_PARENT;
}
if(0 == height){
height = ViewGroup.LayoutParams.WRAP_CONTENT;
}
popWindow.setWidth(width);
popWindow.setHeight(height);
if(0 != animStyleId){
popWindow.setAnimationStyle(animStyleId);
}
popWindow.setOutsideTouchable(isOutsideTouchable);
popWindow.init();
return popWindow;
}
}
}