各種Popwindow和Dialog的匯總以及仿ios的茂腥,可以自定義布局,也可以傻瓜式的代碼生成切省,使用非常方便

由于原生帶的dialog或者popuwindow彈窗確實有點丑最岗,有時候完成不了UI布局和設(shè)計,所以整合了各種各樣的popuwindow和dialog朝捆,居中般渡,底部,帶動畫,有陰影驯用,仿iOS效果脸秽,自定義布局等,原理都差不多
都是自定義繼承popuwindow和dialog寫的蝴乔,實現(xiàn)點擊按鈕的監(jiān)聽事件记餐,設(shè)置動畫樣式,設(shè)置Activity的背景透明度薇正,給出實現(xiàn)點擊事件接口片酝,先看效果圖:

下載地址:
https://github.com/PangHaHa12138/ManyPopWindowAndDialog
先看Popuwindow
1.超簡單的仿ios效果,底部彈出
使用:

final BottomPopupOption bottomPopupOption = new BottomPopupOption(MainActivity.this);
bottomPopupOption.setItemText("拍照","相冊");//這里數(shù)值個數(shù)即條目個數(shù)挖腰,名字即條目名字
bottomPopupOption.showPopupWindow();
bottomPopupOption.setItemClickListener(new BottomPopupOption.onPopupWindowItemClickListener() {
    @Override
    public void onItemClick(int position) {
        switch (position){
            case 0:
                Toast.makeText(MainActivity.this,"拍照",Toast.LENGTH_SHORT).show();
                bottomPopupOption.dismiss();//點擊項目消失的方法
                break;
            case 1:
                Toast.makeText(MainActivity.this,"相冊",Toast.LENGTH_SHORT).show();
                bottomPopupOption.dismiss();//點擊項目消失的方法
                break;
        }
    }
});

代碼 其實就是對popuwindow包裝了一下雕沿,設(shè)置彈出的位置和動畫,以及背景變暗

package com.panghaha.it.manypopwindowanddialog.View;

import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.panghaha.it.manypopwindowanddialog.R;


/**
 * 從下面出來的仿ios  popuwindow
 * */
public class BottomPopupOption {
   //上下文對象
   private Context mContext;
   //Title文字
   private String mTitle;
   //PopupWindow對象
   private PopupWindow mPopupWindow;
   //選項的文字
   private String[] options;
   //選項的顏色
   private int[] Colors;
   //點擊事件
   private onPopupWindowItemClickListener itemClickListener;

   /**
    * 一個參數(shù)的構(gòu)造方法曙聂,用于彈出無標題的PopupWindow
    *
    * @param context
    */
   public BottomPopupOption(Context context) {
       this.mContext = context;
   }

   /**
    * 2個參數(shù)的構(gòu)造方法晦炊,用于彈出有標題的PopupWindow
    *
    * @param context
    * @param title   標題
    */
   public BottomPopupOption(Context context, String title) {
       this.mContext = context;
       this.mTitle = title;
   }

   /**
    * 設(shè)置選項的點擊事件
    *
    * @param itemClickListener
    */
   public void setItemClickListener(onPopupWindowItemClickListener itemClickListener) {
       this.itemClickListener = itemClickListener;
   }

   /**
    * 設(shè)置選項文字
    */
   public void setItemText(String... items) {
       options = items;
   }

   /**
    * 設(shè)置選項文字顏色,必須要和選項的文字對應(yīng)
    */
   public void setColors(int... color) {
       Colors = color;
   }


