android popup

android popup 有两种类型:
1. popup window
2. popup menu

POPUP WINDOW

popup window 和 popup menu 都是显示在其他的空间的上面(z轴)。
下面举例实现的例子;
例子:
package com.hualu.popup;import java.util.ArrayList;
import java.util.List;import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;public class MainActivity extends Activity {private Button Left , right , above, below;private PopupWindow popupWLeft,popupWRight,popupWAbove,popupWBelow ;private Context mContext ;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mContext = this.getApplicationContext() ;Left = (Button)this.findViewById(R.id.Left) ;right = (Button)this.findViewById(R.id.right) ;above = (Button)this.findViewById(R.id.above) ;below = (Button)this.findViewById(R.id.below) ;Button b = (Button)this.findViewById(R.id.button1) ;b.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {startActivity(new Intent("com.hualu.popup.POPUP_MENU")) ;}});Left.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {if(null != popupWLeft )if(!popupWLeft.isShowing()) {showLeftPopupWindow();}else{dimissionLeftPopupWindow() ;}}}) ;right.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {if(null != popupWRight )if(!popupWRight.isShowing()) {showRightPopupWindow();}else{dimissionRightPopupWindow() ;}}}) ;below.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {if(null != popupWBelow )if(!popupWBelow.isShowing()) {showBelowPopupWindow();}else{dimissionBelowPopupWindow() ;}}}) ;above.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {if(null != popupWAbove)if(!popupWAbove.isShowing()) {showAbovePopupWindow();}else{dimissionAbovePopupWindow() ;}}}) ;initLeftPopupWindow();initRightPopupWindow();initAbovePopupWindow();initBelowPopupWindow();}void initLeftPopupWindow(){popupWLeft = new PopupWindow(this) ;popupWLeft.setWidth(100) ;popupWLeft.setHeight(200) ;popupWLeft.setAnimationStyle(R.style.AnimationFade) ;popupWLeft.setFocusable(true) ;//设置为false 界面上除了popup window上面可以点击,其他控件都不可以点击(点击了会无效), 并且popup window会消失//默认是true. 其他控件都可以点击(点击会产生响应的效果), 并且popup window会消失popupWLeft.setOutsideTouchable(true);  popupWLeft.setClippingEnabled(true);popupWLeft.setIgnoreCheekPress();popupWLeft.setInputMethodMode(PopupWindow.INPUT_METHOD_FROM_FOCUSABLE) ;popupWLeft.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.balloon_r_pressed)) ;View view = this.getLayoutInflater().inflate(R.layout.layout_popupwindow_view_listview,null);popupWLeft.setContentView(view) ;ListView list = (ListView)view.findViewById(R.id.listView1);List<String> objects = new ArrayList<String>() ;objects.add("one") ;objects.add("two") ;objects.add("three") ;objects.add("four") ;ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, objects) ;list.setSelector(R.drawable.task_list_selector);list.setAdapter(adapter) ;list.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {Toast.makeText(mContext, ((TextView)view).getText().toString(), Toast.LENGTH_SHORT).show() ;//dimissionLeftPopupWindow() ;}}) ;}void initRightPopupWindow(){popupWRight = new PopupWindow(this) ;popupWRight.setWidth(100) ;popupWRight.setHeight(200) ;popupWRight.setAnimationStyle(R.style.AnimationFade) ;popupWRight.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.balloon_l_pressed)) ;popupWRight.setContentView(this.getLayoutInflater().inflate(R.layout.layout_popupwindow_view,null)) ;}void initAbovePopupWindow(){popupWAbove = new PopupWindow(this) ;popupWAbove.setWidth(100) ;popupWAbove.setHeight(200) ;popupWAbove.setAnimationStyle(R.style.AnimationFade) ;//当输入法弹起来的时候,popupView不会向上移动。popupWAbove.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);popupWAbove.setFocusable(true);//单独设定这句:下面这个两个效果达不到//1. 點擊PopupWindow以外的地方。//2. 點擊左下的Back按鈕。//需要加popupWAbove.setFocusable(true);popupWAbove.setBackgroundDrawable(/*new BitmapDrawable());*/this.getResources().getDrawable(R.drawable.billd_selected)) ;popupWAbove.setContentView(this.getLayoutInflater().inflate(R.layout.layout_popupwindow_view,null)) ;}void initBelowPopupWindow(){popupWBelow = new PopupWindow(this) ;popupWBelow.setWidth(100) ;popupWBelow.setHeight(200) ;popupWBelow.setAnimationStyle(R.style.AnimationFade) ;popupWBelow.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.near_filter_popup)) ;popupWBelow.setContentView(this.getLayoutInflater().inflate(R.layout.layout_popupwindow_view,null)) ;}void showLeftPopupWindow(){popupWLeft.showAtLocation(Left, Gravity.LEFT|Gravity.BOTTOM, 60, 200);}void dimissionLeftPopupWindow(){popupWLeft.dismiss();}void showRightPopupWindow(){popupWRight.showAtLocation(right, Gravity.RIGHT|Gravity.BOTTOM, 50, 140);}void dimissionRightPopupWindow(){popupWRight.dismiss();}void showAbovePopupWindow(){popupWAbove.showAtLocation(above, Gravity.TOP, 50, 185);}void dimissionAbovePopupWindow(){popupWAbove.dismiss();}void showBelowPopupWindow(){popupWBelow.showAsDropDown(below);}void dimissionBelowPopupWindow(){popupWBelow.dismiss();}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.activity_main, menu);return true;}}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity" ><Buttonandroid:id="@+id/Left"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true" android:textColor="@drawable/button_selector"android:background="@drawable/button_drawable_selector"android:text="Left" /><Buttonandroid:id="@+id/above"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignBaseline="@+id/Left"android:layout_alignBottom="@+id/Left"android:layout_toRightOf="@+id/Left"android:text="above" /><Buttonandroid:id="@+id/below"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/Left"android:layout_below="@+id/Left"android:text="below" /><Buttonandroid:id="@+id/right"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/above"android:layout_toRightOf="@+id/below"android:text="right" /><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"android:text="@string/popup_menu" /></RelativeLayout>

layout_popupwindow_view.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" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="TextView" /><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="TextView" /><TextViewandroid:id="@+id/textView3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="TextView" /><TextViewandroid:id="@+id/textView4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="TextView" /></LinearLayout>

layout_popupwindow_view_listview.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" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Header" /><ListViewandroid:id="@+id/listView1"android:layout_width="match_parent"android:layout_height="wrap_content" ></ListView></LinearLayout>

style.xml

<resources><!--Base application theme, dependent on API level. This theme is replacedby AppBaseTheme from res/values-vXX/styles.xml on newer devices.--><style name="AppBaseTheme" parent="android:Theme.Light"><!--Theme customizations available in newer API levels can go inres/values-vXX/styles.xml, while customizations related tobackward-compatibility can go here.--></style><!-- Application theme. --><style name="AppTheme" parent="AppBaseTheme"><!-- All customizations that are NOT specific to a particular API-level can go here. --></style><style name="AnimationFade"><item name="android:windowEnterAnimation">@anim/fade_in</item><item name="android:windowExitAnimation">@anim/fade_out</item></style></resources>

fade_in.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"android:interpolator="@android:anim/accelerate_interpolator"android:fromAlpha="0" android:toAlpha="1" android:duration="1000"></alpha>

fade_out.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"android:interpolator="@android:anim/accelerate_interpolator"android:fromAlpha="1" android:toAlpha="0" android:duration="1000"></alpha>

效果:

POPUP MENU

package com.hualu.popup;import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.Toast;public class PopupMenuActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.layout_popup_menu) ;Button btn = (Button) findViewById(R.id.btn);OnClickListener listener = new OnClickListener() {@SuppressLint("NewApi")@Overridepublic void onClick(View v) {/** Instantiating PopupMenu class */PopupMenu popup = new PopupMenu(getBaseContext(), v);/** Adding menu items to the popumenu */popup.getMenuInflater().inflate(R.menu.popup, popup.getMenu());/** Defining menu item click listener for the popup menu */popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {@Overridepublic boolean onMenuItemClick(MenuItem item) {Toast.makeText(getBaseContext(), "You selected the action : " + item.getTitle(), Toast.LENGTH_SHORT).show();return true;}});/** Showing the popup menu */popup.show();}};btn.setOnClickListener(listener);}}

