PopupWindow是一个可以用来显示一个任意的视图的弹出窗口,他需要完全依赖layout布局。

它没什么界面,在弹出的窗口中完全显示布局中的控件。

上面两个美女头就是弹窗PopupWindow显示的内容。是两个Button。

具体实现:

注意:那三个Button不能和普通的Button一样通过findViewById()方法获得,必须首先说的Button所在的视图,View popview = layoutInflater.inflate(R.layout.poplayout, null);

我的Button在poplayout.xml中。最后通过button1 = (Button) popview.findViewById(R.id.button1)获得。

另外一点就是:不要在oncreate()中获得Button,而是像我一样在onclick方法下获得,和popupwindow一起。这一点不一定正确。

为什么要提呢?因为我在oncreate()中获得Button时,button的点击事件不能用,我也不是很清楚。那位大牛要是知道的话,可以告诉我一下。

showAtLocation(findViewById(R.id.edit_layout), Gravity.BOTTOM,0, 0);

设置弹窗的位置:

第一个参数是弹窗父控件的布局;

第二个参数是位置如左,右,上部,下部等;

第三个参数是X方向的偏移量;

第四个参数是Y方向的偏移量。

  1. package xiaosi.popwindow;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.Gravity;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.view.WindowManager.LayoutParams;
  9. import android.widget.Button;
  10. import android.widget.PopupWindow;
  11. import android.widget.Toast;
  12. public class PopwindowActivity extends Activity implements OnClickListener
  13. {
  14. /** Called when the activity is first created. */
  15. private Button button = null;
  16. private Button button1 = null;
  17. private Button button2 = null;
  18. private Button button3 = null;
  19. @Override
  20. public void onCreate(Bundle savedInstanceState)
  21. {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.main);
  24. button = (Button) findViewById(R.id.button);
  25. button.setOnClickListener(this);
  26. }
  27. public void onClick(View arg0)
  28. {
  29. if (arg0.getId() == R.id.button)
  30. {
  31. LayoutInflater layoutInflater = (LayoutInflater) (PopwindowActivity.this)
  32. .getSystemService(LAYOUT_INFLATER_SERVICE);
  33. // 获取自定义布局文件poplayout.xml的视图
  34. View popview = layoutInflater.inflate(R.layout.poplayout, null);
  35. PopupWindow popWindow = new PopupWindow(popview,
  36. LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  37. //规定弹窗的位置
  38. popWindow.showAtLocation(findViewById(R.id.main), Gravity.BOTTOM,
  39. 0, 0);
  40. //PopupWindow里的两个Button
  41. button1 = (Button) popview.findViewById(R.id.button1);
  42. button1.setOnClickListener(this);
  43. button2 = (Button) popview.findViewById(R.id.button2);
  44. button2.setOnClickListener(this);
  45. }
  46. else if (arg0.getId() == R.id.button1)
  47. {
  48. Toast.makeText(PopwindowActivity.this, "button1", Toast.LENGTH_LONG)
  49. .show();
  50. }
  51. else if (arg0.getId() == R.id.button2)
  52. {
  53. Toast.makeText(PopwindowActivity.this, "button2", Toast.LENGTH_LONG)
  54. .show();
  55. }
  56. }
  57. }

poplayout.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:gravity="center_horizontal"
  6. android:orientation="horizontal" >
  7. <Button
  8. android:id="@+id/button1"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:background="@drawable/colorframe_1" />
  12. <Button
  13. android:id="@+id/button2"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:background="@drawable/colorframe_3" />
  17. </LinearLayout>

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/main"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:orientation="vertical"
  7. android:background="@drawable/background">
  8. <Button
  9. android:id="@+id/button"
  10. android:layout_width="fill_parent"
  11. android:layout_height="wrap_content"
  12. android:text="点击查看效果" />
  13. <ImageView
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. android:src="@drawable/a" />
  17. </LinearLayout>

以上只是用来学习之用,拿出来和大家一起分享一下。

有想要源代码的给我留言。

http://blog.csdn.net/sjf0115/article/details/7339914

