popupWindow

@Override
    public void onItemClick(AdapterView parent, View view, int position,
            long id) {
        // 弹窗

        // contentView-显示的View, width, height-宽高
    /*  TextView contentView = new TextView(mContext);
        contentView.setText("弹窗");
        contentView.setTextSize(20);
        contentView.setBackgroundColor(Color.RED);*/
        
        View contentView = View.inflate(mContext, R.layout.pop_window_layout, null);

        final PopupWindow window = new PopupWindow(contentView,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        
        //设置默认只显示一个弹窗
        window.setBackgroundDrawable(new ColorDrawable());
        window.setFocusable(true);
        window.setOutsideTouchable(true);//设置外围点击
        
        //设置动画样式
        window.setAnimationStyle(R.style.pop_anim);
        
        //显示
        //window.showAsDropDown(view);//在哪个控件下面显示
        //window.showAsDropDown(view, 80, -60);
        
        AppBean bean = mListDatas.get(position);
        final String packageName = bean.packageName;
        
        //parent是popupWindow要在哪个夫容器里面展示
        window.showAtLocation(parent, Gravity.CENTER, 0, 0);//显示在指定位置
        
        TextView tvUninstall = (TextView) contentView.findViewById(R.id.tv_uninstall);
        TextView tvOpen = (TextView) contentView.findViewById(R.id.tv_open);
        TextView tvShare = (TextView) contentView.findViewById(R.id.tv_share);
        TextView tvInfo = (TextView) contentView.findViewById(R.id.tv_info);
        
        
        
        //判断显示或者隐藏
        tvUninstall.setVisibility(bean.isSystem ? View.GONE : View.VISIBLE);
        
        PackageManager pkgMgr = getPackageManager();
        final Intent intent = pkgMgr.getLaunchIntentForPackage(packageName);
        tvOpen.setVisibility(intent == null ? View.GONE : View.VISIBLE);
        
        
        tvUninstall.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                //实现卸载功能
                  /* 
                    
                    
                    
                    
                */
                
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                intent.setAction("android.intent.action.DELETE");
                intent.addCategory("android.intent.category.DEFAULT");
                intent.setData(Uri.parse("package:" + packageName));
                startActivity(intent);
                //startActivityForResult(intent, requestCode)不能实现监听卸载完成
                
                window.dismiss();
            }
        });

window.dismiss();

public class AppBean {
    public Drawable icon;//图标 
    public String name;
    public boolean isInstallSD;//是否安装在SD卡
    public String space;//应用大小
    public boolean isSystem;//是否为系统应用
    public String packageName;
}




    

    

    

    







    






        


TWO

LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.pop_login, null);
//        View v = View.inflate(getContext(),R.layout.pop_login, null);


        popupWindow = new PopupWindow(v, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setFocusable(true);
        popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        popupWindow.setOutsideTouchable(true);
        //设置渐入、渐出动画效果
        popupWindow.setAnimationStyle(android.R.style.Animation_Toast);
        popupWindow.setTouchable(true);
        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                    popupWindow.dismiss();
                    return true;
                }
                return false;
            }
        });

   // popupWindow.update();
        popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
//        popupWindow.showAsDropDown(view,100,-150);

点击事件中的view就是父容器

public void onViewClicked(View view) {
    switch (view.getId()) {
case R.id.rl_touxiang_personalCenter:
    showPopWindow(view);
    break;
    }
}

你可能感兴趣的:(popupWindow)