layout_popup_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent" ><Buttonandroid:id="@+id/btn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_centerVertical="true"android:text="@string/str_btn" />
</RelativeLayout>

strings.xml

    <string name="str_btn">Open Popup Menu</string><string name="action1">Action1</string><string name="action2">Action2</string><string name="action3">Action3</string><string name="popup_menu">popup menu</string>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.hualu.popup"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="17" /><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name="com.hualu.popup.MainActivity"android:label="@string/app_name" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activity android:name=".PopupMenuActivity" ><intent-filter><action android:name="com.hualu.popup.POPUP_MENU" /><category android:name="android.intent.category.DEFAULT"/></intent-filter></activity></application></manifest>

效果:

android popup相关推荐

  1. android 弹出网格菜单,在android中的recyclerView中显示弹出按钮的确...

    as per my above comment可以使用Popup Menu Android Popup Menu displays the menu below the anchor text if ...

  2. Android 获得 usb 权限的两种方式

    0. 前言 在做 USB OTG 通信时,第一步就是要能够获取到 usb 的使用权限,因此特地在此处介绍一下两种我用过的获取 usb 权限方式. 1. 直接在 AndroidManifest.xml ...

  3. android菜单_Android菜单简介

    android菜单 There are three types of menus in Android: Popup, Contextual and Options. Android中有三种类型的菜单 ...

  4. Android usb-serial-for-android驱动库 实现(读卡器rs232串口转usb)插入开发板读取数据

    刚开始接到这个这个任务的时候,看了一下这款读卡器设备的参数(res232协议读卡器技术参数) 初步尝试,利用串口接收 : 1.依赖接入: //串口检测implementation 'com.githu ...

  5. 深度解决 SecurityException: User has not given permission to device UsbDevice

    深度解决 SecurityException: User has not given permission to device UsbDevice 在Android上直接使用USB端口会出现如下权限相 ...

  6. 盘点 6 个 牛牛牛牛牛 的开源项目

    本期推荐开源项目目录: 1. 一种专为 API 而生的 JSON 网络传输协议 2. 为性能而生的 Java 服务器框架 3. 基于 Java 的在线网盘程序 4. Spring Boot 相关漏洞学 ...

  7. Android Menu,OptionMenu选项菜单,ContextMenu上下文菜单,Contextual ActionMode,PopUp Menu,PopU pWi ndow

    Android Menu 一.Opti onMenu选项菜单: (一).简介: 1.A ndroi d应用中的菜单默认是隐藏的,只有当用户点击手机上的ME NU 键,系统才会显示菜单.这种菜单叫做选项 ...

  8. android 开启线程关闭对话框,java – 从后台线程的PopUp对话框Android

    Use the notification system - don't use dialog Boxes in place of notifications If your background se ...

  9. Android studio 自动导入(全部)包 import

    1  Android studio 只有import单个包的快捷键:Alt+Enter.没有Eclipse下的快速导入包的快捷键Ctrl+Shift+O. 2 但android studio设置里有一 ...