Android进阶2之PopupWindow弹窗(有点悬浮窗的感觉)的更多相关文章

  1. 【Android - 进阶】之PopupWindow的使用

    创建一个类继承自PopupWindow,编写自定义的PopupWindow类.示例代码如下: import android.app.Activity; import android.graphics. ...

  2. JavaScrip——练习(做悬浮框进一步:悬浮窗后缀悬浮窗【感觉这种方法比较麻烦】)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. Android开发 PopupWindow弹窗调用第三方地图(百度,高德)实现导航功能

    博客描述:后台返回地点的经纬度在地图上进行描点,点击导航弹出PopupWindow进行选择地图操作,如果手机中没有安装地图,提示没有,否则传值调起地图进行导航操作 看一下实现的效果,没图说再多都白搭 ...

  4. Android悬浮窗实现 使用WindowManager

    Android悬浮窗实现 使用WindowManager WindowManager介绍 通过Context.getSystemService(Context.WINDOW_SERVICE)可以获得  ...

  5. 我的Android进阶之旅------>经典的大牛博客推荐(排名不分先后)!!

    本文来自:http://blog.csdn.net/ouyang_peng/article/details/11358405 今天看到一篇文章,收藏了很多大牛的博客,在这里分享一下 谦虚的天下 柳志超 ...

  6. Android 使用WindowManager实现Android悬浮窗

    WindowManager介绍 通过Context.getSystemService(Context.WINDOW_SERVICE)可以获得 WindowManager对象. 每一个WindowMan ...

  7. Android悬浮框,在Service中打开悬浮窗;在Service中打开Dialog;

    文章介绍了如何在Service中显示悬浮框,在Service中弹出Dialog,在Service中做耗时的轮询操作: 背景需求: 公司的项目现在的逻辑是这样的:发送一个指令,然后3秒一次轮询去查询这个 ...

  8. Android无需权限显示悬浮窗, 兼谈逆向分析app

    前言 最近UC浏览器中文版出了一个快速搜索的功能, 在使用其他app的时候, 如果复制了一些内容, 屏幕顶部会弹一个窗口, 提示一些操作, 点击后跳转到UC, 显示这个悬浮窗不需要申请android. ...

  9. (转载)Android UI设计之AlertDialog弹窗控件

    Android UI设计之AlertDialog弹窗控件 作者:qq_27630169 字体:[增加 减小] 类型:转载 时间:2016-08-18我要评论 这篇文章主要为大家详细介绍了Android ...

随机推荐

  1. HDU 5514 Frogs (容斥原理)

    题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=5514 题意 : 有m个石子围成一圈, 有n只青蛙从跳石子, 都从0号石子开始, 每只能越过a[i] ...

  2. Android Develop 之 Ddevelop WorkFlow Basics

    Android应用程序的开发流程一言以蔽之,可以用一图五步概括.一图如下. 第一步:安装.安装开发环境,包括JDK,Android Studio,SDK Manager(通常下载Android Stu ...

  3. tungsten

    ./tools/tungsten-installer --master-slave -a --datasource-type=mysql --master-host=master.puppet.org ...

  4. python用parammiko模块实现linux的远程操作

    parammiko  可以实现远程的带密码登录,解决ssh远程登陆需要交互的问题 (当然很多其他的,如tcl也可以).但这个用python做比较简单 1.parammiko 的安装 1.1.依赖模块 ...

  5. fedora19配置 SSH 免密码登陆

    a.ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa b.cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys   ...

  6. CAD INSTALL PROBLEMS

    Windows8操作系统,没有重装系统前安装Autodesk软件是完全没有问题的 把电脑还原系统后再安装Autodesk软件提示缺少LiteHtml.dll文件了 兼容性改为XP3,然后点管理员身份运 ...

  7. python Eve RESTFul 尝试笔记

    0.前言 最近重点研究了yeelink平台的原理和使用,yeelink平台和多数云平台设计一样应用了RESTFul框架.嵌入式侧(或者是客服端侧)的相关技术研究的比较充分(个人这么认为),是不是该弄弄 ...

  8. 写自己的WPF样式 - 按钮

    做一个后台管理小程序,据说WPF的界面比较"炫",于是选择使用WPF来开发.既然用了WPF当然需要做好看点了,于是稍微研究了下WPF的样式,废话不多说下面开始自定义一个按钮样式: ...

  9. ListView的优化

    1. ListView须要设置adapter,它的item是通过adapter的方法getView(int position, View convertView, ViewGroup parent)获 ...

  10. unity3d触屏操作对象运动

    using UnityEngine; using System.Collections; public class robot : MonoBehaviour { private GameObject ...