   /**
    * 添加子View
    */
   private void addView(View v) {
       LinearLayout lin_layout = (LinearLayout) v.findViewById(R.id.layout_popup_add);
       //Title
       TextView tv_pop_title = (TextView) v.findViewById(R.id.tv_popup_title);
       //取消按鈕
       Button btn_cancel = (Button) v.findViewById(R.id.btn_cancel);
       btn_cancel.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               dismiss();
           }
       });
       if (mTitle != null) {
           tv_pop_title.setText(mTitle);
       } else {
           tv_pop_title.setVisibility(View.GONE);
       }
       if (options != null && options.length > 0) {
           for (int i = 0; i < options.length; i++) {
               View item = LayoutInflater.from(mContext).inflate(R.layout.basetools_popup_item, null);
               Button btn_txt = (Button) item.findViewById(R.id.btn_popup_option);
               btn_txt.setText(options[i]);
               if (Colors != null && Colors.length == options.length) {
                   btn_txt.setTextColor(Colors[i]);
               }
               final int finalI = i;
               btn_txt.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {
                       if (itemClickListener != null) {
                           itemClickListener.onItemClick(finalI);
                       }
                   }
               });
               lin_layout.addView(item);
           }
       }
   }

   /**
    * 彈出Popupwindow
    */
   public void showPopupWindow() {
       View popupWindow_view = LayoutInflater.from(mContext).inflate(R.layout.basetools_popup_bottom, null);
       //添加子View
       addView(popupWindow_view);
       mPopupWindow = new PopupWindow(popupWindow_view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
       mPopupWindow.setAnimationStyle(R.style.popwindow_anim_style);
       mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
       mPopupWindow.setFocusable(true);
       mPopupWindow.setOutsideTouchable(true);
       mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
           @Override
           public void onDismiss() {
               setWindowAlpa(false);
           }
       });
       /**
        * 防止擋住虛擬鍵
        * */
       mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
       show(popupWindow_view);
   }


   /**
    * 顯示PopupWindow
    */
   private void show(View v) {
       if (mPopupWindow != null && !mPopupWindow.isShowing()) {
           mPopupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);
       }
       setWindowAlpa(true);
   }


   /**
    * 消失PopupWindow
    */
   public void dismiss() {
       if (mPopupWindow != null && mPopupWindow.isShowing()) {
           mPopupWindow.dismiss();
       }
   }

   /**
    * 動態(tài)設(shè)置Activity背景透明度
    *
    * @param isopen
    */
   public void setWindowAlpa(boolean isopen) {
       if (Build.VERSION.SDK_INT < 11) {
           return;
       }
       final Window window = ((Activity) mContext).getWindow();
       final WindowManager.LayoutParams lp = window.getAttributes();
       window.setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND, WindowManager.LayoutParams.FLAG_DIM_BEHIND);
       ValueAnimator animator;
       if (isopen) {
           animator = ValueAnimator.ofFloat(1.0f, 0.5f);
       } else {
           animator = ValueAnimator.ofFloat(0.5f, 1.0f);
       }
       animator.setDuration(400);
       animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

           @TargetApi(Build.VERSION_CODES.HONEYCOMB)
           @Override
           public void onAnimationUpdate(ValueAnimator animation) {
               float alpha = (float) animation.getAnimatedValue();
               lp.alpha = alpha;
               window.setAttributes(lp);
           }
       });
       animator.start();
   }


   /**
    * 點擊事件選擇回調(diào)
    */
   public interface onPopupWindowItemClickListener {
       void onItemClick(int position);
   }
}

2.自定義布局宁脊,底部彈出
布局就是普通的能實現(xiàn)需求就行

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal">

    <LinearLayout
            android:id="@+id/pop_layout"
            android:layout_marginBottom="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/renwumiaoshu_bg"
            android:gravity="center_horizontal"
            android:orientation="vertical">

    <LinearLayout
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:id="@+id/tubiaotongji"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:clickable="true"
        android:background="?android:attr/selectableItemBackground"
        android:layout_width="match_parent"
        android:layout_height="42dp">

        <ImageView
            android:layout_marginLeft="10dp"
            android:src="@drawable/tab_but_tubiao_2x"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:text="圖表統(tǒng)計"
            android:layout_marginLeft="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>
    <View
        android:layout_gravity="center_horizontal"
        android:background="#a8a8a8"
        android:layout_width="320dp"
        android:layout_height="0.5dp"/>

    <LinearLayout
        android:layout_marginRight="5dp"
        android:layout_marginLeft="5dp"
        android:id="@+id/renwusousu"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:clickable="true"
        android:background="?android:attr/selectableItemBackground"
        android:layout_width="match_parent"
        android:layout_height="42dp">

        <ImageView
            android:layout_marginLeft="10dp"
            android:src="@drawable/tab_but_sousuo_2x"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:text="任務(wù)搜索"
            android:layout_marginLeft="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>
    <View
        android:layout_gravity="center_horizontal"
        android:background="#a8a8a8"
        android:layout_width="320dp"
        android:layout_height="0.5dp"/>

    <LinearLayout
        android:layout_marginRight="5dp"
        android:layout_marginLeft="5dp"
        android:id="@+id/paixu"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:clickable="true"
        android:background="?android:attr/selectableItemBackground"
        android:layout_width="match_parent"
        android:layout_height="42dp">
        <ImageView
            android:layout_marginLeft="10dp"
            android:src="@drawable/tab_but_paixu_2x"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:text="排序"
            android:layout_marginLeft="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>


