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拖拽的思路: ()根 ...
随机推荐
- WSARecv()
简述:从一个套接口接收数据. #include <winsock2.h> int WSAAPI WSARecv ( SOCKET s, LPWSABUF lpBuffers, DWORD ...
- css控制图片自适应大小
相信大家做网页时经常会碰到大分辨率的图片会把表格涨破以致漂亮的网页面目全非,但只要使用以下的CSS语句即可解决. 该CSS的功能是:大于600的图片自动调整为600显示. <style ...
- 结构类模式(六):享元(Flyweight)
定义 运用共享技术有效的支持大量细粒度的对象. 两个状态 内蕴状态存储在享元内部,不会随环境的改变而有所不同,是可以共享的. 外蕴状态是不可以共享的,它随环境的改变而改变的,因此外蕴状态是由客户端来保 ...
- non-manifold Mesh(非流形网格)
三角网格曲面中,大多的算法基于流形网格manifold mesh,其定义如下: 1)Each edge is incident to only one or two faces: 一条网格边为一个或两 ...
- 为什么要把js代码写到<!--//-->中
是为了兼容,不支持js的浏览器会把其中的内容当做html注释.
- Swift 实现Bitmask Option(Enum)
在Swift中实现ObjC中的NS_OPTION不是通过enum,而是通过conform RawOptionSetType protocol的struct来实现的. 代码如下: struct Test ...
- Correct thread terminate and destroy
http://www.techques.com/question/1-3788743/Correct-thread-destroy Hello At my form I create TFrame a ...
- c# asp.net 鼠标改变控件坐标位置,更改控件坐标,注册表保存读取,打印,查找局域网内打印机等等收集
界面虽然被我弄的很难看,但功能还可以 里边注册表的路径自己设置一下,或者加一个创建注册表的语句,不然会报错 前台: <%@ Page Language="C#" AutoEv ...
- BZOJ 1084: [SCOI2005]最大子矩阵 DP
1084: [SCOI2005]最大子矩阵 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1084 Description 这里有一个n* ...
- 对.NET中Hashtable和ArryList的理解
1.HashTabel 在.NET Framework中,Hashtable是System.Collections命名空间提供的集合对象,同时它也是一个可变长的数组,用于处理和表现类似key/valu ...