最新文章

  1. 转录组测序技术和结果解读(二)——文库构建和测序策略
  2. 初学php时一些术语以及一些基础知识
  3. 专访腾讯云沙开波:从无到有,打造全球领先调度系统
  4. 使用timer控件创建一个简单的报警程序
  5. 贪心算法——洛谷(P4995)跳跳!
  6. 显示器分辨率一直跳_2020如何选择适合自己的显示器?小白选购电脑显示器必看!...
  7. Java 蓝桥杯 判断闰年
  8. atoi 原来将字符串02002xzm100转换为int以后是2002
  9. linux ip地址本地缓存,ip-address – 如何解析组织的IP地址(使用缓存)
  10. Linux wifi优先级高于ethernet
  11. linux卸载keystone服务,OpenStack —— 认证服务Keystone(二)
  12. 神经网络的归一化(batch normalization)
  13. macOS安装wget
  14. 永劫无间游戏设计之上瘾
  15. uniapp开发微信小程序教程(一)
  16. Mysql之账号管理、建库以及四大引擎【入门篇】
  17. mysql-1093 - You can‘t specify target table ‘titles_test‘ for update in FROM clause
  18. python中函数的使用
  19. 散列表及散列冲突解决方案
  20. 网络安全系列-三十一: 网络攻防之红队快速入门

热门文章

  1. 5e服务器显示fps被锁定,csgo强制被锁60帧 被锁60fps解决方法
  2. 2019年“网红”芯片大盘点,哪一颗让你印象最深刻?
  3. 阿里云搭建MQTT服务器并进行本地和服务器端联通测试
  4. 【元胞自动机】元胞自动机多车道信号交叉口仿真【含Matlab源码 818期】
  5. 2019投资总结,5月入市的小账户,收益25%
  6. Maven-3.maven知识点
  7. 电源完整性PI原理知识1
  8. 科技培训专业委员会成立,乐博乐博正式成为会员单位
  9. java开发微信设计论文_集客微信公众号: 本科毕业设计:基于WxJava框架的集客微信公众号的设计与实现...
  10. 教你怎么不添加付款方式订阅苹果arcade