方法一的Activity

package com.app.test02;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
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;
import android.widget.Toast; public class PopwindowLeft extends Activity {
// 声明PopupWindow对象的引用
private PopupWindow popupWindow; /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_popupwindow_main);
// 点击按钮弹出菜单
Button pop = (Button) findViewById(R.id.popBtn);
pop.setOnClickListener(popClick);
} // 点击弹出左侧菜单的显示方式
OnClickListener popClick = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
getPopupWindow();
// 这里是位置显示方式,在屏幕的左侧
popupWindow.showAtLocation(v, Gravity.LEFT, 0, 0);
}
}; /**
* 创建PopupWindow
*/
protected void initPopuptWindow() {
// TODO Auto-generated method stub
// 获取自定义布局文件activity_popupwindow_left.xml的视图
View popupWindow_view = getLayoutInflater().inflate(R.layout.activity_popupwindow_left, null,
false);
// 创建PopupWindow实例,200,LayoutParams.MATCH_PARENT分别是宽度和高度
popupWindow = new PopupWindow(popupWindow_view, 200, LayoutParams.MATCH_PARENT, true);
// 设置动画效果
popupWindow.setAnimationStyle(R.style.AnimationFade);
// 点击其他地方消失
popupWindow_view.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
popupWindow = null;
}
return false;
}
});
}
/***
* 获取PopupWindow实例
*/
private void getPopupWindow() {
if (null != popupWindow) {
popupWindow.dismiss();
return;
} else {
initPopuptWindow();
}
}
}

方法二的Activity

package com.app.test02;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
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.PopupWindow; public class PopwindowLeftNew extends Activity{
private PopupWindow popupWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_popupwindow_main); findViewById(R.id.popBtn).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 获取自定义布局文件activity_popupwindow_left.xml的视图
View popupWindow_view = getLayoutInflater().inflate(R.layout.activity_popupwindow_left, null,false);
// 创建PopupWindow实例,200,LayoutParams.MATCH_PARENT分别是宽度和高度
popupWindow = new PopupWindow(popupWindow_view, 200, LayoutParams.MATCH_PARENT, true);
// 设置动画效果
popupWindow.setAnimationStyle(R.style.AnimationFade);
// 这里是位置显示方式,在屏幕的左侧
popupWindow.showAtLocation(v, Gravity.LEFT, 0, 0);
// 点击其他地方消失
popupWindow_view.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
popupWindow = null;
}
return false;
}
});
}
}); }
}

效果图





附:一些相关的布局文件

PopupWindow弹出菜单

activity_popupwindow_main.xml

<?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="vertical"
android:background="#fff" > <Button android:id="@+id/popBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="弹出左侧菜单" /> </LinearLayout>

activity_popupwindow_left.xml

<?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:background="@android:color/darker_gray"
android:orientation="vertical"
android:gravity="center"
android:paddingTop="50dp"> <Button
android:id="@+id/open"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/darker_gray"
android:text="打开" /> <Button
android:id="@+id/save"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/darker_gray"
android:text="保存" /> <Button
android:id="@+id/close"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/darker_gray"
android:text="关闭" /> <Button
android:id="@+id/open"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/darker_gray"
android:text="打开" /> <Button
android:id="@+id/save"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/darker_gray"
android:text="保存" /> <Button
android:id="@+id/close"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/darker_gray"
android:text="关闭" /> <Button
android:id="@+id/open"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/darker_gray"
android:text="打开" /> <Button
android:id="@+id/save"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/darker_gray"
android:text="保存" /> <Button
android:id="@+id/close"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/darker_gray"
android:text="关闭" /> </LinearLayout>

弹出动画XML

在res文件夹下,建立anim文件夹。写入如下两个文件。
弹出动画
in_lefttoright.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 定义从左向右进入的动画 -->
<translate
android:duration="500"
android:fromXDelta="-100%"
android:toXDelta="0" /> </set>
弹回动画
out_righttoleft.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 定义从右向左动画退出动画 -->
<translate
android:duration="500"
android:fromXDelta="0"
android:toXDelta="-100%" /> </set>

动画管理

在styles.xml中,添加如下管理代码。
    <style name="AnimationFade">

        <!-- PopupWindow左右弹出的效果 -->
<item name="android:windowEnterAnimation">@anim/in_lefttoright</item>
<item name="android:windowExitAnimation">@anim/out_righttoleft</item>
</style>

