Android PopupWindow详解

Android Popupwindow 详解


在Android中弹出式菜单(以下称弹窗)是使用十分广泛一种菜单呈现的方式,弹窗为用户交互提供了便利。关于弹窗的实现大致有以下两种方式AlertDialog和PopupWindow,当然网上也有使用Activity并配合Dialog主题的方式实现弹窗,有兴趣的朋友也可以去研究一下。对于AlertDialog和PopupWindow两者的最主要区别也有以下两点:

1 、位置是否固定。 AlertDialog在位置显示上是固定的,而PopupWindow则相对比较随意,能够在主屏幕上的任意位置显示。

2、是否会阻塞UI线程。 AlertDialog在显示的时候不会阻塞UI线程,而PopupWindow在显示的时候会阻塞UI线程。

也就是说PopupWindow是一个以弹窗方式呈现的控件,可以用来显示任意视图(View),而且会浮动在当前活动(activity)的顶部”。因此我们可以通过PopupWindow实现各种各样的弹窗效果,进行信息的展示或者是UI交互,由于PopupWindow自定义布局比较方便,而且在显示位置比较自由不受限制,因此受到众多开发者的青睐。


一、介绍:

1、构造方法

(1)public PopupWindow(View contentView, int width, int height, boolean focusable)

contentView为要显示的view,width和height为宽和高,值为像素值,也可以是MATCHT_PARENT和WRAP_CONTENT。

还可以

(2) public PopupWindow (Context context)

或者


(3) public PopupWindow(View contentView, int width, int height)

或者


(4) public PopupWindow(View contentView)

其中第一种最省事,构造函数中设置了要显示的View,宽度 ,高度以及是否能获得焦点。以上是几种用的比较常见的构造方法。

笔记:可通过重新构造构造方法在Activity和PopupWindows中进行值传递。


2、获得PopupWindow的视图内容

(1) public View getContentView()


显示PopupWindow:

showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移

showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移

