android PopupWindow实现从底部弹出或滑出选择菜单或窗口
本实例弹出窗口主要是继承PopupWindow类来实现的弹出窗体,布局可以根据自己定义设计。弹出效果主要使用了translate和alpha样式实现,具体实习如下:
第一步:设计弹出窗口xml:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center_horizontal"
- android:orientation="vertical"
- >
- <LinearLayout
- android:id="@+id/pop_layout"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center_horizontal"
- android:orientation="vertical"
- android:layout_alignParentBottom="true"
- android:background="@drawable/btn_style_alert_dialog_background"
- >
- <Button
- android:id="@+id/btn_take_photo"
- android:layout_marginLeft="20dip"
- android:layout_marginRight="20dip"
- android:layout_marginTop="20dip"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="拍照"
- android:background="@drawable/btn_style_alert_dialog_button"
- android:textStyle="bold"
- />
- <Button
- android:id="@+id/btn_pick_photo"
- android:layout_marginLeft="20dip"
- android:layout_marginRight="20dip"
- android:layout_marginTop="5dip"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="从相册选择"
- android:background="@drawable/btn_style_alert_dialog_button"
- android:textStyle="bold"
- />
- <Button
- android:id="@+id/btn_cancel"
- android:layout_marginLeft="20dip"
- android:layout_marginRight="20dip"
- android:layout_marginTop="15dip"
- android:layout_marginBottom="15dip"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="取消"
- android:background="@drawable/btn_style_alert_dialog_cancel"
- android:textColor="#ffffff"
- android:textStyle="bold"
- />
- </LinearLayout>
- </RelativeLayout>
第二步:创建SelectPicPopupWindow类继承PopupWindow:
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.drawable.ColorDrawable;
- import android.view.LayoutInflater;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.View.OnTouchListener;
- import android.view.ViewGroup.LayoutParams;
- import android.widget.Button;
- import android.widget.PopupWindow;
- public class SelectPicPopupWindow extends PopupWindow {
- private Button btn_take_photo, btn_pick_photo, btn_cancel;
- private View mMenuView;
- public SelectPicPopupWindow(Activity context,OnClickListener itemsOnClick) {
- super(context);
- LayoutInflater inflater = (LayoutInflater) context
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- mMenuView = inflater.inflate(R.layout.alert_dialog, null);
- btn_take_photo = (Button) mMenuView.findViewById(R.id.btn_take_photo);
- btn_pick_photo = (Button) mMenuView.findViewById(R.id.btn_pick_photo);
- btn_cancel = (Button) mMenuView.findViewById(R.id.btn_cancel);
- //取消按钮
- btn_cancel.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- //销毁弹出框
- dismiss();
- }
- });
- //设置按钮监听
- btn_pick_photo.setOnClickListener(itemsOnClick);
- btn_take_photo.setOnClickListener(itemsOnClick);
- //设置SelectPicPopupWindow的View
- this.setContentView(mMenuView);
- //设置SelectPicPopupWindow弹出窗体的宽
- this.setWidth(LayoutParams.FILL_PARENT);
- //设置SelectPicPopupWindow弹出窗体的高
- this.setHeight(LayoutParams.WRAP_CONTENT);
- //设置SelectPicPopupWindow弹出窗体可点击
- this.setFocusable(true);
- //设置SelectPicPopupWindow弹出窗体动画效果
- this.setAnimationStyle(R.style.AnimBottom);
- //实例化一个ColorDrawable颜色为半透明
- ColorDrawable dw = new ColorDrawable(0xb0000000);
- //设置SelectPicPopupWindow弹出窗体的背景
- this.setBackgroundDrawable(dw);
- //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
- mMenuView.setOnTouchListener(new OnTouchListener() {
- public boolean onTouch(View v, MotionEvent event) {
- int height = mMenuView.findViewById(R.id.pop_layout).getTop();
- int y=(int) event.getY();
- if(event.getAction()==MotionEvent.ACTION_UP){
- if(y<height){
- dismiss();
- }
- }
- return true;
- }
- });
- }
- }
第三步:编写MainActivity类实现测试:
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.TextView;
- public class MainActivity extends Activity {
- //自定义的弹出框类
- SelectPicPopupWindow menuWindow;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- TextView tv = (TextView) this.findViewById(R.id.text);
- //把文字控件添加监听,点击弹出自定义窗口
- tv.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- //实例化SelectPicPopupWindow
- menuWindow = new SelectPicPopupWindow(MainActivity.this, itemsOnClick);
- //显示窗口
- menuWindow.showAtLocation(MainActivity.this.findViewById(R.id.main), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置
- }
- });
- }
- //为弹出窗口实现监听类
- private OnClickListener itemsOnClick = new OnClickListener(){
- public void onClick(View v) {
- menuWindow.dismiss();
- switch (v.getId()) {
- case R.id.btn_take_photo:
- break;
- case R.id.btn_pick_photo:
- break;
- default:
- break;
- }
- }
- };
- }
第四:运行效果如下:

