popupWindow的使用

前言

好久没有更新自己的博客了,这周在项目中有应用到,当服务端崩溃的时候,弹出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一下,为我自己的小菜成长之路积累一下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值