本实例弹出窗口主要是继承PopupWindow类来实现的弹出窗体,布局可以根据自己定义设计。弹出效果主要使用了translate和alpha样式实现,具体实习如下:

第一步:设计弹出窗口xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="fill_parent"
  5. android:layout_height="wrap_content"
  6. android:gravity="center_horizontal"
  7. android:orientation="vertical"
  8. >
  9. <LinearLayout
  10. android:id="@+id/pop_layout"
  11. android:layout_width="fill_parent"
  12. android:layout_height="wrap_content"
  13. android:gravity="center_horizontal"
  14. android:orientation="vertical"
  15. android:layout_alignParentBottom="true"
  16. android:background="@drawable/btn_style_alert_dialog_background"
  17. >
  18. <Button
  19. android:id="@+id/btn_take_photo"
  20. android:layout_marginLeft="20dip"
  21. android:layout_marginRight="20dip"
  22. android:layout_marginTop="20dip"
  23. android:layout_width="fill_parent"
  24. android:layout_height="wrap_content"
  25. android:text="拍照"
  26. android:background="@drawable/btn_style_alert_dialog_button"
  27. android:textStyle="bold"
  28. />
  29. <Button
  30. android:id="@+id/btn_pick_photo"
  31. android:layout_marginLeft="20dip"
  32. android:layout_marginRight="20dip"
  33. android:layout_marginTop="5dip"
  34. android:layout_width="fill_parent"
  35. android:layout_height="wrap_content"
  36. android:text="从相册选择"
  37. android:background="@drawable/btn_style_alert_dialog_button"
  38. android:textStyle="bold"
  39. />
  40. <Button
  41. android:id="@+id/btn_cancel"
  42. android:layout_marginLeft="20dip"
  43. android:layout_marginRight="20dip"
  44. android:layout_marginTop="15dip"
  45. android:layout_marginBottom="15dip"
  46. android:layout_width="fill_parent"
  47. android:layout_height="wrap_content"
  48. android:text="取消"
  49. android:background="@drawable/btn_style_alert_dialog_cancel"
  50. android:textColor="#ffffff"
  51. android:textStyle="bold"
  52. />
  53. </LinearLayout>
  54. </RelativeLayout>

第二步:创建SelectPicPopupWindow类继承PopupWindow:

  1. import android.app.Activity;
  2. import android.content.Context;
  3. import android.graphics.drawable.ColorDrawable;
  4. import android.view.LayoutInflater;
  5. import android.view.MotionEvent;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.view.View.OnTouchListener;
  9. import android.view.ViewGroup.LayoutParams;
  10. import android.widget.Button;
  11. import android.widget.PopupWindow;
  12. public class SelectPicPopupWindow extends PopupWindow {
  13. private Button btn_take_photo, btn_pick_photo, btn_cancel;
  14. private View mMenuView;
  15. public SelectPicPopupWindow(Activity context,OnClickListener itemsOnClick) {
  16. super(context);
  17. LayoutInflater inflater = (LayoutInflater) context
  18. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  19. mMenuView = inflater.inflate(R.layout.alert_dialog, null);
  20. btn_take_photo = (Button) mMenuView.findViewById(R.id.btn_take_photo);
  21. btn_pick_photo = (Button) mMenuView.findViewById(R.id.btn_pick_photo);
  22. btn_cancel = (Button) mMenuView.findViewById(R.id.btn_cancel);
  23. //取消按钮
  24. btn_cancel.setOnClickListener(new OnClickListener() {
  25. public void onClick(View v) {
  26. //销毁弹出框
  27. dismiss();
  28. }
  29. });
  30. //设置按钮监听
  31. btn_pick_photo.setOnClickListener(itemsOnClick);
  32. btn_take_photo.setOnClickListener(itemsOnClick);
  33. //设置SelectPicPopupWindow的View
  34. this.setContentView(mMenuView);
  35. //设置SelectPicPopupWindow弹出窗体的宽
  36. this.setWidth(LayoutParams.FILL_PARENT);
  37. //设置SelectPicPopupWindow弹出窗体的高
  38. this.setHeight(LayoutParams.WRAP_CONTENT);
  39. //设置SelectPicPopupWindow弹出窗体可点击
  40. this.setFocusable(true);
  41. //设置SelectPicPopupWindow弹出窗体动画效果
  42. this.setAnimationStyle(R.style.AnimBottom);
  43. //实例化一个ColorDrawable颜色为半透明
  44. ColorDrawable dw = new ColorDrawable(0xb0000000);
  45. //设置SelectPicPopupWindow弹出窗体的背景
  46. this.setBackgroundDrawable(dw);
  47. //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
  48. mMenuView.setOnTouchListener(new OnTouchListener() {
  49. public boolean onTouch(View v, MotionEvent event) {
  50. int height = mMenuView.findViewById(R.id.pop_layout).getTop();
  51. int y=(int) event.getY();
  52. if(event.getAction()==MotionEvent.ACTION_UP){
  53. if(y<height){
  54. dismiss();
  55. }
  56. }
  57. return true;
  58. }
  59. });
  60. }
  61. }