- PicPopupWindow.zip (663.4 KB)
- 描述: 源码
- 下载次数: 4599
android PopupWindow实现从底部弹出或滑出选择菜单或窗口的更多相关文章
- Android Demo---实现从底部弹出窗口
在前面的博文中,小编简单的介绍了如何制作圆角的按钮以及圆角的图片,伴着键盘和手指之间的舞步,迎来新的问题,不知道小伙伴有没有这样的经历,以App为例,点击头像的时候,会从底部弹出一个窗口,有从相册中选 ...
- Android PopupWindow怎么合理控制弹出位置(showAtLocation)
说到PopupWindow,应该都会有种熟悉的感觉,使用起来也很简单 // 一个自定义的布局,作为显示的内容 Context context = null; // 真实环境中要赋值 int layou ...
- 转 android 从底部弹出一个popuwindow,渐入渐出效果。我这里是用在购物车需要选择购买选项的操作。
最近要改客户端,需要实现一个从底部弹出的popuwindow,像我这种渣渣android技术,能整出popuwindow但是整不出动画,百度之,记录一下. 从下面这个地址转的 http://blog. ...
- 仿iOS底部弹出popUpWindow
上面为弹出来的效果 popUpWindow布局: <?xml version="1.0" encoding="utf-8"?> <Linear ...
- 【Android UI设计与开发】7.底部菜单栏(四)PopupWindow 实现显示仿腾讯新闻底部弹出菜单
前一篇文章中有用到 PopupWindow 来实现弹窗的功能.简单介绍以下吧. 官方文档是这样解释的:这就是一个弹出窗口,可以用来显示一个任意视图.出现的弹出窗口是一个浮动容器的当前活动. 1.首先来 ...
- 通用的popupwindow底部弹出框
前段时间做项目的时候,有几个底部弹出框,当时因为忙着赶进度所有就单独写了好几个popupwindow.后来就想着怎么实现一个通用的PopupWindow工具类 就是在要用到的时候创建该工具类的对象,并 ...
- Android 底部弹出Dialog(横向满屏)
项目中经常需要底部弹出框,这里我整理一下其中我用的比较顺手的一个方式(底部弹出一个横向满屏的dialog). 效果图如下所示(只显示关键部分): 步骤如下所示: 1.定义一个dialog的布局(lay ...
- Android开发实战之底部Dialog弹出效果
在Android开发中,有很多情况下我们需要使用到对话框,遗憾的是,安卓自带的对话框样式不能满足我们实际的需要,所以往往需要我们自定义对话框,具体做法:写一个对话框继承自Dialog实现他的一个构造方 ...
- Android BottomSheet:底部弹出Fragment面板(4)
Android BottomSheet:底部弹出Fragment面板(4) BottomSheet不仅可以弹出轻量级的定制好的面板(见附录文章5,6,7),还可以弹出"重"的 ...
随机推荐
- linux ext2 文件系统学习
Linux ext2文件系统理解 硬盘组成: 硬盘由多个圆形硬盘片组成.按照硬盘片能够容纳的数据量分为单盘和多盘.硬盘的数据读取主要靠机械手臂上的磁头,在机械手臂上有多个磁头.机械手臂不动硬盘旋转一 ...
- Editplus从下载到使用
☆ 准备工作 1,保证浏览器正常上网 2,能下载软件或已经下载到Editplus这个工具. ☆ 下载editplus 在浏览器输入http://www.editplus.com,然后回车.进入edit ...
- 浏览器User-agent String里的历史故事
你是否好奇标识浏览器身份的User-Agent,为什么每个浏览器都有Mozilla字样? Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 ...
- jquery click点击事件重复执行多次
$("button[name^='privateBtn']").click(function(){ alert('demo'); //接触绑定.避免重复执行 $("but ...
- Amoeba基本配置
Amoeba安装及读写分离配置一.amoeba简介官网:http://docs.hexnova.com/amoeba/index.html二.Centos下安装jdk1.yum 安装1.6版本jdk2 ...
- mysql备份恢复
备份命令: mysqldump -u root -p --opt 数据库名 > /data/数据库文件名.sql 恢复命令: mysql -u root -p 数据库名</data/恢复的 ...
- attr-img-src
https://dev.w3.org/html5/spec-preview/the-img-element.html#attr-img-src The src attribute must be pr ...
- 【转】【DP_树形DP专辑】【9月9最新更新】【from zeroclock's blog】
树,一种十分优美的数据结构,因为它本身就具有的递归性,所以它和子树见能相互传递很多信息,还因为它作为被限制的图在上面可进行的操作更多,所以各种用于不同地方的树都出现了,二叉树.三叉树.静态搜索树.AV ...
- P1443 马的遍历
同样是一个bfs水题... #include <bits/stdc++.h> using namespace std; typedef pair<int, int> St; S ...
- java.util.concurrent.CopyOnWriteArrayList
import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; impo ...