popupWindow的使用

来源:互联网 发布:新闻推荐系统数据集 编辑:程序博客网 时间:2024/06/05 04:46

前言

好久没有更新自己的博客了,这周在项目中有应用到,当服务端崩溃的时候,弹出Tips框,弹出后用户点击
Tip 框之外的区域要使得Tips消失,需求很简单,实现方式也有多种,这里借用该机会也学习使用一下popwindow。

简要介绍 ##

PopupWindow就是弹出窗口的意思,类似windows7电脑下面的开始按钮弹出的框。PopupWindow可以实现浮层效果,而且可以自定义显示位置,出现和退出时的动画。

使用方法

一、为popupwindow创建布局 ###

本人这里创建的布局文件名为shutdown_tip_popupwindow.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:background="@drawable/bg_homepage_watch_sync_tip"    android:orientation="vertical">    <TextView        android:id="@+id/shutdown_title"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:text="奔溃提示" />    <TextView        android:id="@+id/watch_sync_tip_sub_title"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginBottom="10dp"        android:lineSpacingExtra="10dp"        android:text="引起奔溃的主要原因如下:"        android:textColor="@color/color_watch_sync_tip_sub_title"        android:textSize="25dp" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginBottom="10dp"        android:orientation="horizontal">        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginRight="10dp"            android:layout_marginTop="10dp"            android:background="@drawable/shape_watch_sync_tip" />        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:lineSpacingExtra="10dp"            android:text="提示1..."            android:textColor="@color/color_watch_sync_tip_content"            android:textSize="25dp" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginBottom="10dp"        android:orientation="horizontal">        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginRight="10dp"            android:layout_marginTop="10dp"            android:background="@drawable/shape_watch_sync_tip" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:lineSpacingExtra="10dp"            android:text="提示2..."            android:textColor="#000000"            android:textSize="25dp" />    </LinearLayout></LinearLayout>

二、函数中初始化调用

//初始化两个变量  private PopupWindow popupWindow;  private View view_popupWindow;  private void showWindow(View parent) {        if (popupWindow == null) { //  由于这里是在fragment中调用getSystemService,所以先在onAttach()里面给homepageActivity赋值 /* public void onAttach(Context context) {        super.onAttach(context);        homepageActivity=(HomepageActivity) getActivity();    }*/            LayoutInflater layoutInflater = (LayoutInflater)homepageActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);            view_popupWindow = layoutInflater.inflate(R.layout.shutdown_tip_popupwindow, null); //此处是为了获取布局中的参数,方便设置popupwindow窗口的大小            LinearLayout.LayoutParams params = ( LinearLayout.LayoutParams)view_popupWindow.getLayoutParams();  //创建popupwindow,设置其大小为适合内容                             popupWindow = new PopupWindow(view_popupWindow, params.WRAP_CONTENT, params.WRAP_CONTENT);        }        popupWindow.setFocusable(true);        // 设置允许在外点击消失        popupWindow.setBackgroundDrawable(new BitmapDrawable());        popupWindow.setOutsideTouchable(true);        WindowManager windowManager = (WindowManager)homepageActivity.getSystemService(Context.WINDOW_SERVICE);        int xPos = windowManager.getDefaultDisplay().getWidth() / 2                - popupWindow.getWidth() / 2;        popupWindow.showAsDropDown(parent, xPos, 0);    }

总结

popupwindow使用起来还是很简单的,在完成上述代码编写后,在合适位置调用函数即可实现,其实在布局文件中还可以添加ListView等,也可以为他们设置点击监听,所以说popupwindow本身确实是很强大的,值得mark一下,为我自己的小菜成长之路积累一下。

原创粉丝点击