</LinearLayout>
</RelativeLayout>

然后代碼

package com.panghaha.it.manypopwindowanddialog.View;

/**
 * Created by pang on 2017/4/4.
 *  自定義 popupwindow 彈窗
 */

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.panghaha.it.manypopwindowanddialog.R;


public class PhotoPopupWindows extends PopupWindow {
    private View mMenuView; // PopupWindow 菜單布局
    private Context context; // 上下文參數(shù)
    private OnClickListener myOnClick; // PopupWindow 菜單 空間單擊事件

    private LinearLayout tubiaotongji;
    private LinearLayout renwusousu;
    private LinearLayout paixu;

    public PhotoPopupWindows(Activity context, OnClickListener myOnClick) {
        super(context);
        this.context = context;
        this.myOnClick = myOnClick;
        Init();
    }

    private void Init() {
        // TODO Auto-generated method stub
        // PopupWindow 導(dǎo)入
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.popuwindow_chaxun, null);
        tubiaotongji = (LinearLayout) mMenuView.findViewById(R.id.tubiaotongji);
        renwusousu = (LinearLayout) mMenuView.findViewById(R.id.renwusousu);
        paixu = (LinearLayout) mMenuView.findViewById(R.id.paixu);

        //圖表統(tǒng)計
        tubiao#setOnClickListener(myOnClick);
        //任務(wù)搜索
        renwusousu.setOnClickListener(myOnClick);
        //排序
        paixu.setOnClickListener(myOnClick);

        // 導(dǎo)入布局
        this.setContentView(mMenuView);
        // 設(shè)置動畫效果
        this.setAnimationStyle(R.style.popwindow_anim_style);
        //防止虛擬鍵擋住
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //設(shè)置彈出窗體的 寬断国,高
        this.setWidth(LayoutParams.WRAP_CONTENT);
        this.setHeight(LayoutParams.WRAP_CONTENT);
        // 設(shè)置可觸
        this.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0x0000000);
        this.setBackgroundDrawable(dw);
        // 單擊彈出窗以外處 關(guān)閉彈出窗
        mMenuView.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });
    }
}

上面代碼中先是找控件id 設(shè)置點擊事件,構(gòu)造方法傳點擊事件對象
調(diào)用榆苞;

//展示彈窗 popuwindow 為底部彈出的窗口進行按鈕的監(jiān)聽稳衬。
    private void showPopMenu() {
        //彈窗與虛擬鍵重合
//        popMenus.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        popMenus = new PhotoPopupWindows(MainActivity.this, myMenusOnClick);
        popMenus.showAtLocation(MainActivity.this.findViewById(R.id.main_activity),
                Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);

    }

    private View.OnClickListener myMenusOnClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            popMenus.dismiss();
            switch (v.getId()) {
                //圖表統(tǒng)計
                case R.id.tubiaotongji:
                    Toast.makeText(MainActivity.this,"圖表統(tǒng)計",Toast.LENGTH_SHORT).show();
                    break;
                //任務(wù)搜索
                case R.id.renwusousu:
                    Toast.makeText(MainActivity.this,"任務(wù)搜索",Toast.LENGTH_SHORT).show();
                    break;
                //排序
                case R.id.paixu:
                    Toast.makeText(MainActivity.this,"排序",Toast.LENGTH_SHORT).show();
                    break;
            }

        }
    };

3.居中的popuwindow 代碼跟上面 差不多,

popMenus2.showAtLocation(MainActivity.this.findViewById(R.id.main_activity),
        Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 0);

就是設(shè)置位置的時候坐漏,設(shè)置為center薄疚,
4.仿微信右上角的poupwindow
控件代碼,其實也是先自定義布局

package com.panghaha.it.manypopwindowanddialog.View;