showAtLocation(View parent, int gravity, int x, int y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移

其实我发现showAtLocation的parent参数可以很随意,只要是activity中的view都可以。


3、大小:

有两种方法设置PopupWindow的大小:

调用有宽高参数的构造函数:

(1)LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

(2)View contentview = inflater.inflate(R.layout.popup_process, null);

(3)PopupWindow popupWindow = new PopupWindow(contentview,LayoutParams.WRAP_CONTENT,

(4)LayoutParams.WRAP_CONTENT);

通过setWidth和setHeight设置

(1)PopupWindow popupWindow = new PopupWindow(contentview);

(2)popupWindow.setWidth(LayoutParams.WRAP_CONTENT);           

(3)popupWindow.setHeight(LayoutParams.WRAP_CONTENT);

两种办法是等效的,不管采用何种办法,必须设置宽和高,否则不显示任何东西.


这里的WRAP_CONTENT可以换成fill_parent 也可以是具体的数值,它是指PopupWindow的大小,也就是contentview的大小,注意popupwindow根据这个大小显示你的View,如果你的View本身是从xml得到的,那么xml的第一层view的大小属性将被忽略。相当于popupWindow的width和height属性直接和第一层View相对应。


4、PopUpWindow的焦点:

setFocusable设置PopupWindow的焦点,一般资料对此的解释都是:是否让Popupwindow可以点击但是这揭示了本质却与直观现象不符。实际上,

如果:


setFocusable(true);


则PopUpWindow本身可以看作一个类似于模态对话框的东西(但有区别),PopupWindow弹出后,所有的触屏和物理按键都有PopupWindows处理。其他任何事件的响应都必须发生在PopupWindow消失之后, (home  等系统层面的事件除外)。比如这样一个PopupWindow出现的时候,按back键首先是让PopupWindow消失,第二次按才是退出activity,准确的说是想退出activity你得首先让PopupWindow消失,因为不并是任何情况下按back  PopupWindow都会消失,必须在PopupWindow设置了背景的情况下 。

如果PopupWindow中有Editor的话,focusable要为true。

setFocusable(false);

PopUpWindow只是一个浮现在当前界面上的view而已,不影响当前界面的任何操作。

是一个“没有存在感”的东西。

一般情况下setFocusable(true);


5、点击空白处的时候让PopupWindow消失

关于PopupWindow最搞笑的地方是setOutsideTouchable方法,原本以为如果你setOutsideTouchable(true)则点击PopupWindow之外的地方PopupWindow会消失,其实这玩意儿好像一点用都没有。

要让点击PopupWindow之外的地方PopupWindow消失你需要调用setBackgroundDrawable(new BitmapDrawable());

设置背景,为了不影响样式,这个背景是空的。还可以这样写,觉得这样要保险些:


setBackgroundDrawable(new ColorDrawable(0x00000000));


背景不为空但是完全透明。如此设置还能让PopupWindow在点击back的时候消失。其实一直觉得很奇怪,不明白为什么一个背景会影响点击事件,只知道这样用可行。

后来在评论中有人回答了此问题:


如果有背景,则会在contentView外面包一层PopupViewContainer之后作为mPopupView,如果没有背景,则直接用contentView作为mPopupView。

而这个PopupViewContainer是一个内部私有类,它继承了FrameLayout,在其中重写了Key和Touch事件的分发处理。


二、源代码

在Activity添加一个MV按钮,点击此按钮后弹出Popupwindow,Popupwindow中添加四个按钮来做具体的操作;并把Activity中的一个变量传递给Popupwindow。

1、布局

(1)Popupwindow布局

activity_mv.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop = "true"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
            <Button
                android:id="@+id/Btn_png0"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#20000000"
                android:text="no"
                />
            <Button
                android:id="@+id/Btn_png1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#20000000"
                android:layout_weight="1"
                android:text="snow"
                />
            <Button
                android:id="@+id/Btn_png2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#20000000"
                android:layout_weight="1"
                android:text="png2"
                />
            <Button
                android:id="@+id/Btn_png3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#20000000"
                android:layout_weight="1"
                android:text="png3"
                />
        </LinearLayout>

</RelativeLayout>

(2)Activity布局

activity_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <RelativeLayout
        android:id="@+id/record_fullpreview_buffer"
        android:layout_width="1dip"
        android:layout_height="1dip">
        <SurfaceView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/record_fullpreview_view"/>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/record_fullpreview_area"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <SurfaceView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/record_fullsurface_view"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="返回"
            android:id="@+id/record_fullreturn_button"
            android:layout_alignTop="@+id/record_fullswitch_camera_button"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="切换"
            android:id="@+id/record_fullswitch_camera_button"
            android:layout_alignParentBottom="true"
            android:layout_toRightOf="@id/record_fullreturn_button"
            android:layout_toEndOf="@id/record_fullreturn_button" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="录制"
            android:id="@+id/recording_full_button"
            android:layout_alignTop="@id/record_fullswitch_camera_button"
            android:layout_toRightOf="@id/record_fullswitch_camera_button"
            android:layout_toEndOf="@id/record_fullswitch_camera_button" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="MV"
            android:id="@+id/mv_fullbutton"
            android:layout_alignTop="@id/recording_full_button"
            android:layout_toRightOf="@id/recording_full_button"
            android:layout_toEndOf="@id/recording_full_button" />

    </RelativeLayout>


</LinearLayout>


2、代码

(1)Activity

                    case R.id.mv_fullbutton:
                        Log.e("zhaodb", "mv button clicked!");
                        LayoutInflater inflater = LayoutInflater.from(YXRecordFullScreenActivity.this);
                        // 引入窗口配置文件
                        View view = inflater.inflate(R.layout.activity_yxmv, null);
                        // 创建PopupWindow对象
                        Intent intent = new Intent();
                        intent.putExtra( "YXVideoEditInterface", m_videoEdit);
                        final YXMVActivity pop = new YXMVActivity(view, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, false,intent);
                        // 需要设置一下此参数,点击外边可消失
                        pop.setBackgroundDrawable(new BitmapDrawable());
                        //设置点击窗口外边窗口消失
                        pop.setOutsideTouchable(true);
                        // 设置此参数获得焦点,否则无法点击
                        pop.setFocusable(true);
                        pop.showAtLocation(v, Gravity.TOP,0,0);

                        break;



(2)Popupwindow

public class YXMVActivity extends PopupWindow {
    private static String TAG = "YXMVActivity";
    private View m_contentView = null;
    YXVideoEditInterface m_videoEdit = null;
    Button m_BtnNo = null;
    Button m_BtnPng1 = null;
    Button m_BtnPng2 = null;
    Button m_BtnPng3 = null;
    Button.OnClickListener m_BtnListener = null;

    public YXMVActivity(View contentView, int width, int height, boolean focusable, Intent intent){
        super(contentView,width,height,focusable);
        m_videoEdit = (YXVideoEditInterface) intent.getSerializableExtra("YXVideoEditInterface");
        m_contentView = contentView;

        m_BtnListener = new Button.OnClickListener(){

            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.Btn_png0:
                        Log.e(TAG,"png0");
                        m_videoEdit.changeCameraFrontPngSequence("",true);
                        break;
                    case R.id.Btn_png1:
                        Log.e(TAG,"png1");
                        m_videoEdit.changeCameraFrontPngSequence("/sdcard/VideoEdit/frontpng/xuehua",true);
                        break;
                    case R.id.Btn_png2:
                        Log.e(TAG,"png2");
                        break;
                    case R.id.Btn_png3:
                        Log.e(TAG,"png3");
                        break;
                    default:
                        Log.e(TAG,"click error!");

                }
            }
        };

        init();

    }

    private void init() {
        Log.i(TAG,"init,enter!");

        m_BtnNo = (Button) m_contentView.findViewById(R.id.Btn_png0);
        m_BtnNo.setOnClickListener(m_BtnListener);

        m_BtnPng1 = (Button) m_contentView.findViewById(R.id.Btn_png1);
        m_BtnPng1.setOnClickListener(m_BtnListener);

        m_BtnPng2 = (Button) m_contentView.findViewById(R.id.Btn_png2);
        m_BtnPng2.setOnClickListener(m_BtnListener);

        m_BtnPng3 = (Button) m_contentView.findViewById(R.id.Btn_png3);
        m_BtnPng3.setOnClickListener(m_BtnListener);

    }
}