【Android】创建Popwindow弹出菜单的两种方式的更多相关文章

  1. 【转】android创建Popwindow弹出菜单的两种方式

    方法一的Activity package com.app.test02; import android.app.Activity; import android.os.Bundle; import a ...

  2. [Android] Android ViewPager 中加载 Fragment的两种方式 方式(二)

    接上文: https://www.cnblogs.com/wukong1688/p/10693338.html Android ViewPager 中加载 Fragmenet的两种方式 方式(一) 二 ...

  3. 怎样在Android开发中FPS游戏实现的两种方式比较

    怎样在Android开发中FPS游戏实现的两种方式比较 如何用Android平台开发FPS游戏,其实现过程有哪些方法,这些方法又有哪些不同的地方呢?首先让我们先了解下什么是FPS 英文名:FPS (F ...

  4. [Android] Android ViewPager 中加载 Fragment的两种方式 方式(一)

    Android ViewPager 中加载 Fragmenet的两种方式 一.当fragment里面的内容较少时,直接 使用fragment xml布局文件填充 文件总数 布局文件:view_one. ...

  5. JS弹出对话框的三种方式

    JS弹出对话框的三种方式 我们用到了alert()方法.prompt()方法.prompt()方法,都是在网页有一个弹出框,那么就让我们探究一下他们之间的区别: 一.第一种:alert()方法 < ...

  6. Android中H5和Native交互的两种方式

    Android中H5和Native交互的两种方式:http://www.jianshu.com/p/bcb5d8582d92 注意事项: 1.android给h5页面注入一个对象(WZApp),这个对 ...

  7. android 长按弹出菜单,复制,粘贴,全选

    <!-- 定义基础布局LinearLayout --> <LinearLayout xmlns:android="http://schemas.android.com/ap ...

  8. Android更改桌面应用程序launcher的两种方式

    http://blog.csdn.net/mdx20072419/article/details/9632779/ launcher,也就是android的桌面应用程序.下图是我正在使用的魅族手机的l ...

  9. js弹出对话框的三种方式(转)

    原文地址:https://www.jb51.net/article/81376.htm javascript的三种对话框是通过调用window对象的三个方法alert(),confirm()和prom ...

随机推荐

  1. HTML5 元素拖放

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. 执行SQL查询脚本

    static void Main(string[] args) { Console.WriteLine("输入用户编号:"); string cusernum = Console. ...

  3. 安装rac遇到的问题总结:

    1. 选择虚拟机工具 这个过程是非常的波折.这次安装也让我吸取了很大教训,获得了宝贵经验. 首先啊,必须了解rac的机制. 共享磁盘+多实例. 这就意味着,我们必须使用一个支持共享磁盘的虚拟机. 第一 ...

  4. Ubuntu引导修复问题

    Ubuntu和Win7双系统,一不小心在Ubuntu下把Win7 C盘的boot目录给删了(手贱呀),然后Win7就没了,后来找了张WinPE启动盘修复了Win7引导结果,Ubuntu系统的引导没了. ...

  5. JavaScript的一点简介(注:本文诸多观点源于JavaScript高级程序设计,如有侵权,立即删除)

    JavaScript是一门最易让人误解的语言,该语言中精华与糟粕并存(可能比一般语言的这个比例要大一些):但“千淘万漉虽辛苦,吹尽黄沙始到金”,层层面纱下是易用灵活.优雅轻灵的内在.很久以前,Java ...

  6. c 陷阱与缺陷(一)

    1.程序在设计时,往往得出正确的结果,但是它并不是程序员自己想要的. 例如: printf("hello world!") 编译器进行编译时不会出现任何问题,但是结果: 提示竟然出 ...

  7. ACM大数模板(支持正负整数)

    之前就保留过简陋的几个用外部数组变量实现的简单大数模板,也没有怎么用过,今天就想着整合封装一下,封装成C++的类,以后需要调用的时候也方便得多. 实现了基本的加减乘除和取模运算的操作符重载,大数除以大 ...

  8. 简易对象垃圾回收框架 for Delphi

    (一).缘起 1.1 我的一个出错程序 程序名称:呼叫处理模块的压力测试工具,分为客户端和服务端. 开发工具:Delhpi 5 相关技术:客户端通过与服务端建立Socket连接来模拟一组电话机的拨入. ...

  9. 获取EIP(汇编语言直接给Delphi变量赋值)

    var EIP: Cardinal; procedure GetEIP(); stdcall; asm pop eax; mov EIP,eax; push eax; end; procedure T ...

  10. git 配置文件

    设置记住密码(默认15分钟): git config --global credential.helper cache 如果想自己设置时间,可以这样做: git config credential.h ...