/**
 * Created by pang on 2017/4/4.
 *  自定義 popupwindow 彈窗  仿微信下拉選擇器
 */

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.panghaha.it.manypopwindowanddialog.R;


public class TopPopupWindows extends PopupWindow {
    private View mMenuView; // PopupWindow 菜單布局
    private Context context; // 上下文參數(shù)
    private OnClickListener myOnClick; // PopupWindow 菜單 空間單擊事件

    private LinearLayout zhuzhuang,zhexian;

    public TopPopupWindows(Activity context, OnClickListener myOnClick) {
        super(context);
        this.context = context;
        this.myOnClick = myOnClick;

        Init();
    }


    private void Init() {
        // TODO Auto-generated method stub
        // PopupWindow 導(dǎo)入
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.popuwindow_weixin, null);
        zhuzhuang = (LinearLayout) mMenuView.findViewById(R.id.zhu_layout);
        zhexian = (LinearLayout) mMenuView.findViewById(R.id.zhexian_layout);


        //柱狀圖
        zhuzhuang.setOnClickListener(myOnClick);
        //折線圖
        zhexian.setOnClickListener(myOnClick);

        // 導(dǎo)入布局
        this.setContentView(mMenuView);
        // 設(shè)置動畫效果
        this.setAnimationStyle(R.style.AnimBottom);
        //防止虛擬鍵擋住
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //設(shè)置彈出窗體的 寬赊琳,高
        this.setWidth(LayoutParams.WRAP_CONTENT);
        this.setHeight(LayoutParams.WRAP_CONTENT);
        // 設(shè)置可觸
        this.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0x0000000);
        this.setBackgroundDrawable(dw);
        // 單擊彈出窗以外處 關(guān)閉彈出窗
        mMenuView.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });
    }



}

用法街夭;

private void ShowTopPopwindow() {
    //顯示排序彈窗
    topPopupWindows = new TopPopupWindows(Pop4.this,myMenusOnClick);
    topPopupWindows.showAsDropDown(ChoseModelBut,0,0);
}
private View.OnClickListener myMenusOnClick = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        topPopupWindows.dismiss();
        switch (v.getId()) {
            //柱狀圖
            case R.id.zhu_layout:
                ChoseModelName.setText("柱狀圖");

                jiatu.setImageResource(R.drawable.zhuzhuang);
                break;
            //折線圖
            case R.id.zhexian_layout:
                ChoseModelName.setText("折線圖");
                jiatu.setImageResource(R.drawable.zhexian);

                break;
        }

    }
};

注意;

topPopupWindows.showAsDropDown(ChoseModelBut,0,0);
這行代碼決定了彈窗顯示的位置躏筏,第一個參數(shù)是你要顯示哪個控件下面的對象



即上面柱狀圖和三個點所在的Linelayout對象板丽,
這里可以換成任何你在上面的view控件 Imageview Imagebutton 隨意,只要找到控件id
然后設(shè)置在控件底部就ok



5.居中的dialog
package com.panghaha.it.manypopwindowanddialog.View;


import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.panghaha.it.manypopwindowanddialog.R;


/**
 * @author   龐世宇
 * @describe 自定義居中彈出dialog
 */
public class CenterDialog extends Dialog implements View.OnClickListener {

    private Context context;

    private int layoutResID;

    /**
     * 要監(jiān)聽的控件id
     */
    private int[] listenedItems;

    private OnCenterItemClickListener listener;

    public CenterDialog(Context context, int layoutResID, int[] listenedItems) {
        super(context, R.style.MyDialog);
        this.context = context;
        this.layoutResID = layoutResID;
        this.listenedItems = listenedItems;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        window.setGravity(Gravity.CENTER); // 此處可以設(shè)置dialog顯示的位置為居中
        window.setWindowAnimations(R.style.bottom_menu_animation); // 添加動畫效果
        setContentView(layoutResID);
        // 寬度全屏
        WindowManager windowManager = ((Activity) context).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = display.getWidth()*4/5; // 設(shè)置dialog寬度為屏幕的4/5
        getWindow().setAttributes(lp);
        // 點擊Dialog外部消失
        setCanceledOnTouchOutside(true);

        for (int id : listenedItems) {
            findViewById(id).setOnClickListener(this);
        }
    }