第三步:编写MainActivity类实现测试:

  1. import android.app.Activity;
  2. import android.os.Bundle;
  3. import android.view.Gravity;
  4. import android.view.View;
  5. import android.view.View.OnClickListener;
  6. import android.widget.TextView;
  7. public class MainActivity extends Activity {
  8. //自定义的弹出框类
  9. SelectPicPopupWindow menuWindow;
  10. @Override
  11. public void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. TextView tv = (TextView) this.findViewById(R.id.text);
  15. //把文字控件添加监听,点击弹出自定义窗口
  16. tv.setOnClickListener(new OnClickListener() {
  17. public void onClick(View v) {
  18. //实例化SelectPicPopupWindow
  19. menuWindow = new SelectPicPopupWindow(MainActivity.this, itemsOnClick);
  20. //显示窗口
  21. menuWindow.showAtLocation(MainActivity.this.findViewById(R.id.main), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置
  22. }
  23. });
  24. }
  25. //为弹出窗口实现监听类
  26. private OnClickListener  itemsOnClick = new OnClickListener(){
  27. public void onClick(View v) {
  28. menuWindow.dismiss();
  29. switch (v.getId()) {
  30. case R.id.btn_take_photo:
  31. break;
  32. case R.id.btn_pick_photo:
  33. break;
  34. default:
  35. break;
  36. }
  37. }
  38. };
  39. }

第四:运行效果如下:

参考:http://104zz.iteye.com/blog/1685389

android PopupWindow实现从底部弹出或滑出选择菜单或窗口的更多相关文章

  1. Android Demo---实现从底部弹出窗口

    在前面的博文中,小编简单的介绍了如何制作圆角的按钮以及圆角的图片,伴着键盘和手指之间的舞步,迎来新的问题,不知道小伙伴有没有这样的经历,以App为例,点击头像的时候,会从底部弹出一个窗口,有从相册中选 ...

  2. Android PopupWindow怎么合理控制弹出位置(showAtLocation)

    说到PopupWindow,应该都会有种熟悉的感觉,使用起来也很简单 // 一个自定义的布局,作为显示的内容 Context context = null; // 真实环境中要赋值 int layou ...

  3. 转 android 从底部弹出一个popuwindow,渐入渐出效果。我这里是用在购物车需要选择购买选项的操作。

    最近要改客户端,需要实现一个从底部弹出的popuwindow,像我这种渣渣android技术,能整出popuwindow但是整不出动画,百度之,记录一下. 从下面这个地址转的 http://blog. ...

  4. 仿iOS底部弹出popUpWindow

    上面为弹出来的效果 popUpWindow布局: <?xml version="1.0" encoding="utf-8"?> <Linear ...

  5. 【Android UI设计与开发】7.底部菜单栏(四)PopupWindow 实现显示仿腾讯新闻底部弹出菜单

    前一篇文章中有用到 PopupWindow 来实现弹窗的功能.简单介绍以下吧. 官方文档是这样解释的:这就是一个弹出窗口,可以用来显示一个任意视图.出现的弹出窗口是一个浮动容器的当前活动. 1.首先来 ...

  6. 通用的popupwindow底部弹出框

    前段时间做项目的时候,有几个底部弹出框,当时因为忙着赶进度所有就单独写了好几个popupwindow.后来就想着怎么实现一个通用的PopupWindow工具类 就是在要用到的时候创建该工具类的对象,并 ...

  7. Android 底部弹出Dialog(横向满屏)

    项目中经常需要底部弹出框,这里我整理一下其中我用的比较顺手的一个方式(底部弹出一个横向满屏的dialog). 效果图如下所示(只显示关键部分): 步骤如下所示: 1.定义一个dialog的布局(lay ...

  8. Android开发实战之底部Dialog弹出效果

    在Android开发中,有很多情况下我们需要使用到对话框,遗憾的是,安卓自带的对话框样式不能满足我们实际的需要,所以往往需要我们自定义对话框,具体做法:写一个对话框继承自Dialog实现他的一个构造方 ...

  9. Android BottomSheet:底部弹出Fragment面板(4)

     Android BottomSheet:底部弹出Fragment面板(4) BottomSheet不仅可以弹出轻量级的定制好的面板(见附录文章5,6,7),还可以弹出"重"的 ...

随机推荐

  1. linux ext2 文件系统学习

    Linux  ext2文件系统理解 硬盘组成: 硬盘由多个圆形硬盘片组成.按照硬盘片能够容纳的数据量分为单盘和多盘.硬盘的数据读取主要靠机械手臂上的磁头,在机械手臂上有多个磁头.机械手臂不动硬盘旋转一 ...

  2. Editplus从下载到使用

    ☆ 准备工作 1,保证浏览器正常上网 2,能下载软件或已经下载到Editplus这个工具. ☆ 下载editplus 在浏览器输入http://www.editplus.com,然后回车.进入edit ...

  3. 浏览器User-agent String里的历史故事

    你是否好奇标识浏览器身份的User-Agent,为什么每个浏览器都有Mozilla字样? Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 ...

  4. jquery click点击事件重复执行多次

    $("button[name^='privateBtn']").click(function(){ alert('demo'); //接触绑定.避免重复执行 $("but ...

  5. Amoeba基本配置

    Amoeba安装及读写分离配置一.amoeba简介官网:http://docs.hexnova.com/amoeba/index.html二.Centos下安装jdk1.yum 安装1.6版本jdk2 ...

  6. mysql备份恢复

    备份命令: mysqldump -u root -p --opt 数据库名 > /data/数据库文件名.sql 恢复命令: mysql -u root -p 数据库名</data/恢复的 ...

  7. attr-img-src

    https://dev.w3.org/html5/spec-preview/the-img-element.html#attr-img-src The src attribute must be pr ...

  8. 【转】【DP_树形DP专辑】【9月9最新更新】【from zeroclock's blog】

    树,一种十分优美的数据结构,因为它本身就具有的递归性,所以它和子树见能相互传递很多信息,还因为它作为被限制的图在上面可进行的操作更多,所以各种用于不同地方的树都出现了,二叉树.三叉树.静态搜索树.AV ...

  9. P1443 马的遍历

    同样是一个bfs水题... #include <bits/stdc++.h> using namespace std; typedef pair<int, int> St; S ...

  10. java.util.concurrent.CopyOnWriteArrayList

    import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; impo ...