zoukankan      html  css  js  c++  java
  • 简单 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



  • 相关阅读:
    xutils3批量上传文件
    超详细Android面试的准备与经历分享 | 百度(offer)
    [网站公告]3月10日23:00-4:00阿里云SLB升级,会有4-8次连接闪断团队
    上周热点回顾(3.2-3.8)团队
    发布新博客皮肤red_autumnal_leaves团队
    上周热点回顾(2.23-3.1)团队
    上周热点回顾(2.16-2.22)团队
    上周热点回顾(2.2-2.8)团队
    如何禁止IE自动生成链接团队
    [云上天气预报-有时有闪电]2月3日23:00-4:00阿里云SLB升级期间网络会闪断团队
  • 原文地址:https://www.cnblogs.com/sharecenter/p/5621104.html
Copyright © 2011-2022 走看看