您的位置:首页 > 移动开发 > Android开发

Android_PopupWindow使用介绍

2013-12-29 09:11 429 查看
本博文为子墨原创,转载请注明出处!
http://blog.csdn.net/zimo2013/article/details/17636105

1.PopupWindow常用方法

public void method() {
popupWindow.dismiss();//popupWindow消失
popupWindow.getContentView();//得到popupWindow设置的view
popupWindow.isShowing();//是否显示
popupWindow.setAnimationStyle(animationStyle)//设置动画样式
popupWindow.showAsDropDown(anchorView);
popupWindow.showAsDropDown(anchorView, xoff, yoff);
popupWindow.showAtLocation(parent, gravity, x, y);
}

2.简单PopupWindow实例

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

view = getLayoutInflater().inflate(R.layout.item, null);
popupWindow = new PopupWindow(view, LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
}

/** 显示在View v的下方 */
public void show1(View v) {
if (popupWindow.isShowing()) {
popupWindow.dismiss();
} else {
popupWindow.showAsDropDown(v);
}
}

3.自定义动画



<!-- popupWindow样式 -->
<style name="style_popup_anim">
<item name="android:windowEnterAnimation">@anim/show</item>
<item name="android:windowExitAnimation">@anim/hide</item>
</style>
<?xml version="1.0" encoding="utf-8"?>
<!-- res/anim/show.xml -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<scale
android:duration="100"
android:fillAfter="false"
android:fromXScale="1.0"
android:fromYScale="0.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:pivotY="0%"
android:toXScale="1.0"
android:toYScale="1.0" />

</set>
<?xml version="1.0" encoding="utf-8"?>
<!-- res/anim/hide.xml -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<scale
android:duration="200"
android:fillAfter="false"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:pivotY="0%"
android:toXScale="1.0"
android:toYScale="0.0" />

</set>
popupWindow.setAnimationStyle(R.style.style_popup_anim);
popupWindow.showAsDropDown(v);


4.PopupWindow置于控件上方



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

view = getLayoutInflater().inflate(R.layout.item, null);
popupWindow = new PopupWindow(view, LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
// 计算view的宽高
getWidthAndHeight(view);
}

@Override
public void onClick(View v) {
if (popupWindow.isShowing()) {
popupWindow.dismiss();
} else {
//设置动画样式,自下往上
popupWindow.setAnimationStyle(R.style.style_popup_anim);
//得到view 左上点在屏幕中的位置
int[] location = new int[2];
v.getLocationOnScreen(location);
popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0],
location[1] - height);
}
}

// 得到View宽高
public void getWidthAndHeight(View view) {
int widthSpec = View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED);
int heightSpec = View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED);
view.measure(widthSpec, heightSpec);
width = view.getMeasuredWidth();
height = view.getMeasuredHeight();
}
<?xml version="1.0" encoding="utf-8"?>
<!-- res/anim/hide.xml -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<scale
android:duration="200"
android:fillAfter="false"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:pivotY="100%"
android:toXScale="1.0"
android:toYScale="0.0" />

</set>
<?xml version="1.0" encoding="utf-8"?>
<!-- res/anim/show.xml -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<scale
android:duration="100"
android:fillAfter="false"
android:fromXScale="1.0"
android:fromYScale="0.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:pivotY="100%"
android:toXScale="1.0"
android:toYScale="1.0" />

</set>

5.自动消失

popupWindow = new PopupWindow(view, LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);

或者在Activity的onTouch()方法中处理

public boolean onTouchEvent(MotionEvent event) {

if (popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
popupWindow = null;
}
return super.onTouchEvent(event);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: