• 弹出popwindow 背景变暗


    先看下效果图吧

    代码如下

    package com.example.administrator.popwindowdemo.view;
    
    import android.app.Activity;
    import android.content.Context;
    import android.graphics.drawable.ColorDrawable;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup.LayoutParams;
    import android.view.WindowManager;
    import android.widget.LinearLayout;
    import android.widget.PopupWindow;
    
    import com.example.administrator.popwindowdemo.R;
    
    /**
     * Created by Administrator on 2016/4/12.
     */
    public class CustomPopwindow extends PopupWindow {
        private View mView;
        public CustomPopwindow(Activity context,View.OnClickListener itemsOnClick){
            super(context);
            initView(context,itemsOnClick);
        }
    
        private void initView(final Activity context,View.OnClickListener itemsOnClick) {
            // TODO Auto-generated method stub
            LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mView = mInflater.inflate(R.layout.select_popwindow,null);
            LinearLayout ll_select01 = (LinearLayout) mView.findViewById(R.id.ll_select01);
            LinearLayout ll_select02 = (LinearLayout) mView.findViewById(R.id.ll_select02);
            LinearLayout ll_select03 = (LinearLayout) mView.findViewById(R.id.ll_select03);
            //取消按钮
            ll_select03.setOnClickListener(new View.OnClickListener() {
    
                public void onClick(View v) {
                    //销毁弹出框
                    dismiss();
                    backgroundAlpha(context, 1f);
                }
            });
            //设置按钮监听
            ll_select01.setOnClickListener(itemsOnClick);
            ll_select02.setOnClickListener(itemsOnClick);
            //设置SelectPicPopupWindow的View
            this.setContentView(mView);
            //设置SelectPicPopupWindow弹出窗体的宽
            this.setWidth(LayoutParams.FILL_PARENT);
            //设置SelectPicPopupWindow弹出窗体的高
            this.setHeight(LayoutParams.WRAP_CONTENT);
            //设置SelectPicPopupWindow弹出窗体可点击
            this.setFocusable(true);
            //设置PopupWindow可触摸
            this.setTouchable(true);
            //设置非PopupWindow区域是否可触摸
    //        this.setOutsideTouchable(false);
            //设置SelectPicPopupWindow弹出窗体动画效果
            this.setAnimationStyle(R.style.select_anim);
            //实例化一个ColorDrawable颜色为半透明
            ColorDrawable dw = new ColorDrawable(0x00000000);
            //设置SelectPicPopupWindow弹出窗体的背景
            this.setBackgroundDrawable(dw);
            backgroundAlpha(context,0.5f);//0.0-1.0
            this.setOnDismissListener(new OnDismissListener() {
    
                @Override
                public void onDismiss() {
                    // TODO Auto-generated method stub
                    backgroundAlpha(context, 1f);
                }
            });
        }
        /**
         * 设置添加屏幕的背景透明度
         * @param bgAlpha
         */
        public void backgroundAlpha(Activity context, float bgAlpha)
        {
            WindowManager.LayoutParams lp = context.getWindow().getAttributes();
            lp.alpha = bgAlpha;
            context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            context.getWindow().setAttributes(lp);
        }
    
    }

    在Activity中的代码:

    public class MainActivity extends AppCompatActivity {
        private CustomPopwindow customPopwindow;
        private Activity mActivity;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mActivity = this;
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
    
            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    customPopwindow = new CustomPopwindow(mActivity,itemsOnClick);
                    customPopwindow.showAtLocation(view,
                            Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
                }
            });
        }
        //为弹出窗口实现监听类
        private View.OnClickListener itemsOnClick = new View.OnClickListener(){
    
            public void onClick(View v) {
                customPopwindow.dismiss();
                customPopwindow.backgroundAlpha(mActivity,1f);
                switch (v.getId()) {
                    case R.id.ll_select01:
                        //do something
                        break;
                    case R.id.ll_select02:
                         //do something
    
                        break;
                    default:
                        break;
                }
            }
    
        };
    
    }

    源码下载:https://git.oschina.net/alicelsj/PopwindowDemo

  • 相关阅读:
    Field 'CID' doesn't have a default value
    HTTP Status 500
    Exceprtion:e createQuery is not valid without active transaction; nested exception is org.hibernate.HibernateException: createQuery is not valid without active transaction
    SSH框架中hibernate 出现 user is not mapped 问题
    解决Exception:Could not open Hibernate Session for transaction; nested exception is java.lang.NoClassDefFoundError: org/hibernate/engine/transaction/spi/TransactionContext
    在浏览器上直接输入 http://www.bookEstore.com就可以访问工程问题
    JavaWeb之动态页面技术JSP/EL/JSTL
    ConvertUtils.reqister注册转换器
    编写组件TComponent published $M+ 问题
    inherited 的研究。
  • 原文地址:https://www.cnblogs.com/lushiju/p/5385949.html
Copyright © 2020-2023  润新知