第一步:自定义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. IO,File对象-构造函数和常用方法

    import java.io.File; import java.text.DateFormat; import java.util.Date; public class FileDemo { pub ...

  2. JAVA中判断年月日格式是否正确(支持判断闰年的2月份)

    一.先说一下年月日(yyyy-MM-dd)正则表达式: 1.年月日正则表达式:^((19|20)[0-9]{2})-((0?2-((0?[1-9])|([1-2][0-9])))|(0?(1|3|5| ...

  3. MySQL 参数slave_pending_jobs_size_max设置

    今天生产环境上从库出现SQL进程停止的异常,错误信息如下: Slave_IO_Running: Yes Slave_SQL_Running: No Replicate_Do_DB: Replicate ...

  4. 微信小程序数据分析之自定义分析

    在小程序后台,微信已经提供了强大的数据分析功能,包括实时统计.访问分析.来源分析和用户画像功能,可以说对一般的数据分析已经完全足够了,但有时应用需要做一些更加精准的数据分析,比如具体到某一个页面的分享 ...

  5. 公司内部搭建git服务器

    前言 因为gitHub上的项目是公开的,不适合公司内部项目放在上面,而私人的需要收费,这绝非是我们愿意的.所以找了个跟gitHub很相似,但是又免费的gitLab.现在将搭建gitLab过程记录一下留 ...

  6. 7、Linux应用程序地址布局

    程序构成 在学习Linux应用程序开发时,经常会遇到如下概念: 代码段.数据段.BSS段(Block Started by Symbol,又名:未始化数据段) .堆(heap)和栈(stack).始化 ...

  7. 手搓一个C语言简单计算器。

    #include <stdio.h> void xing(int shu); void biaoti(int kong,char * title); void zhuyemian(char ...

  8. java语言描述 用抽象类模拟咖啡机的工作

    import java.util.Scanner; class Test { public static void main(String[] args) { coffee per = new cof ...

  9. mybatis中的resultMap实际作用

    resultMap和resultType在实际的使用上完全可以进行替换,但是resultMap有比resultType更多的一个功能.我们先定义一个简单的resultMap例子 <resultM ...

  10. Solr第二讲——SolrJ客户端的使用与案例

    一.Solrj的使用 1.什么是Solrj solrj是访问Solr服务的java客户端(就像通过jedis操作redis一样),提供索引和搜索的请求方法,SolrJ通常在嵌入在业务系统中,通过Sol ...