转载http://blog.csdn.net/lidew521/article/details/8976627

PopupWindow是一个可以显示在当前Activity之上的浮动容器,PopupWindow弹出的位置是能够改变的,按照有无偏移量,可以分为无偏移和有便宜两种;按照参照对象的不同又可以分为两种:相对某个控件(Anchor锚点)的位置和在父容器内部的相对位置。

创建Android应用:Project Name:PopupWindow,Android2.2,Application Name:泡泡窗口,Packagename:cn.itcast.popwindow,CreateActivity:MainActivity。

1. 界面

在界面上添加按钮,点击后显示Pop窗口。

/PopupWindow/res/values/strings.xml

<?xml version="1.0"encoding="utf-8"?>

<resources>

<string name="hello">Hello World,MainActivity!</string>

<string name="app_name">泡泡窗口</string>

<string name="button">打开泡泡窗口</string>

</resources>

/PopupWindow/res/layout/main.xml

<?xml version="1.0"encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical"

android:id="@+id/main"

>

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/button"

android:onClick="openPopWindow"

/>

</LinearLayout>

2. 实现按钮点击方法

创建PopupWIndow:

LayoutInflater mLayoutInflater = (LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE);

View contentView = mLayoutInflater.inflate(R.layout.xxx,null); //R.layout.xxx为界面文件

popupWindow = newPopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);

popupWindow.setBackgroundDrawable(newBitmapDrawable());

popupWindow.setFocusable(true);//取得焦点,创建出来的PopupWindow默认无焦点

显示PopupWindow的方法:

showAsDropDown(Viewanchor)相对某个控件的位置(正下方),无偏移

showAsDropDown(Viewanchor, int xoff, int yoff)相对某个控件的位置,有偏移,xoff X轴的偏移量,yoff Y轴的偏移量

showAtLocation(Viewparent, int gravity, int x, int y)在父容器的什么位置,gravity为相对位置,如:正中央Gravity.CENTER、下方Gravity.BOTTOM、Gravity.Right|Gravity.BOTTOM右下方等,后面两个参数为x/y轴的偏移量。

关闭PopupWindow:dismiss()

/PopupWindow/src/cn/itcast/popwindow/MainActivity.java

package cn.itcast.popwindow;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import android.app.Activity;

importandroid.graphics.drawable.BitmapDrawable;

import android.os.Bundle;

import android.view.Gravity;

import android.view.View;

import android.view.ViewGroup;

import android.widget.AdapterView;

importandroid.widget.AdapterView.OnItemClickListener;

import android.widget.GridView;

import android.widget.ListAdapter;

import android.widget.PopupWindow;

import android.widget.SimpleAdapter;

public class MainActivity extendsActivity {

PopupWindowpopupWindow;

Viewparent;

privateint[] images ={R.drawable.i1,R.drawable.i2,R.drawable.i3,R.drawable.i4,R.drawable.i5,R.drawable.i6,R.drawable.i7,R.drawable.i8};

privateString[] names = {"搜索", "文件管理", "下载管理", "全屏", "网址", "书签", "加入书签", "分享页面"};

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

       ViewcontentView = getLayoutInflater().inflate(R.layout.popwindow, null);

      GridView gridView = (GridView) contentView.findViewById(R.id.gridView);

      gridView.setAdapter(getAdapter());

      gridView.setOnItemClickListener(new ItemClickListener());

       

       popupWindow = new PopupWindow(contentView,ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

       popupWindow.setFocusable(true);//取得焦点

       popupWindow.setBackgroundDrawable(newBitmapDrawable()); //点击空白的地方关闭PopupWindow

       popupWindow.setAnimationStyle(R.style.animation);

parent =this.findViewById(R.id.main);

}

private final class ItemClickListener implements OnItemClickListener{

publicvoid onItemClick(AdapterView<?> parent, View view, int position, long id){

if(popupWindow.isShowing())popupWindow.dismiss();//关闭

//....

}

}

private ListAdapter getAdapter() {

List<HashMap<String,Object>> data = new ArrayList<HashMap<String,Object>>();

for(int i = 0 ; i <images.length ; i++ ){

HashMap<String,Object> item = new HashMap<String, Object>();

item.put("image",images[i]);

item.put("name",names[i]);

data.add(item);

}

SimpleAdaptersimpleAdapter = new SimpleAdapter(this, data, R.layout.grid_item,

newString[]{"image", "name"}, new int[]{R.id.imageView,R.id.textView});

returnsimpleAdapter;

}

publicvoid openPopWindow(Viewv){

       popupWindow.showAtLocation(parent,Gravity.BOTTOM, 0, 0);

}

}

