码迷,mamicode.com
首页 > Windows程序 > 详细

自定义popupwindow二三事

时间:2016-01-25 17:16:55      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:

效果图:

技术分享

代码:

public class ViewActivity extends Activity implements View.OnClickListener {

    PopupWindow popWindow;
    LinearLayout rootView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view);

        rootView = (LinearLayout) findViewById(R.id.ll_rootview);

        initPopWindow(this);
    }

    @Override
    public void onClick(View v) {
        popWindow.showAtLocation(rootView, Gravity.CENTER, 0, 0);
    }

    public void initPopWindow(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_share, null);
        popWindow = new PopupWindow(view, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT, true);

        ImageView iv_cancel = (ImageView) view.findViewById(R.id.iv_cancel);
        iv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });

        //给popupwindow设置返回键监听
        popWindow.getContentView().setFocusableInTouchMode(true);
        popWindow.getContentView().setFocusable(true);
        popWindow.getContentView().setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK) {
                    if (popWindow != null && popWindow.isShowing()) {
                        popWindow.dismiss();
                    }
                    return true;
                }
                return false;
            }
        });
    }
}

背景是否为灰色:

//直接在view_share的根布局设置背景颜色就可以
  android:background="#aa666666"

自定义popupwindow二三事

标签:

原文地址:http://blog.csdn.net/pengkv/article/details/50580196

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!