用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实现弹出菜单(弹出的菜单采用自定义布局)的更多相关文章

  1. android PopupWindow实现从底部弹出或滑出选择菜单或窗口

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

  2. Android 仿 新闻阅读器 菜单弹出效果(附源码DEMO)

    这一系列博文都是:(android高仿系列)今日头条 --新闻阅读器 (一) 开发中碰到问题之后实现的,觉得可能有的开发者用的到或则希望独立成一个小功能DEMO,所以就放出来这么一个DEMO. 原本觉 ...

  3. Android中PopupWindow中有输入框时无法弹出输入法的解决办法

    PopupWindow window=new PopupWindow(view, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); //必须让p ...

  4. 云笔记项目-笔记列表弹出"分享移动删除"子菜单

    业务需求: 笔记列表里还有一个按钮可以弹出子菜单,要求做到以下几点: (1)点击选中的笔记行的弹出按钮后,弹出子菜单,再次点击,子菜单收回. (2)选中其他笔记后,子菜单消失.效果如下图所示: 业务分 ...

  5. 小白jquery横向菜单弹出菜单制作

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

  6. 在Winform框架的多文档界面中实现双击子窗口单独弹出或拖出及拽回的处理

    在基于DevExpress的多文档窗口界面中,我们一般使用XtraTabbedMdiManager来管理多文档窗口的一些特性,如顶部菜单,页面的关闭按钮处理,以及一些特殊的设置,本篇随笔介绍这些特点, ...

  7. javascript的alert()的消息框不弹出或者弹出信息有误

    有时不知道什么,有时javascript的alert()的消息框不弹出或者弹出信息有误,代码是这么写的: //提示信息 public static void alert(TemplateControl ...

  8. 原生Js封装的弹出框-弹出窗口-页面居中-多状态可选

    原生Js封装的弹出框-弹出窗口-页面居中-多状态可选   实现了一下功能: 1.title可自定义 可拖拽 2.width height可以自定义 3.背景遮罩和透明度可以自定义 4.可以自己编辑弹出 ...

  9. 弹出视图/弹出模态presentViewController与presentModalViewController

    一.主要用途 弹出模态ViewController是IOS变成中很有用的一个技术,UIKit提供的一些专门用于模态显示的ViewController,如UIImagePickerController等 ...

随机推荐

  1. JS两种事件的触发方式

    一.入侵式触发方式 <input type="button" id="one" onclick="事件" /> 二.非入侵式触发 ...

  2. web程序快速开发

    关于web程序快速开发个人见解以及经历 由于在之前公司业务的发展,需要在基于核心业务的基础上开发其他较为独立的业务系统,所以就有了这个基于Dapper,DDD概念的基础框架,由于个人基于这个框架已经经 ...

  3. vue-element-table-js去重合并单元格解析【实战需求】

    有数据如下: { '2019-01-23': [ { 'channel': 'zp', 'listScanListNum': 24, 'listParseOkNum': 0, 'listPersonM ...

  4. 【LOJ】#2536. 「CQOI2018」解锁屏幕

    题解 什么破题,看一眼就能想出来\(n^2 2^n\)看了一眼数据范围有点虚,结果跑得飞快= = 处理出\(a[i][j]\)表示从\(i\)到\(j\)经过的点的点集 然后\(f[i][S]\)表示 ...

  5. 004 jquery过滤选择器-----------(基本过滤选择器)

    1.介绍 2.常见基本过滤器 3.程序 <!DOCTYPE html> <html> <head> <meta charset="UTF-8&quo ...

  6. JMS Java消息服务(Java Message Service)

    JMS 在一些场景下RPC的同步方式可能不太适合业务逻辑的处理,并且这种方式在某些场景下会导致业务的紧耦合. 基于异步交互模型的JMS解决了RPC产生的紧耦合问题,它提供了一个可以通过网络访问的抽象消 ...

  7. 循序渐进学.Net Core Web Api开发系列【8】:访问数据库(基本功能)

    系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 本篇讨论如 ...

  8. BZOJ3501 : PA2008 Cliquers Strike Back

    \[\begin{eqnarray*}ans&=&m^{\sum_{i=1}^n Stirling2(n,i)\bmod 999999598}\bmod 999999599\\& ...

  9. Codeforces Round #370 (Div. 2) C. Memory and De-Evolution 水题

    C. Memory and De-Evolution 题目连接: http://codeforces.com/contest/712/problem/C Description Memory is n ...

  10. GET POST方法长度限制(转)

    1.    Get方法长度限制 Http Get方法提交的数据大小长度并没有限制,HTTP协议规范没有对URL长度进行限制.这个限制是特定的浏览器及服务器对它的限制. 如:IE对URL长度的限制是20 ...