    public interface OnCenterItemClickListener {

        void OnCenterItemClick(CenterDialog dialog, View view);

    }

    public void setOnCenterItemClickListener(OnCenterItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View view) {
        dismiss();
        listener.OnCenterItemClick(this, view);
    }
}

使用趁尼;
這里對點擊事件先進行了dimiss()方法埃碱,所有點擊就會消失,可以不用手動dimiss

centerDialog.show();
centerDialog.setOnCenterItemClickListener(new CenterDialog.OnCenterItemClickListener() {
    @Override
    public void OnCenterItemClick(CenterDialog dialog, View view) {
        
        switch (view.getId()){
            case R.id.dialog_sure:
                Toast.makeText(MainActivity.this,"確定退出",Toast.LENGTH_SHORT).show();

                break;
        }
    }
});

6.底部彈出的dialog基本一樣就不貼代碼了
周圍變暗的dialog

package com.panghaha.it.manypopwindowanddialog.View;

/**
 * Created by pang on 2017/4/4.
 *  自定義 dialog對話框
 */
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.panghaha.it.manypopwindowanddialog.R;


public class MyDialog extends Dialog {
    private LinearLayout xiangce;
    private LinearLayout paizhao;
    private Context mcontext;
    private onPaizhao_OnclickListener onpaizhao_onclickListener;//拍照按鈕被點擊了的監(jiān)聽器
    private onXiangce_OnclickListener onxiangce_onclickListener;//相冊按鈕被點擊了的監(jiān)聽器

    /**
     * 設(shè)置確定按鈕的顯示內(nèi)容和監(jiān)聽
     *
     * @param
     * @param
     */
    public void set_paizhao_OnclickListener(onPaizhao_OnclickListener paizhao_onclick) {

        this.onpaizhao_onclickListener = paizhao_onclick;
    }
    /**
     * 設(shè)置取消按鈕的顯示內(nèi)容和監(jiān)聽
     *
     * @param
     * @param
     */
    public void set_xiangce_OnclickListener( onXiangce_OnclickListener xiangce_onclick) {

        this.onxiangce_onclickListener = xiangce_onclick;
    }

    public MyDialog(Context context) {
        super(context, R.style.MyDialog);
        this.mcontext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_touxiang);
        //按空白處取消動畫
        setCanceledOnTouchOutside(true);

        //初始化界面控件
        initView();
        //初始化界面控件的事件
        initEvent();
    }
    /**
     * 初始化界面的確定和取消監(jiān)聽器
     */
    private void initEvent() {
        //設(shè)置拍照 按鈕被點擊后酥泞,向外界提供監(jiān)聽
        paizhao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onpaizhao_onclickListener != null) {
                    onpaizhao_onclickListener.paizhao_onClick();
                }
            }
        });
        //設(shè)置相冊 按鈕被點擊后砚殿,向外界提供監(jiān)聽
        xiangce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onxiangce_onclickListener != null) {
                    onxiangce_onclickListener.xiangce_onClick();
                }
            }
        });
    }



    /**
     * 初始化界面控件
     */
    private void initView() {

        xiangce = (LinearLayout) findViewById(R.id.xiangce);
        paizhao = (LinearLayout) findViewById(R.id.paizhao);

        
    }

    /**
     * 動態(tài)設(shè)置Activity背景透明度
     *
     * @param isopen
     */
    public void setWindowAlpa(boolean isopen) {
        if (Build.VERSION.SDK_INT < 11) {
            return;
        }
        final Window window = ((Activity) mcontext).getWindow();
        final WindowManager.LayoutParams lp = window.getAttributes();
        window.setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND, WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        ValueAnimator animator;
        if (isopen) {
            animator = ValueAnimator.ofFloat(1.0f, 0.5f);
        } else {
            animator = ValueAnimator.ofFloat(0.5f, 1.0f);
        }
        animator.setDuration(400);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @TargetApi(Build.VERSION_CODES.HONEYCOMB)
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float alpha = (float) animation.getAnimatedValue();
                lp.alpha = alpha;
                window.setAttributes(lp);
            }
        });
        animator.start();
    }


    /**
     * 設(shè)置確定按鈕和取消被點擊的接口
     * 相冊 拍照
     */
    public interface onPaizhao_OnclickListener {
         void paizhao_onClick();
    }

    public interface onXiangce_OnclickListener {
         void xiangce_onClick();
    }
}

