第一步:自定义xml文件

第二步:获取LayoutInflater对象

第三步:调用inflate()方法获取View对象

第四步:创建PopupWindow对象

第五步:调用PopupWindow的showAsDropDown或者showAsLocation方法显示对话框

activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="es.source.code.activity.popupwindowtest_tencentclass.MainActivity"> <Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="点击"
android:onClick="btnClick"/>
</RelativeLayout>

layout_myself_dialog.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:orientation="vertical"
android:background="#fff"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#0e2438"
android:gravity="center"
android:orientation="horizontal"
>
<TextView
android:text="提醒"
android:textSize="18sp"
android:textColor="#fff"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" android:gravity="center"
android:orientation="vertical"
android:padding="10dp"
>
<TextView
android:text="恭喜您,验证码发送成功"
android:textSize="18sp"
android:textColor="#000"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <Button
android:id="@+id/btn"
android:text="确定"
android:textColor="#fff"
android:textSize="14sp"
android:layout_width="66dp"
android:layout_height="30dp"
android:background="@drawable/bg4"
android:layout_marginTop="10dp"/> </LinearLayout> </LinearLayout>

  

MainActivity.class

public class MainActivity extends AppCompatActivity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //View view = this.getLayoutInflater().inflate(R.layout.layout_myself_dialog, null); LayoutInflater inflater = LayoutInflater.from(this);
View myView = inflater.inflate(R.layout.layout_myself_dialog, null); popupWindow = new PopupWindow(myView, 600, 450);
myView.findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
} PopupWindow popupWindow;
public void btnClick(View v)
{
popupWindow.showAsDropDown(v);
} }

  

android入门 — PopupWindow的更多相关文章

  1. android入门——UI(6)——ViewPager+Menu+PopupWindow

    一.使用ViewPager开发新特性引导界面 <?xml version="1.0" encoding="utf-8"?> <Relative ...

  2. Android入门(十二)SQLite事务、升级数据库

    原文链接:http://www.orlion.ga/610/ 一.事务 SQLite支持事务,看一下Android如何使用事务:比如 Book表中的数据都已经很老了,现在准备全部废弃掉替换成新数据,可 ...

  3. Android 中PopupWindow使用 (转)

    参考学习后遇到问题: 要引用:有好几个,可以用错误提示解决: import android.widget.PopupWindow; import android.widget.Toast; Activ ...

  4. 【转】Xamarin.Android 入门之:Xamarin+vs2015 环境搭建

    Xamarin.Android 入门之:Xamarin+vs2015 环境搭建   一.前言 此篇博客主要写了如何使用搭建xamarin开发的环境,防止我自己万一哪天电脑重装系统了,可以直接看这篇博客 ...

  5. android 入门 006(sqlite增删改查)

    android 入门 006(sqlite增删改查) package cn.rfvip.feb_14_2_sqlite; import android.content.Context; import ...

  6. android 入门 005(登录记住)

    android 入门 005(登录记住) <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android ...

  7. Android入门:绑定本地服务

    一.绑定服务介绍   前面文章中讲过一般的通过startService开启的服务,当访问者关闭时,服务仍然存在: 但是如果存在这样一种情况:访问者需要与服务进行通信,则我们需要将访问者与服务进行绑定: ...

  8. Android入门视频推荐

      marschen老师的Android入门视频推荐网址: 1.Android应用程序开发视频教程(重制版)第一季 2.Android应用开发视频教程(重制版)第二季 2.marschen老师的个人微 ...

  9. Android:PopupWindow简单弹窗改进版

    Android:PopupWindow简单弹窗 继续上一节的内容,改进一下,目标是点击菜单后把菜单收缩回去并且切换内容,我使用的是PopupWindow+RadioGroup public class ...

随机推荐

  1. UIDynamic-吸附-重力-碰撞-物理仿真动画

    现实生活中: 运动场==物理仿真器 跑步==物理仿真行为 人==仿真元素 创建步骤: 1.创建物理仿真器,并且指定仿真范围 2.创建物理仿真行为,并且指定仿真元素 3.将物理仿真行为添加到仿真器中 D ...

  2. 纯js轮播图练习-1

    偶尔练习,看视频自己学着做个简单的纯JS轮播. 简单的纯js轮播图练习-1. 样子就是上面图片那样,先不管好不好看,主要是学会运用和理解轮播的原理 掌握核心的理论知识和技术的操作,其他的都可以在这个基 ...

  3. 微信小程序车牌号码模拟键盘输入

    微信小程序车牌号码模拟键盘输入练习, 未经允许,禁止转载,抄袭,如需借鉴参考等,请附上该文章连接. 相关资料参考:https://blog.csdn.net/littlerboss/article/d ...

  4. Python文本和字符串常用操作

    ## 字符串分割 line = "This is my love!" fields = line.split(' ') print(fields) # ['This', 'is', ...

  5. SHOPEX快递物流单号查询插件

    本SHOPEX快递物流单号跟踪插件提供国内外近2000家快递物流订单单号查询服务例如申通快递.顺丰快递.圆通快递.EMS快递.汇通快递.宅急送快递.德邦物流.百世快递.汇通快递.中通快递.天天快递等知 ...

  6. 从零开始一个http服务器(一)-开始

    从零开始一个http服务器 (一) 代码地址 : https://github.com/flamedancer/cserver git checkout step1 一个简单的socket serve ...

  7. java getter和setter的方法及内部类的调用

    class Test{ public static void main(String[]args){ Person person=new Person(); person.age=22; person ...

  8. print&input--Python

    1.print --> 打印到屏幕输出字符串 print("this is a dog!") -->代码 D:\Python\venv\Scripts\python.e ...

  9. SpaceVim 发布 v0.8.0

    This project exists thanks to all the people who have contributed. The last release v0.7.0 is target ...

  10. SQL 注入、XSS 攻击、CSRF 攻击

    SQL 注入.XSS 攻击.CSRF 攻击 SQL 注入 什么是 SQL 注入 SQL 注入,顾名思义就是通过注入 SQL 命令来进行攻击,更确切地说攻击者把 SQL 命令插入到 web 表单或请求参 ...