用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. chunk writer 中需要对抛错的交易进行回滚,同时又要在其他表中记录是哪一笔交易记录失败

    首先根据我有限的知识判断,回滚之后进行写表,该写表动作只能使用listener来进行. 考虑使用的listener有:ItemWriteListener     StepExecutionListen ...

  2. Javascript之继承(原型链方式)

    1.原型链 原型链是JavaScript中继承的主要方法. 每个构造函数都拥有一个原型对象,原型对象都包含一个指向构造函数的指针(constructor),实例都包含一个指向原型对象的内部指针(__p ...

  3. android studio 3.0之后版本自定义文件名生成apk文件

    修改app模块的build.gradle 在android闭包中添加以下代码 //指定打包后应用名称 android.applicationVariants.all { variant -> v ...

  4. vue1.0

    vue1.0学习总结   前言 使用vue已经有三.四个月了,但是只是学着使用了一些基本方法.因为现在的前端框架越来越多(Angular,React...),但是我相信万变不离其宗,很多用法框架之间还 ...

  5. hadoop2.6.4的HA集群搭建超详细步骤

    hadoop2.0已经发布了稳定版本了,增加了很多特性,比如HDFS HA.YARN等.最新的hadoop-2.6.4又增加了YARN HA 注意:apache提供的hadoop-2.6.4的安装包是 ...

  6. 自然语言处理---用隐马尔科夫模型(HMM)实现词性标注---1998年1月份人民日报语料---learn---test---evaluation---Demo---java实现

    先放上一张Demo的测试图 测试的句子及每个分词的词性标注为:   目前/t 这/rzv 条/q 高速公路/n 之间/f 的/ude1 路段/n 已/d 紧急/a 封闭/v ./w 需要基础知识 HM ...

  7. SVN同步时报错:“Previous operation has not finished; run 'cleanup' if it was interrupted”

    SVN同步时报错:“Previous operation has not finished; run 'cleanup' if it was interrupted” 这大概是SVN之前的操作没有完成 ...

  8. Spring重复扫描导致事务失败的解决方案及深入分析

    问题及日志使用Spring和mybatis,然后配置事务,出现SqlSession was not registered for synchronization because synchroniza ...

  9. Kali Linux 2017.3发布了

     Kali Linux 2017.3发布了 Kali Linux官方在11月21日发布Kali Linux 2017的第三个版本2017.3.这次发布变化相对不大,主要是设置面板风格发生改变,增加少量 ...

  10. 二叉查找树(二叉排序树)的详细实现,以及随机平衡二叉查找树Treap的分析与应用

    这是一篇两年前写的东西,自我感觉还是相当不错的Treap教程.正好期末信息科学技术概论课要求交一个论文,就把这个东西修改了一下交了,顺便也发到这里吧. 随机平衡二叉查找树Treap的分析与应用 1.序 ...