這里留出了手動設(shè)置Activity背景透明度的方法
使用;

myDialog = new MyDialog(MainActivity.this);
myDialog.show();
myDialog.setWindowAlpa(true);
myDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialog) {
        myDialog.setWindowAlpa(false);
    }
});
//拍照
myDialog.set_paizhao_OnclickListener(new MyDialog.onPaizhao_OnclickListener() {
    @Override
    public void paizhao_onClick() {

        Toast.makeText(MainActivity.this,"拍照",Toast.LENGTH_SHORT).show();

        myDialog.dismiss();
        myDialog.setWindowAlpa(false);
    }
});
//相冊
myDialog.set_xiangce_OnclickListener(new MyDialog.onXiangce_OnclickListener() {
    @Override
    public void xiangce_onClick() {

        Toast.makeText(MainActivity.this,"相冊",Toast.LENGTH_SHORT).show();

        myDialog.dismiss();
    }
});

監(jiān)聽dimiss時候把activity變暗芝囤,show的時候取消
7.小火箭彈窗之前寫過了似炎,跟5其實原理一樣辛萍,就是換個背景

最后 感謝閱讀
demo地址:https://github.com/PangHaHa12138/ManyPopWindowAndDialog

                              ~have a nice day~
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市羡藐,隨后出現(xiàn)的幾起案子叹阔,更是在濱河造成了極大的恐慌,老刑警劉巖传睹,帶你破解...
    沈念sama閱讀 212,383評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異岸晦,居然都是意外死亡欧啤,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,522評論 3 385
  • 文/潘曉璐 我一進店門启上,熙熙樓的掌柜王于貴愁眉苦臉地迎上來邢隧,“玉大人,你說我怎么就攤上這事冈在〉够郏” “怎么了?”我有些...
    開封第一講書人閱讀 157,852評論 0 348
  • 文/不壞的土叔 我叫張陵包券,是天一觀的道長纫谅。 經(jīng)常有香客問我,道長溅固,這世上最難降的妖魔是什么付秕? 我笑而不...
    開封第一講書人閱讀 56,621評論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮侍郭,結(jié)果婚禮上询吴,老公的妹妹穿的比我還像新娘。我一直安慰自己亮元,他們只是感情好猛计,可當我...
    茶點故事閱讀 65,741評論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著爆捞,像睡著了一般奉瘤。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上嵌削,一...
    開封第一講書人閱讀 49,929評論 1 290
  • 那天毛好,我揣著相機與錄音,去河邊找鬼苛秕。 笑死肌访,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的艇劫。 我是一名探鬼主播吼驶,決...
    沈念sama閱讀 39,076評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了蟹演?” 一聲冷哼從身側(cè)響起风钻,我...
    開封第一講書人閱讀 37,803評論 0 268
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎酒请,沒想到半個月后骡技,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,265評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡羞反,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,582評論 2 327
  • 正文 我和宋清朗相戀三年布朦,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片昼窗。...
    茶點故事閱讀 38,716評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡是趴,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出澄惊,到底是詐尸還是另有隱情唆途,我是刑警寧澤,帶...
    沈念sama閱讀 34,395評論 4 333
  • 正文 年R本政府宣布掸驱,位于F島的核電站肛搬,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏亭敢。R本人自食惡果不足惜滚婉,卻給世界環(huán)境...
    茶點故事閱讀 40,039評論 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望帅刀。 院中可真熱鬧让腹,春花似錦、人聲如沸扣溺。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,798評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽锥余。三九已至腹纳,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間驱犹,已是汗流浹背嘲恍。 一陣腳步聲響...
    開封第一講書人閱讀 32,027評論 1 266
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留雄驹,地道東北人佃牛。 一個月前我還...
    沈念sama閱讀 46,488評論 2 361
  • 正文 我出身青樓,卻偏偏與公主長得像医舆,于是被迫代替她去往敵國和親俘侠。 傳聞我的和親對象是個殘疾皇子象缀,可洞房花燭夜當晚...
    茶點故事閱讀 43,612評論 2 350

推薦閱讀更多精彩內(nèi)容