您的位置:首页 > 运维架构

PopupWindow的简单使用

2016-05-30 16:52 441 查看
public class

PopupWindow

extends Object

java.lang.Object
   ↳android.widget.PopupWindow

Class Overview

A popup window that can be used to display an arbitrary view. The popup window is a floating container that appears on top of the current activity.

PopupWindow的构造函数:
//方法一:
public PopupWindow (Context context)
//方法二:
public PopupWindow(View contentView)
//方法三:
public PopupWindow(View contentView, int width, int height)
//方法四:
public PopupWindow(View contentView, int width, int height, boolean focusable)

注意:这里有四个构造函数,但要生成一个PopupWindow最基本的三个条件是一定要设置的:View contentView,int width, int height ;少任意一个就不可能弹出来PopupWindow!!

构造一个PopupWindow:
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);
PopupWindow没有默认布局,所以需要设置width和height。
设置显示的位置的方法:
//相对某个控件的位置(正左下方),无偏移
showAsDropDown(View anchor):
//相对某个控件的位置,有偏移;xoff表示x轴的偏移,正值表示向左,负值表示向右;yoff表示相对y轴的偏移,正值是向下,负值是向上;
showAsDropDown(View anchor, int xoff, int yoff):
//相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移
showAtLocation(View parent, int gravity, int x, int y):
设置其他属性的函数:
public void dismiss()
public void setFocusable(boolean focusable)  //设置窗体可点击
public void setTouchable(boolean touchable)
public void setOutsideTouchable(boolean touchable)
public void setBackgroundDrawable(Drawable background)  //设置半透明,透明等背景
简单示例:(setAnimationStyle()、showAtLocation())
布局文件activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PopupWindow" />

</RelativeLayout>
PopupWindow弹出窗口的布局popwindowlayout.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" >

<!-- 这里的linearLayout加android:background=""这个属性要谨慎,如果加了后,popwindow是不能半透明了的 -->

<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:background="@drawable/divider_horizontal_line"/>
<TextView
android:id="@+id/shool_intro"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="驾校介绍" />

</LinearLayout> <!-- android:background="@android:color/ " -->
窗口隐藏的动画:res/anim/pophidden_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

<translate
android:duration="1000"
android:fromYDelta="0"
android:toYDelta="50%p" />

<alpha
android:duration="1000"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>
窗口显示的动画:res/anim/popshow_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromYDelta="100%p"
android:toYDelta="0" />

<alpha
android:duration="1000"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
在res/values/styles.xml 中添加动画类型:
<!--  这个是加入的代码 -->
<style name="mypopwindow_anim_style">
<item name="android:windowEnterAnimation">@anim/popshow_anim</item>
<!-- 指定显示的动画xml -->

<item name="android:windowExitAnimation">@anim/pophidden_anim</item>
<!-- 指定消失的动画xml -->
</style>



点击按钮弹出驾校介绍窗口,点击屏幕别处,窗口消失。
Thanks to http://blog.csdn.net/harvic880925/article/details/49272285
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: