Android 高级UI设计笔记19:PopupWindow使用详解
1. PopupWindow使用
PopupWindow这个类用来实现一个弹出框,可以使用任意布局的View作为其内容,这个弹出框是悬浮在当前activity之上的。
2. PopupWindow使用的案例:
(1)首先是我们弹出框的布局设计,如下:
<?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="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical" > <!-- 这里的linearLayout加android:background=""这个属性要谨慎,如果加了后,popwindow是不能半透明了的 --> <Button
android:id="@+id/first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="@android:color/holo_red_light"
android:text="第一个按钮" /> <Button
android:id="@+id/second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@android:color/holo_red_light"
android:text="第二个按钮" /> <Button
android:id="@+id/third"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@android:color/holo_red_light"
android:text="第三个按钮" /> </LinearLayout>
布局效果图,如下:

(2)主布局activity_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="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical" > <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/start"
android:text="@string/click"/> </LinearLayout>
(3)首先当我们进入MainActivity(承载着activity_main.xml),当我们点击按钮,PopupWindow会从下往上弹出显示;PopupWindow弹出来之后,当我们点击PopupWindow之外的地方的时候,PopupWindow就会从上往下收缩隐藏。
这里需要定义PopupWindow弹出和隐藏的两个动画,如下:
在res / 下新建一个文件夹anim,进而anim下新建两个xml文件,如图所示:

其中,pophidden_anim.xml,如下:
<?xml version="1.0" encoding="utf-8"?> <!-- android<set>标签代表一系列的帧动画,可以在里面添加动画效果 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" > <translate
android:duration="2000"
android:fromYDelta="0"
android:toYDelta="50%p" /> <alpha
android:duration="2000"
android:fromAlpha="1.0"
android:toAlpha="0.0" /> </set>
popshow_anim的代码如下:
<?xml version="1.0" encoding="utf-8"?> <!--android<set>标签代表一系列的帧动画,可以在里面添加动画效果 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" > <translate
android:duration="2000"
android:fromYDelta="100%p"
android:toYDelta="0" /> <alpha
android:duration="2000"
android:fromAlpha="0.0"
android:toAlpha="1.0" /> </set>
备注:
android:fromYDelta --- 表示Y的起始值
android:toYDelta --- 表示Y的结束值
在这些属性里面还可以加上%和p,例如:
android:toXDelta="100%",表示自身的100%,也就是从View自己的位置开始。
android:toXDelta="80%p",表示父层View的80%,是以它父层View为参照的。
另外,如下:
android:fromXDelta="0" android:toXDelta="-100%p" 往左邊消失
android:fromXDelta="-100%p" android:toXDelta="0" 從左邊進
android:fromXDelta="0" android:toXDelta="100%p" 往右邊消失
android:fromXDelta="100%p" android:toXDelta="0" 從右邊進
经查阅资料才发现动画的启始位置虽然是在控件的左下角,但是相对位置却不是我们平时想的那样.
在实现左右动画的时候,其相对位置应该为(位置2为起始位置):

在实现上下动画的时候,其相对位置应该为(位置2为起始位置):

上面弹出 和 隐藏两种动画,我们在res / values / styles中定义动画,如下:
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by 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 in
res/values-vXX/styles.xml, while customizations related to
backward-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="mypopwindow_anim_style">
<!-- 指定显示的动画xml -->
<item name="android:windowEnterAnimation">@anim/popshow_anim</item>
<!-- 指定消失的动画xml -->
<item name="android:windowExitAnimation">@anim/pophidden_anim</item>
</style>
</resources>
(4)来到MainActivity之中,如下:
package com.himi.popwindowdemo; import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start = (Button) findViewById(R.id.start);
start.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
showPopwindow();
} });
} /**
* 显示popupWindow
*/
private void showPopwindow() {
// 利用layoutInflater获得View
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.popwindowlayout, null); // 下面是两种方法得到宽度和高度 getWindow().getDecorView().getWidth() PopupWindow window = new PopupWindow(view, WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT); // 设置popWindow弹出窗体可点击,这句话必须添加,并且是true
window.setFocusable(true); // 实例化一个ColorDrawable颜色为半透明
ColorDrawable dw = new ColorDrawable(0xb0000000);
window.setBackgroundDrawable(dw); // 设置popWindow的显示和消失动画
window.setAnimationStyle(R.style.mypopwindow_anim_style);
// 在底部显示
window.showAtLocation(MainActivity.this.findViewById(R.id.start), Gravity.BOTTOM, 0, 0); // 这里检验popWindow里的button是否可以点击
Button first = (Button) view.findViewById(R.id.first);
first.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) { System.out.println("第一个按钮被点击了");
}
}); // popWindow消失监听方法
window.setOnDismissListener(new OnDismissListener() { @Override
public void onDismiss() {
System.out.println("popWindow消失");
}
}); }
}
(5)部署程序到模拟器上,如下:
刚开始进入程序,效果如下:

点击上面的按钮,效果如下,popupwindow 弹出显示:

点击popupwindow之外的地方,效果如下,popupwindow 收缩隐藏:

