效果图

popup_window_addition.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/addition_popup_window_background"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingTop="15dp"
android:paddingRight="15dp"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="15dp"
android:drawableLeft="@mipmap/ic_chat"
android:drawablePadding="15dp"
android:gravity="center"
android:text="发起群聊"
android:textColor="@android:color/white" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:drawableLeft="@mipmap/ic_add_friend"
android:drawablePadding="15dp"
android:gravity="center"
android:text="添加朋友"
android:textColor="@android:color/white" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:drawableLeft="@mipmap/ic_scan"
android:drawablePadding="15dp"
android:gravity="center"
android:text="扫一扫"
android:textColor="@android:color/white" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:drawableLeft="@mipmap/ic_receive_pay"
android:drawablePadding="15dp"
android:gravity="center"
android:text="收付款"
android:textColor="@android:color/white" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:drawableLeft="@mipmap/ic_feedback"
android:drawablePadding="15dp"
android:gravity="center"
android:text="帮助与反馈"
android:textColor="@android:color/white" />
</LinearLayout>

AdditionPopupWindow.java

package com.bu_ish.popup_window_example;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow; public class AdditionPopupWindow extends PopupWindow {
public AdditionPopupWindow(Context context) {
setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
setBackgroundDrawable(new ColorDrawable());
setOutsideTouchable(true);
View view = LayoutInflater.from(context).inflate(R.layout.popup_window_addition, null);
setContentView(view);
}
}

MainActivity.java

package com.bu_ish.popup_window_example;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.addImageView).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AdditionPopupWindow popupWindow = new AdditionPopupWindow(MainActivity.this);
popupWindow.showAsDropDown(v);
}
});
}
}

注意

要想点击外部关闭PopupWindow,必须调用setBackgroundDrawable(Drawable)和setOutsideTouchable(boolean)

完整Demo链接:https://pan.baidu.com/s/1NF5-SUaOyHOWRWCFKfTWCA,提取码:c59o

Android笔记之自定义PopupWindow的更多相关文章

  1. Android笔记之自定义对话框

    效果如下图 对话框布局 dialog_uninstallation_confirmation.xml <?xml version="1.0" encoding="u ...

  2. Android笔记之自定义的RadioGroup、RadioButton,以及View实例状态的保存与恢复

    效果图 activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLay ...

  3. Android 自定义PopupWindow动画效果

    public class RollActivity extends Activity { private View view; private Button btn; private PopupWin ...

  4. Android学习笔记_34_自定义窗口标题

    1.建好项目之后在它的layout文件夹下创建一个title.xml文件,作为自定义窗口标题的文件. <?xml version="1.0" encoding="u ...

  5. 自定义PopupWindow弹出框(带有动画)

    使用PopupWindow来实现弹出框,并且带有动画效果 首先自定义PopupWindow public class LostPopupWindow extends PopupWindow { pub ...

  6. Android笔记:触摸事件的分析与总结----TouchEvent处理机制

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://glblong.blog.51cto.com/3058613/1559320   ...

  7. 自定义PopupWindow动画效果

    public class RollActivity extends Activity { private View view; private Button btn; private PopupWin ...

  8. 我的Android进阶之旅------>Android如何通过自定义SeekBar来实现视频播放进度条

    首先来看一下效果图,如下所示: 其中进度条如下: 接下来说一说我的思路,上面的进度拖动条有自定义的Thumb,在Thumb正上方有一个PopupWindow窗口,窗口里面显示当前的播放时间.在Seek ...

  9. Android笔记--View绘制流程源码分析(一)

    Android笔记--View绘制流程源码分析 View绘制之前框架流程分析 View绘制的分析始终是离不开Activity及其内部的Window的.在Activity的源码启动流程中,一并包含 着A ...

随机推荐

  1. FNV与FNV-1a Hash算法说明【转】

    转自:http://blog.csdn.net/jiayanhui2877/article/details/12090575 The core of the FNV hash The core of ...

  2. Delphi GDI对象之脱屏位图(Offscreen Bitmaps),也叫内存位图

    http://www.cnblogs.com/pchmonster/archive/2012/07/09/2583613.html 脱屏位图(Offscreen Bitmaps) 脱屏位图,也叫内存位 ...

  3. 002如何升级 Linux 的内核?

    我们不应该升级 Linux 内核,而是始终使用 rpm 命令来安装新的内核,因为升级内核会让你的 Linux 机器处于一个无法启动的状态.

  4. Codeforces Gym 100203H Highways 最小生成树

    原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf 题解 给你平面上若干点,生成一颗完全图,让 ...

  5. repeated-substring-pattern

    https://leetcode.com/problems/repeated-substring-pattern/ 下面这个方法,开始我觉得挺好.可惜还是超时了.后来我就加了一个剪枝策略,只有长度能够 ...

  6. Spring核心(ioc控制反转)

     IoC,Inversion Of Control 即控制反转,由容器来管理业务对象之间的依赖关系,而非传统方式中的由代码来管理. 其本质.即将控制权由应用程序代码转到了外部容器,控制权的转移就是 ...

  7. TCP三次握手与DDOS攻击原理

    TCP三次握手与DDOS攻击原理 作者:冰盾防火墙 网站:www.bingdun.com 日期:2014-12-09   在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接. ...

  8. 小谈Vim打开文件开头的&lt;feff&gt;

    在本地Windows机上开发的PHP程序上传到linuxserver上后,通过浏览器訪问对应接口.发现返回的数据前多了一个莫名的字符'-',甚为不解.之后通过网络抓包的方式,查看到接口返回数据前多了 ...

  9. C++继承:公有,私有,保护(转)

    公有继承(public).私有继承(private).保护继承(protected)是常用的三种继承方式. 1. 公有继承(public) 公有继承的特点是基类的公有成员和保护成员作为派生类的成员时, ...

  10. Word Ladder II——找出两词之间最短路径的所有可能

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...