用PopupWindow实现弹出菜单(弹出的菜单采用自定义布局)

用PopupWindow实现弹出菜单是一个比较好的方式。当然我们还有一个类PopupMenu也能实现弹出菜单,但那个太过于局限了,所以不是很推荐。
这个实例的效果是这样的:点击按钮后,一个菜单从屏幕的右边滑入到屏幕中,点击按钮/空白处后菜单消失。
布局文件时一个按钮,我就不贴出代码了。下面是菜单的布局:
<?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="match_parent"
android:orientation="horizontal" > <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" /> <Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3" /> <Button
android:id="@+id/closet_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="关闭" /> </LinearLayout>
MainActivity.java
package com.kale.popup; import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener{
LayoutInflater inflater = null;
private PopupWindow popupWindow; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
initPopWindow();
} /**
* 初始化popWindow
* */
private void initPopWindow() {
View popView = inflater.inflate(R.layout.menu, null);
popupWindow = new PopupWindow(popView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new ColorDrawable(0));
//设置popwindow出现和消失动画
popupWindow.setAnimationStyle(R.style.PopMenuAnimation);
Button btn01 = (Button)popView.findViewById(R.id.button1);
btn01.setOnClickListener(this);
Button btn02 = (Button)popView.findViewById(R.id.button2);
btn02.setOnClickListener(this);
Button btn03 = (Button)popView.findViewById(R.id.button3);
btn03.setOnClickListener(this);
Button closetBtn = (Button)popView.findViewById(R.id.closet_btn);
closetBtn.setOnClickListener(this); } public void buttonListener(View v) {
showPop(v, 0, 0, 0);
} /**
* 显示popWindow
* */
public void showPop(View parent, int x, int y,int postion) {
//设置popwindow显示位置
popupWindow.showAsDropDown(parent);
//获取popwindow焦点
popupWindow.setFocusable(true);
//设置popwindow如果点击外面区域,便关闭。
popupWindow.setOutsideTouchable(true);
popupWindow.update(); } @Override
public void onClick(View v) {
Button btn = (Button) v;
Toast.makeText(MainActivity.this, btn.getText(), 0).show();
popupWindow.dismiss();
} }
菜单的动画
style.xml
<style name="PopMenuAnimation" parent="@android:style/Animation">
<item name="android:windowEnterAnimation">@anim/slide_left_in</item>
<item name="android:windowExitAnimation">@anim/slide_right_out</item>
</style>
slide_left_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" > <translate
android:duration="200"
android:fromXDelta="100.0%p"
android:toXDelta="0.0" /> </set>
slide_right_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" > <translate
android:duration="100"
android:fromXDelta="0.0"
android:toXDelta="100.0%p" /> </set>
用PopupWindow实现弹出菜单(弹出的菜单采用自定义布局)的更多相关文章
- android PopupWindow实现从底部弹出或滑出选择菜单或窗口
本实例弹出窗口主要是继承PopupWindow类来实现的弹出窗体,布局可以根据自己定义设计.弹出效果主要使用了translate和alpha样式实现,具体实习如下: 第一步:设计弹出窗口xml: &l ...
- Android 仿 新闻阅读器 菜单弹出效果(附源码DEMO)
这一系列博文都是:(android高仿系列)今日头条 --新闻阅读器 (一) 开发中碰到问题之后实现的,觉得可能有的开发者用的到或则希望独立成一个小功能DEMO,所以就放出来这么一个DEMO. 原本觉 ...
- Android中PopupWindow中有输入框时无法弹出输入法的解决办法
PopupWindow window=new PopupWindow(view, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); //必须让p ...
- 云笔记项目-笔记列表弹出"分享移动删除"子菜单
业务需求: 笔记列表里还有一个按钮可以弹出子菜单,要求做到以下几点: (1)点击选中的笔记行的弹出按钮后,弹出子菜单,再次点击,子菜单收回. (2)选中其他笔记后,子菜单消失.效果如下图所示: 业务分 ...
- 小白jquery横向菜单弹出菜单制作
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 在Winform框架的多文档界面中实现双击子窗口单独弹出或拖出及拽回的处理
在基于DevExpress的多文档窗口界面中,我们一般使用XtraTabbedMdiManager来管理多文档窗口的一些特性,如顶部菜单,页面的关闭按钮处理,以及一些特殊的设置,本篇随笔介绍这些特点, ...
- javascript的alert()的消息框不弹出或者弹出信息有误
有时不知道什么,有时javascript的alert()的消息框不弹出或者弹出信息有误,代码是这么写的: //提示信息 public static void alert(TemplateControl ...
- 原生Js封装的弹出框-弹出窗口-页面居中-多状态可选
原生Js封装的弹出框-弹出窗口-页面居中-多状态可选 实现了一下功能: 1.title可自定义 可拖拽 2.width height可以自定义 3.背景遮罩和透明度可以自定义 4.可以自己编辑弹出 ...
- 弹出视图/弹出模态presentViewController与presentModalViewController
一.主要用途 弹出模态ViewController是IOS变成中很有用的一个技术,UIKit提供的一些专门用于模态显示的ViewController,如UIImagePickerController等 ...
随机推荐
- #JS 前端javascript规范文档
一.规范目的 为提高团队协作效率,便于前端后期优化维护,输出高质量的文档. 二.基本准则 符合web标准,结构表现行为分离,兼容性优良.页面性能方面,代码要求简洁明了有序, 尽可能的减小服务器负载,保 ...
- 【LOJ】#2496. 「AHOI / HNOI2018」毒瘤
题面 还有这么诚实的出题人! 我们最多影响20个点,然后把这20个点的虚树建出来,并且枚举每个点的选举状态,如果一个点选或不选可以通过改\(dp[u][0] = 0\)或\(dp[u][1] = 0\ ...
- 记在VMware虚拟机中对网站进行性能压力测试的经历
由于本次测试,仅仅是对静态网站首页进行的测试,所以没有涉及到MySQL数据库的性能监测 服务器基本配置 webbench测试工具 Linux上一款优秀的web性能压力测试工具.webbench最多可以 ...
- MIT-6.828-JOS-lab2:Memory management
MIT-6.828 Lab 2: Memory Management实验报告 tags:mit-6.828 os 概述 本文主要介绍lab2,讲的是操作系统内存管理,从内容上分为三部分: 第一部分讲的 ...
- BZOJ.3522.[POI2014]Hotel(DP)
题目链接 BZOJ 洛谷 以为裸点分治,但数据范围怎么这么小?快打完了发现不对.. n^2做的话其实是个水题.. 枚举每一个点为根,为了不重复计算,我们要求所求的三个点必须分别位于三棵子树上. 考虑当 ...
- hdu CA Loves GCD(dp)
一道我想骂人的题,差点把我气炸了. 题意: 求一个数的集合中(非多重集,每个数只出现一次)所有子集的gcd的和.结果MOD10^8+7输出. 输入输出不说了,自己看吧,不想写了. 当时我真把它当作数论 ...
- C语言结构体及typedef关键字定义结构体别名和函数指针的应用
结构体(struct)的初始化 struct autonlist { char *symbol; struct nlist nl[2]; struct autonlist *left, *right; ...
- Xtreme8.0 - Back to Square 1 数学
Back to Square 1 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/back-to- ...
- 设置eclipse不同的workspace共享配置
有很多的项目,每个项目使用一个workspace,结果每新建一个workspace重新配置一下,但是配置的东西都是一样的, 总结一下,复制工作空间配置步骤如下: 1 使用eclipse新建worksp ...
- 使用CefSharp在.Net程序中嵌入Chrome浏览器(四)——启动优化
在实际使用过程中,发现有的客户端会出现chrome加载网页过慢问题,定位后发现很多是因为设置系统代理所致,此时可以通过如下启动参数禁止系统代理. {"proxy-auto-detect&qu ...