3. PopupWindow 在指定位置上的显示(重点):
(转载:http://www.cnblogs.com/zhwl/archive/2013/10/17/3373531.html)
这里主要介绍PopupWindow 在控件的各个方向上的显示(上、下、左、右),主要用到popupWindow 的showAtLocation()方法。
注意参数Gravity.NO_GRAVITY:用来标明没有设定对齐方向
(1)在控件的上方:
private void showPopUp(View v) {
LinearLayout layout = new LinearLayout(this);
layout.setBackgroundColor(Color.GRAY);
TextView tv = new TextView(this);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setText("I'm a pop !");
tv.setTextColor(Color.WHITE);
layout.addView(tv);
popupWindow = new PopupWindow(layout,120,120);
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
int[] location = new int[2];
v.getLocationOnScreen(location);
popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0], location[1]-popupWindow.getHeight());
}
(2)在控件的下方:
在控件的其他方向上显示只需修改最后一行代码即可,如:
popupWindow.showAsDropDown(v);
(3)在控件的左边:
popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]-popupWindow.getWidth(), location[1]);
(4)在控件的右边:
popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]+v.getWidth(), location[1]);
Android 高级UI设计笔记19:PopupWindow使用详解的更多相关文章
- Android 高级UI设计笔记07:RecyclerView 的详解
1. 使用RecyclerView 在 Android 应用程序中列表是一个非常重要的控件,适用场合非常多,如新闻列表.应用列表.消息列表等等,但是从Android 一出生到现在并没有非常 ...
- Android 高级UI设计笔记01:使用ExpandableListView组件(ListView的扩展)
1.ExpandableListView是一个用来显示二级节点的ListView. 比如如下效果的界面: 2.使用ExpandableListView步骤 (1)要给ExpandableListVie ...
- Android 高级UI设计笔记06:仿微信图片选择器(转载)
仿微信图片选择器: 一.项目整体分析: 1. Android加载图片的3个目标: (1)尽可能的去避免内存溢出. a. 根据图片的显示大小去压缩图片 b. 使用缓存对我们图片进行管理(LruCache ...
- Android 高级UI设计笔记21:Android SegmentView(分段选择控件)
1. 分段控制(SegmentView) 首先我们先看看什么是SegmentView的效果,如下: 分段控制这个View控件是ios7的分段控制,和QQ消息页面顶部的效果一样,android没有这个控 ...
- Android 高级UI设计笔记17:Android在非UI线程中显示Toast
1. 子线程的Toast怎么显示不出来? 因为Toast在创建的时候会依赖于一个Handler,并且一个Handler是需要有一个Looper才能够创建,而普通的线程是不会自动去创建一个Looper对 ...
- Android 高级UI设计笔记15:HorizontalScrollView之 实现画廊式图片浏览器
1. HorizontalScrollView 本来,画廊式的图片浏览器,使用Android中的Gallery就能轻松完成,但是Google说Gallery每次切换图片时都要新建视图,造成太多的资源浪 ...
- Android 高级UI设计笔记09:Android如何实现无限滚动列表
ListView和GridView已经成为原生的Android应用实现中两个最流行的设计模式.目前,这些模式被大量的开发者使用,主要是因为他们是简单而直接的实现,同时他们提供了一个良好,整洁的用户体验 ...
- Android 高级UI设计笔记08:Android开发者常用的7款Android UI组件(转载)
Android开发是目前最热门的移动开发技术之一,随着开发者的不断努力和Android社区的进步,Android开发技术已经日趋成熟,当然,在Android开源社区中也涌现了很多不错的开源UI项目,它 ...
- Android 高级UI设计笔记02:可以拖动交换item位置的GridView(转载)
如果大家不知道GridView基本使用,可以先参见:Android(java)学习笔记154:使用GridView以及重写BaseAdapter 1. 首先我们明白GridView拖拽的思路: ()根 ...
随机推荐
- Microsoft Office Excel 不能访问文件“XXXXXXXXXXXXX.xls”。 可能的原因有:
解决办法:1. 1).通过webconfig中增加模拟,加入管理员权限, <identity impersonate="true" userName="系统管理员& ...
- HTML结构标签介绍
HTML:超文本标记语言 介绍HTML基本标记 1:头部标记(head)----- 头部的内容不会再页面上显示 在头部元素中,一般需要包括标题<title>,基本信息(文档样式, ...
- 一种将Region转为Polyline的方法
在AutoCAD.NET二次开发中,如果要将面域转为Polyline主要有以下几种方式: 1.使用Explode将面域炸成Line和Arc,然后再串起来,此方法可用于AutoCAD2007开始的所有版 ...
- C# 固定窗体大小且不能鼠标调整大小完美实现
1.先把MaximizeBox和MinimumBox设置为false,这时你发现最大最小化按钮不见了,但是鼠标仍能调整窗体的大小. 2.有人说直接把MaximumSize和MinimumSize设置成 ...
- web.xml 详解
http://xmlns.jcp.org/xml/ns/javaee 重定向为 http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javae ...
- branch
1.删除分支 git branch -d branch_name error: The branch 'branch_name' is not fully merged. If you are sur ...
- UI:触摸事件 与 事件的回应
事件分类:晃动.触摸.远程控制(如遥控器.红外控制) 触摸开始时候的方法(判断单击,双击,三击事件可以写在这里) -(void)touchesBegan:(NSSet *)touches withEv ...
- flash 定义主舞台窗口大小
1:[SWF(width=100 height=100)] 写在主类上面2:设置stageScaleMode属性为false;
- jquery checkbox选中状态
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 推荐第三方Oracle客户端查询工具
1.SqlDbx 官方地址:http://www.sqldbx.com/personal_edition.htm 2.devart http://www.devart.com/dbforge/orac ...