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使用详解的更多相关文章

  1. Android 高级UI设计笔记07:RecyclerView 的详解

    1. 使用RecyclerView       在 Android 应用程序中列表是一个非常重要的控件,适用场合非常多,如新闻列表.应用列表.消息列表等等,但是从Android 一出生到现在并没有非常 ...

  2. Android 高级UI设计笔记01:使用ExpandableListView组件(ListView的扩展)

    1.ExpandableListView是一个用来显示二级节点的ListView. 比如如下效果的界面: 2.使用ExpandableListView步骤 (1)要给ExpandableListVie ...

  3. Android 高级UI设计笔记06:仿微信图片选择器(转载)

    仿微信图片选择器: 一.项目整体分析: 1. Android加载图片的3个目标: (1)尽可能的去避免内存溢出. a. 根据图片的显示大小去压缩图片 b. 使用缓存对我们图片进行管理(LruCache ...

  4. Android 高级UI设计笔记21:Android SegmentView(分段选择控件)

    1. 分段控制(SegmentView) 首先我们先看看什么是SegmentView的效果,如下: 分段控制这个View控件是ios7的分段控制,和QQ消息页面顶部的效果一样,android没有这个控 ...

  5. Android 高级UI设计笔记17:Android在非UI线程中显示Toast

    1. 子线程的Toast怎么显示不出来? 因为Toast在创建的时候会依赖于一个Handler,并且一个Handler是需要有一个Looper才能够创建,而普通的线程是不会自动去创建一个Looper对 ...

  6. Android 高级UI设计笔记15:HorizontalScrollView之 实现画廊式图片浏览器

    1. HorizontalScrollView 本来,画廊式的图片浏览器,使用Android中的Gallery就能轻松完成,但是Google说Gallery每次切换图片时都要新建视图,造成太多的资源浪 ...

  7. Android 高级UI设计笔记09:Android如何实现无限滚动列表

    ListView和GridView已经成为原生的Android应用实现中两个最流行的设计模式.目前,这些模式被大量的开发者使用,主要是因为他们是简单而直接的实现,同时他们提供了一个良好,整洁的用户体验 ...

  8. Android 高级UI设计笔记08:Android开发者常用的7款Android UI组件(转载)

    Android开发是目前最热门的移动开发技术之一,随着开发者的不断努力和Android社区的进步,Android开发技术已经日趋成熟,当然,在Android开源社区中也涌现了很多不错的开源UI项目,它 ...

  9. Android 高级UI设计笔记02:可以拖动交换item位置的GridView(转载)

    如果大家不知道GridView基本使用,可以先参见:Android(java)学习笔记154:使用GridView以及重写BaseAdapter 1. 首先我们明白GridView拖拽的思路: ()根 ...

随机推荐

  1. ESB的XmlProPertyMgr类的getNode(xxx)方法

    //------------------------------------------------------------------------------ public static Eleme ...

  2. windows服务安装及卸载

    1)安装脚本Install.bat%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe JobSchedule.exeNet ...

  3. IOC知识

    1.两个基本概念 IOC(Inversion of Control ):反转控制,即将控制权反转出去. DI(Dependency Injection):依赖注入,根据依赖关系进行注入. DI是实现I ...

  4. Linux下安装protobuf并实现简单的客户端服务器端通信

    http://code.google.com/p/protobuf/downloads/list上可以下载Protobuf的源代码. 安装步骤如下所示: 1>tar -xzf protobuf- ...

  5. Codeforces 161 D. Distance in Tree (树dp)

    题目链接:http://codeforces.com/problemset/problem/161/D 题意: 给你一棵树,问你有多少对点的距离为k. 思路: dp[i][j]表示离i节点距离为j的点 ...

  6. POJ3041Asteroids(二分图最少顶点覆盖)

    最少顶点覆盖 = 二分图最大匹配 证明见   http://hi.baidu.com/keeponac/item/111e3438988c786b7d034b56

  7. 备份Xcode6的配色主题以及代码模板

    ~/Library/Developer/Xcode/UserData/FontAndColorThemes ~/Library/Developer/Xcode/UserData/CodeSnippet ...

  8. hdoj 5288 OO’s Sequence

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5288 //*************头文件区************* #include<ios ...

  9. WPF的DataGrid绑定ItemsSource后第一次加载数据有个别列移位的解决办法

    最近用WPF的DataGrid的时候,发现一个很弱智的问题,DataGrid的ItemsSource是绑定了一个属性: 然后取数给这个集合赋值的时候,第一次赋值,就会出现列移位 起初还以为是显卡的问题 ...

  10. sc7731 Android 5.1 LCD驱动简明笔记之一

    基于展讯sc7731 - Android 5.1 代码分析浏览.将屏蔽细节,把握整体,并且不涉及其他设备和LCD的交互. 以下对sc7731 lcd大体流程进行简要说明. 第一,lcd 的两个阶段 1 ...