3. 弹出窗口

/PopupWindow/res/layout/popwindow.xml

<?xml version="1.0"encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:background="@drawable/bg"

>

<GridView

android:layout_width="match_parent"

android:layout_height="match_parent"

android:numColumns="4"

android:horizontalSpacing="10dp"

android:verticalSpacing="10dp"

android:id="@+id/gridView"

/>

</LinearLayout>

/PopupWindow/res/drawable/bg.xml

<?xml version="1.0"encoding="utf-8"?>

<shape

xmlns:android="http://schemas.android.com/apk/res/android"

android:shape="rectangle">

<gradient

android:angle="270"

android:endColor="#1DC9CD"

android:startColor="#A2E0FB"/>

<padding

android:left="2dp"

android:top="2dp"

android:right="2dp"

android:bottom="2dp" />

</shape>

GridView:

/PopupWindow/res/layout/grid_item.xml

<?xml version="1.0"encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:gravity="center"

>

<ImageView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/imageView"

/>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:gravity="center"

android:textSize="16sp"

android:textColor="#000099"

android:id="@+id/textView"

/>

</LinearLayout>

4. 使用的图像

/PopupWindow/res/drawable/i1.png

/PopupWindow/res/drawable/i2.png

/PopupWindow/res/drawable/i3.png

/PopupWindow/res/drawable/i4.png

/PopupWindow/res/drawable/i5.png

/PopupWindow/res/drawable/i6.png

/PopupWindow/res/drawable/i7.png

/PopupWindow/res/drawable/i8.png

5. 显示/隐藏的动画

/PopupWindow/res/values/styles.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>

<style name="animation">

<itemname="android:windowEnterAnimation">@anim/enter</item>

<itemname="android:windowExitAnimation">@anim/out</item>

</style>

</resources>

/PopupWindow/res/anim/enter.xml

<?xml version="1.0"encoding="utf-8"?>

<setxmlns:android="http://schemas.android.com/apk/res/android"

android:shareInterpolator="false">

<translate

android:fromYDelta="100%p"

android:toYDelta="0"

android:duration="500"

/>

<alpha

android:fromAlpha="0.7"

android:toAlpha="1.0"

android:duration="300"

/>

</set>

/PopupWindow/res/anim/out.xml

<?xml version="1.0"encoding="utf-8"?>

<setxmlns:android="http://schemas.android.com/apk/res/android"

android:shareInterpolator="false">

<translate

android:fromYDelta="0"

android:toYDelta="100%p"

android:duration="3000"

/>

<alpha

android:fromAlpha="1.0"

android:toAlpha="0.5"

android:duration="2000"

/>

</set>

位置图如下:

显示效果:

版权声明:本文为博主原创文章,未经博主允许不得转载。

