您的位置:首页 > 移动开发 > Android开发

Android PopupWindow简单解析

2016-04-09 13:45 639 查看
使用过QQ的各位看官,肯定都用过手机版的QQ,QQ里面有个简单的效果,就是在消息页面长按一个聊天对象时,会弹出一个删除/置顶的选项,这个就是今天要写的popupwindow

在sdk的文档中,对于popupwindow的解释是这样的:A popup window that can be used to display an arbitrary view. The popup window is a floating container that appears on top of the current activity.换成中文的意思应该是:一个弹出窗口,可以用来显示任意视图。出现的弹出窗口是一个浮动容器的当前活动。所以,这是一个弹出弹出窗口来着的,

在sdk的解释中。实例化一个popupwindow好几种方法:

PopupWindow(Context context)
Create a new empty, non focusable popup window of dimension (0,0).

PopupWindow(Context context,
AttributeSet attrs)
Create a new empty, non focusable popup window of dimension (0,0).

PopupWindow(Context context,
AttributeSet attrs, int defStyleAttr)
Create a new empty, non focusable popup window of dimension (0,0).

PopupWindow(Context context,
AttributeSet attrs, int defStyleAttr, int defStyleRes)
Create a new, empty, non focusable popup window of dimension (0,0).

PopupWindow()
Create a new empty, non focusable popup window of dimension (0,0).

PopupWindow(View contentView)
Create a new non focusable popup window which can display the contentView.

PopupWindow(int width, int height)
Create a new empty, non focusable popup window.

PopupWindow(View contentView, int width, int height)
Create a new non focusable popup window which can display the contentView.

PopupWindow(View contentView, int width, int height, boolean focusable)
Create a new popup window which can display the contentView.

我今天用的是最后的一种,因为觉得最后一种足够用了。
相信对popupwindow的效果,我不需要再去重复重复再重复,接下来,我会用一个简单的demo来简单的使用popupwindow,

接下来就是上代码:

//PopupWindow这个类用来实现一个弹出框,可以使用任意布局的View作为其内容,这个弹出框是悬浮在当前activity之上的。

//对popupwindow进行较为简单的操作,未添加animation。update等操作

import android.app.Activity;

import android.graphics.drawable.BitmapDrawable;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.LinearLayout.LayoutParams;

import android.widget.PopupWindow;

import android.widget.Toast;

import android.widget.PopupWindow.OnDismissListener;

public class MainActivity extends Activity {

    Button btn1;

    PopupWindow popupwindow;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        btn1 = (Button) this.findViewById(R.id.button1);

        btn1.setOnClickListener(new OnClickListener() {

            @Override

            public void onClick(View v) {

                View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.pop_window, null);

                Button button1 = (Button) contentView.findViewById(R.id.button1);

                // 开始对人popupwindow的操作

                // 初始化,显示

                popupwindow = new PopupWindow(contentView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);

                // popupwindow.setOutsideTouchable(true);

                popupwindow.setBackgroundDrawable(new BitmapDrawable());

                popupwindow.showAsDropDown(button1);

                popupwindow.setOnDismissListener(new OnDismissListener() {

                    @Override

                    public void onDismiss() {

                        Toast.makeText(MainActivity.this, "dismiss", Toast.LENGTH_SHORT).show();

                    }

                });

            }

        });

    }

}

//这个demo简单的使用了popupwindow,并在屏幕的左上角显示了一个contentview,并只在view中显示了一个按钮,以及添加了一个dismiss事件,顾名思义,dismiss的作用是在popupwindow消失时触发的,怎样才会使得popupwindow消失??按照使用QQ的经历。可以知道,当我们点击contemtview以外的区域时,popupwindow便会消失,所以我们需要设置代码使得这个popupwindow可以通过点击外部区域,然后消失,我本以为只需要设置popupwindow.setOutsideTouchable(true);便是可以让他有点击外部的权利。但是发现是我想太多了,最后查看了博客,发现要设置 
popupwindow.setBackgroundDrawable(new BitmapDrawable());才会使得外部点击生效,然而我还是不I懂为什么,只能简单的理解为我的渣,

好了,这边博客基本到这儿,而关于动画效果,以及update,我就不阐述了,因为sdk的解释我觉得挺全面的,最后再返回一点,在设置layoutparams的时候,如果I设置为-1,-1,很不兴的,你的外部点击事件就会被覆盖掉,为什么???因为-1,-1代表的是fill_parent,也就是父容器,所以你返现点击button就会无效




我向大家应该看得懂图片顺序,希望大家对评论,多交流交流
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android popupwindow