【转】android创建Popwindow弹出菜单的两种方式
方法一的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弹出菜单
- <?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
弹出动画
- <?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>
弹回动画
- <?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>
动画管理
- <style name="AnimationFade">
- <!-- PopupWindow左右弹出的效果 -->
- <item name="android:windowEnterAnimation">@anim/in_lefttoright</item>
- <item name="android:windowExitAnimation">@anim/out_righttoleft</item>
- </style>
【转】android创建Popwindow弹出菜单的两种方式的更多相关文章
- 【Android】创建Popwindow弹出菜单的两种方式
方法一的Activity package com.app.test02; import android.app.Activity; import android.os.Bundle; import a ...
- JS弹出对话框的三种方式
JS弹出对话框的三种方式 我们用到了alert()方法.prompt()方法.prompt()方法,都是在网页有一个弹出框,那么就让我们探究一下他们之间的区别: 一.第一种:alert()方法 < ...
- [Android] Android ViewPager 中加载 Fragment的两种方式 方式(二)
接上文: https://www.cnblogs.com/wukong1688/p/10693338.html Android ViewPager 中加载 Fragmenet的两种方式 方式(一) 二 ...
- 怎样在Android开发中FPS游戏实现的两种方式比较
怎样在Android开发中FPS游戏实现的两种方式比较 如何用Android平台开发FPS游戏,其实现过程有哪些方法,这些方法又有哪些不同的地方呢?首先让我们先了解下什么是FPS 英文名:FPS (F ...
- [Android] Android ViewPager 中加载 Fragment的两种方式 方式(一)
Android ViewPager 中加载 Fragmenet的两种方式 一.当fragment里面的内容较少时,直接 使用fragment xml布局文件填充 文件总数 布局文件:view_one. ...
- Android中H5和Native交互的两种方式
Android中H5和Native交互的两种方式:http://www.jianshu.com/p/bcb5d8582d92 注意事项: 1.android给h5页面注入一个对象(WZApp),这个对 ...
- android 长按弹出菜单,复制,粘贴,全选
<!-- 定义基础布局LinearLayout --> <LinearLayout xmlns:android="http://schemas.android.com/ap ...
- js弹出对话框的三种方式(转)
原文地址:https://www.jb51.net/article/81376.htm javascript的三种对话框是通过调用window对象的三个方法alert(),confirm()和prom ...
- Android更改桌面应用程序launcher的两种方式
http://blog.csdn.net/mdx20072419/article/details/9632779/ launcher,也就是android的桌面应用程序.下图是我正在使用的魅族手机的l ...
随机推荐
- php形式的内容被处理
/** * 过滤HTML内容RETURN * * @param $string * @param bool $html * * @return array|string */ public stati ...
- Deep Learning Papers
一.Image Classification(Recognition) lenet: http://yann.lecun.com/exdb/publis/pdf/lecun-01a.pdf alexn ...
- Android SDCard Mount 流程分析
前段时间对Android 的SDCard unmount 流程进行了几篇简短的分析,由于当时只是纸上谈兵,没有实际上的跟进,可能会有一些误导人或者小错误.今天重新梳理了头绪,针对mount的流程再重新 ...
- 二叉搜索树(Binary Search Tree)--C语言描述(转)
图解二叉搜索树概念 二叉树呢,其实就是链表的一个二维形式,而二叉搜索树,就是一种特殊的二叉树,这种二叉树有个特点:对任意节点而言,左孩子(当然了,存在的话)的值总是小于本身,而右孩子(存在的话)的值总 ...
- hdu 4864 Task---2014 Multi-University Training Contest 1
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4864 Task Time Limit: 4000/2000 MS (Java/Others) M ...
- JavaScript通告/订阅的例子
原文链接: Pub/Sub JavaScript Object原始日期: 2014年6一个月11日本: 2014年6月13日 翻译人员: 铁锚 高效AJAX站点的三大杀器: 事件代理, 浏览历史管理, ...
- js 正则学习小记之匹配字符串优化篇
原文:js 正则学习小记之匹配字符串优化篇 昨天在<js 正则学习小记之匹配字符串>谈到 个字符,除了第一个 个,只有 个转义( 个字符),所以 次,只有 次成功.这 次匹配失败,需要回溯 ...
- 《代码的第一行——Android》封面诞生
<代码的第一行--Android>已经上市近一个月,现在的情况是相当不错的销售,也特别感谢众多朋友的支持. 其实一本好书,假设你想卖.除了给予外力所要求的内容.封面设计是至关重要的,这本书 ...
- oracle_oracle中修改日期的显示格式
我的现在的日期格式是 ,要改成英文的需要输入一下命令: ALTER SESSION SET NLS_DATE_LANGUAGE=AMERICAN; 修改后变为: 同样也得若是英文要想 ...
- 兼容安卓的javaproject1.0
<pre class="java" name="code"> //兼容安卓的系统package cn.com.likeshow; import ja ...