笔记:其中涉及到的难点:1、在Popupwindow中注册和响应按键动作,通过构造函数传递过来的view v参数来注册按键。2、从Activity中传递值到Popupwindow中,通过重写构造函数,添加一个intent参数传递。


注:此文章在介绍部分参考了博客:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0702/1627.html

http://www.jianshu.com/p/825d1cc9fa79。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
嗨!对于你关于 Android PopupWindow 的问题,我可以给你一些基本的信息。PopupWindowAndroid 中的一个弹出式窗口,它可以覆盖在其他视图之上,提供一种临时性的用户界面。你可以使用 PopupWindow 来显示额外的内容、菜单或者交互提示。 要创建一个 PopupWindow,首先你需要实例化一个 PopupWindow 对象,并为其设置一些基本属性,例如宽度、高度、背景等。然后,你可以将任何视图或布局添加到 PopupWindow 中,并通过设置位置参数来控制它的显示位置。 下面是一个简单的示例代码,展示如何创建和显示一个 PopupWindow: ```java // 创建一个 PopupWindow 对象 PopupWindow popupWindow = new PopupWindow(context); // 设置 PopupWindow 的宽度和高度 popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); // 设置 PopupWindow 的内容视图 View contentView = LayoutInflater.from(context).inflate(R.layout.popup_layout, null); popupWindow.setContentView(contentView); // 设置 PopupWindow 的背景 popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // 显示 PopupWindow popupWindow.showAtLocation(anchorView, Gravity.CENTER, 0, 0); ``` 在上面的示例中,我们创建了一个 PopupWindow 对象,并设置了宽度和高度为包裹内容。然后,我们通过调用 `setContentView` 方法将一个自定义的布局文件 `R.layout.popup_layout` 添加到 PopupWindow 中。最后,我们使用 `showAtLocation` 方法将 PopupWindow 显示在屏幕中央。 希望这些信息对你有帮助!如果你对 PopupWindow 有更多的问题,或者需要更详细的示例代码,请随时告诉我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值