zoukankan      html  css  js  c++  java
  • 悬浮对话框的实现

    直接上代码:

        private void setDialog(){
            View view = getLayoutInflater().inflate(R.layout.dialog_country, null);
            mDialog = new Dialog(this);
            mDialog.setContentView(view, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            Window window = mDialog.getWindow();
            WindowManager.LayoutParams wl = window.getAttributes();
            wl.x = 0;
            wl.y = getWindowManager().getDefaultDisplay().getHeight();
            wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
            wl.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            mDialog.onWindowAttributesChanged(wl);
            mDialog.setCanceledOnTouchOutside(true);
            mDialog.show();
            
            Button btnChina = (Button) view.findViewById(R.id.btnChina);
            Button btnUSA = (Button) view.findViewById(R.id.btnUSA);
            Button btnUK = (Button) view.findViewById(R.id.btnUK);
            Button btnCancel = (Button) view.findViewById(R.id.btnCancel);
            
            btnChina.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    mQuery.id(R.id.btnCountry).getTextView().setText("China");
                    mDialog.dismiss();
                }            
            });
            btnUSA.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    mQuery.id(R.id.btnCountry).getTextView().setText("USA");
                    mDialog.dismiss();
                }            
            });
            btnUK.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    mQuery.id(R.id.btnCountry).getTextView().setText("UK");
                    mDialog.dismiss();
                }            
            });
            btnCancel.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    mDialog.dismiss();
                }            
            });
        }

    首先inflate一个xml文件,产生一个view;再创建一个Dialog,设置Dialog的View为inflate的view;然后通过WindowManager.LayoutParams设置Dialog的长度、宽度、显示的位置等等,最后setCanceledOnTouchOutside(true),点击其他地方可以退出Dialog。运行效果如下:

  • 相关阅读:
    利用Lambda获取属性名称
    Entity Framework 6.0 源码解读笔记(一)
    [转]Sql server2005中如何格式化时间日期
    python之路_RabbitMQ相关介绍
    python之路_redis相关介绍
    python之路_django之contenttype介绍
    python之路_最简单的Git介绍
    python之路_rest-framework之分页、路由、视图、渲染
    python之路_rest-framework之版本、解析器、序列化
    python之路_rest-framework之认证、权限、频率
  • 原文地址:https://www.cnblogs.com/mstk/p/5332656.html
Copyright © 2011-2022 走看看