• 简单 android popupwindow 实现


    1、 popupWindow 设置大小;

    popupWindow = new  PopupWindow(view, ViewGroup.LayoutParams.FILL_PARENT,

                  ViewGroup.LayoutParams.WRAP_CONTENT);

    2、showAtLocation 方法是将popupWindow显示位置 ;

     popupWindow.showAtLocation(v, Gravity.CENTER_HORIZONTAL,
                            0,
                            0);

    3、PopUpWindow dimiss隐藏,需要 PopupWindow 显示之前设置它的背景不为空:如下面两行代码:

     ColorDrawable cd = new ColorDrawable(-0000);
     popupWindow.setBackgroundDrawable(cd);


    注意这里设置背景并不会覆盖xml文件定义的背景。

    4、 当有popupWindow.setFocusable(false);的时候,说明PopupWindow不能获得焦点,即使设置设置了背景不为空也不能点击外面消失,只能由dismiss()消失;

        但是外面的View的事件还是可以触发,back键也可以顺利dismiss掉。当设置为popupWindow.setFocusable(true);的时候,加上下面两行设置背景代码,点击外面和Back键才会消失。


    5、 // 这里设置显示PopupWindow之后在外面点击是否有效。如果为false的话,那么点击PopupWindow外面并不会关闭PopupWindow。当然这里很明显只能在Touchable下才能使用。

              popupWindow.setOutsideTouchable(true);

    下面给出一个简单的使用PopuWindow的实例:

    package com.popupwindow;


    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Context;

    import android.graphics.drawable.ColorDrawable;
    import android.text.SpannableString;
    import android.view.Gravity;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup.MarginLayoutParams;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.PopupWindow;
    import android.widget.TextView;

    public class MainActivity extends Activity
    {
        PopupWindow popupWindow;
        Button imgCall;
        private View view;
        private Context mContext;
        MarginLayoutParams margin;

        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mContext = this;
            
            imgCall = (Button) findViewById(R.id.showbutton);
            imgCall.setOnClickListener(new PopupOnClickListener());
            
            margin=new MarginLayoutParams(imgCall.getLayoutParams());
        }

        /**
         * 这个类主要显示PopuWindow,并显示之后对里面的按钮添加监听事件。
         */
        private class PopupOnClickListener implements OnClickListener {
            @Override
            public void onClick(View v) {
                System.out.println("onClick myCursor");
                switch (v.getId()) {
                case R.id.showbutton:
                    initPopupWindow();
                    // 加上下面两行可以用back键关闭popupwindow,否则必须调用dismiss();
                    // 需要顺利让PopUpWindow dimiss;PopUpWindow的背景不能为空。
                    // 当有popuWindow.setFocusable(false);的时候,说明PopuWindow不能获得焦点,并不能点击外面消失,只能由dismiss()消失。
                    // 当设置为popuWindow.setFocusable(true);的时候,加上下面两行代码才会消失
                    // 注意这里添加背景并不会覆盖原来的背景。
                    ColorDrawable cd = new ColorDrawable(-0000);
                    popupWindow.setBackgroundDrawable(cd);

                    popupWindow.showAtLocation(v, Gravity.CENTER_HORIZONTAL,
                            0,
                            0);

                    break;
                default:
                    break;
                }
            }
        }

        private void initPopupWindow() {
            view = getInfoWindow();//this.getLayoutInflater().inflate(R.layout.custom_info_window, null);
            popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            // 这里设置显示PopupWindow之后在外面点击是否有效。如果为false的话,那么点击PopupWindow外面并不会关闭PopuWindow。
            popupWindow.setFocusable(true);
            popupWindow.setOutsideTouchable(true);//不能在没有焦点的时候使用

        }

        @Override
        protected void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
            if (popupWindow.isShowing()) {
                popupWindow.dismiss();
            }
        }
        
        public View getInfoWindow()
        {
            View infoWindow = ((Activity) mContext).getLayoutInflater().inflate(
                    R.layout.custom_info_window, null);

            render(infoWindow);
            return infoWindow;
        }
        public void render( View view)
        {
            String title = "我的弹出框";
            TextView titleUi = ((TextView) view.findViewById(R.id.title));
            if(title != null)
            {
                SpannableString titleText = new SpannableString(title);
                titleUi.setText(titleText);

            }
            String snippet = "姓名:李VV 性别:男 出生日期:1990/12/12 12:10:05 所在地:北京";
            TextView snippetUi = ((TextView) view.findViewById(R.id.snippet));
            if(snippet != null)
            {
                SpannableString snippetText = new SpannableString(snippet);
                snippetUi.setText(snippetText);
            }
        }
        
    }


    具体下载路径:http://download.csdn.net/detail/q610098308/8211811



  • 相关阅读:
    InvalidIndexNameException[Invalid index name [2Shard], must be lowercase]
    Head插件——学习Elasticsearch的锋刃利器!
    Fiddler使用AutoResponder进行本地文件和线上文件的映射
    启动redis出现Creating Server TCP listening socket *:6379: bind: No such file or directory
    Project configuration is not up-to-date with pom.xml错误解决方法
    创建支持eclipse的多模块maven项目
    Eclipse添加默认的JRE
    错误:HttpServlet was not found on the Java
    Android插件化开发之Atlas初体验
    Android屏幕适配全攻略(最权威的官方适配指导)
  • 原文地址:https://www.cnblogs.com/sharecenter/p/5621104.html
Copyright © 2020-2023  润新知