android学习笔记---63-PopupWindow,泡泡窗口的实现的更多相关文章

  1. Android学习笔记之Activity详解

    1 理解Activity Activity就是一个包含应用程序界面的窗口,是Android四大组件之一.一个应用程序可以包含零个或多个Activity.一个Activity的生命周期是指从屏幕上显示那 ...

  2. 【转】Pro Android学习笔记(十二):了解Intent(下)

    解析Intent,寻找匹配Activity 如果给出component名字(包名.类名)是explicit intent,否则是implicit intent.对于explicit intent,关键 ...

  3. 【转】Pro Android学习笔记(二):开发环境:基础概念、连接真实设备、生命周期

    在Android学习笔记(二):安装环境中已经有相应的内容.看看何为新.这是在source网站上的Android架构图,和标准图没有区别,只是这张图颜色好看多了,录之.本笔记主要讲述Android开发 ...

  4. Android 学习笔记之Volley(七)实现Json数据加载和解析...

    学习内容: 1.使用Volley实现异步加载Json数据...   Volley的第二大请求就是通过发送请求异步实现Json数据信息的加载,加载Json数据有两种方式,一种是通过获取Json对象,然后 ...

  5. Android学习笔记进阶之在图片上涂鸦(能清屏)

    Android学习笔记进阶之在图片上涂鸦(能清屏) 2013-11-19 10:52 117人阅读 评论(0) 收藏 举报 HandWritingActivity.java package xiaos ...

  6. android学习笔记36——使用原始XML文件

    XML文件 android中使用XML文件,需要开发者手动创建res/xml文件夹. 实例如下: book.xml==> <?xml version="1.0" enc ...

  7. Android学习笔记之JSON数据解析

    转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...

  8. udacity android 学习笔记: lesson 4 part b

    udacity android 学习笔记: lesson 4 part b 作者:干货店打杂的 /titer1 /Archimedes 出处:https://code.csdn.net/titer1 ...

  9. Android学习笔记36:使用SQLite方式存储数据

    在Android中一共提供了5种数据存储方式,分别为: (1)Files:通过FileInputStream和FileOutputStream对文件进行操作.具体使用方法可以参阅博文<Andro ...

  10. Pro Android学习笔记 ActionBar(1):Home图标区

     Pro Android学习笔记(四八):ActionBar(1):Home图标区 2013年03月10日 ⁄ 综合 ⁄ 共 3256字 ⁄ 字号 小 中 大 ⁄ 评论关闭 ActionBar在A ...

随机推荐

  1. css布局之负margin妙用及其他实现

    相信大家在项目的开发中都遇到过这样的需求,一行放X(X>1)个块且相邻块之间的间距相同. 大概就是上面这个样子,下面介绍几种实现的方式. 1.负margin大法 设置好元素的宽度和留白占满父级的 ...

  2. js 获取元素在页面上的偏移量的最佳方式

    使用js制作效果时,我们常常要获取某个元素在页面上的偏移量(例如tip提示框功能).而获取偏移量可以直接获取相对于document的偏移量,也可以获取相对与视口的偏移量(viewpoint)加上页面滚 ...

  3. 关于VS2013连接远程数据库服务器的蛋疼问题

    填写完用户名和密码后,点击数据库下拉菜单,接着就报错误

  4. https大势已来?看腾讯专家如何在高并发压测中支持https

    WeTest 导读 用epoll编写一个高并发网络程序是很常见的任务,但在epoll中加入ssl层的支持则是一个不常见的场景.腾讯WeTest服务器压力测产品,在用户反馈中收到了不少支持https协议 ...

  5. GridView下DropDownList 的选择方法onselectedindexchanged 实现方法

    在GridView下面绑定好了下拉框,我们常常会遇到一个问题, 选择方法怎么实现呢,用js总是难的去算是在GridView的第几行第几个元素,因为服务器的id和客户端的id经常变化让js根本无从找起, ...

  6. Andoid源码 BUG修改集合--不断更新

    BUG001:很抱歉,***已停止运行 网上查找问题原因很多,有人说事缓存不够,作为一个开发者,需要从代码解决问题 比如,这次遇到一个"很抱歉,instant已停止运行",inst ...

  7. 高放的c++学习笔记之类

    类的基本思想是数据抽象和封装1.this 成员函数通过一个名为this的额外隐式参数来访问调用它的对象,当我们调用一个函数的时候,用请求该函数的对象的初始化this. 如果某个类的名字为sale,某个 ...

  8. 完整版的strcpy函数

    char *strcpy(char *strDest,const char *strSrc) { assert((strDest!=NULL) && (strSrc!=NULL)); ...

  9. Ubuntu 12.04(32位)下PHP环境的搭建(LAMP)

    Ubuntu 12.04 32位 下默认安装为5.3.10  不是以下图文中的5.4 1.首先打开命令行,切换到root身份,获得最新的软件包 su root sudo apt-get install ...

  10. python: list[-1] 与 list[-1:] 的区别

    >>> l '3.542485\t1.977398\t-1\r\n' >>> l.split() ['3.542485', '1.977398', '-1'] &g ...