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]); 

最新文章

  1. POJ2104 K-th Number[主席树]【学习笔记】
  2. 算法系列:FFT 003
  3. Kafka实战系列--Kafka API使用体验
  4. [Mongodb]3.2版本的一些基本操作--待续
  5. &ldquo;耐撕&rdquo;团队记账本 剧透
  6. Java 查询URL对应IP地址
  7. Android 中的 Service 全面总结 (转)
  8. Service的启动与停止、绑定与解绑
  9. ArcGIS中文件共享锁定数据溢出 这个方法不行,建议用gdb,不要用mdb
  10. Seattle(65) lypzxy的博客
  11. WPF DataBinding之我见
  12. 为什么EXE文件出现了不该出现的“盾牌”
  13. 利用GeneratedKeyHolder获得新增数据主键值
  14. ORM映射设计思想
  15. 关于eclipse新建web项目,提示:&quot;The superclass &quot;javax.servlet.http.HttpServlet&quot; was not found on the Java&quot;解决办法
  16. EmguCV中图像类型进行转换
  17. Linux内核异常处理体系结构详解(一)【转】
  18. 3173. 【GDOI2103模拟3.17】扫雷游戏(搜索 + 剪枝)
  19. Ubuntu 18.04 LTS 常用软件安装杂记
  20. eclipse svn合并小结

热门文章

  1. linux下find查找命令用法
  2. HDU 5443 The Water Problem (水题,暴力)
  3. zendstudio 出现failed to create the java machine转
  4. libev笔记
  5. erlang pool模块。
  6. wikioi 1098 均分纸牌
  7. pycharm快捷键大全
  8. libcurl API:CURLOPT_REFERER的用法
  9. eclipse提示servlet不存在 的解决办法
  10. System.Runtime.InteropServices.COMException (0x800706BA) 解决方法