Android_PopupWindow使用介绍
2013-12-29 09:11
429 查看
本博文为子墨原创,转载请注明出处!
http://blog.csdn.net/zimo2013/article/details/17636105
或者在Activity的onTouch()方法中处理
public boolean onTouchEvent(MotionEvent event) {
if (popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
popupWindow = null;
}
return super.onTouchEvent(event);
}
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);
}
相关文章推荐
- Android popupwindow简单使用方法介绍
- Android Fragments 详细使用详细介绍
- Android PopupWindow的使用和分析
- android 模拟器的使用(Android模拟器介绍及创建)
- Android Paint的使用以及方法介绍(附源码下载)
- Android 中 PopupWindow使用
- Fragment的使用简单介绍【Android】
- android AsyncTask使用介绍。
- android教程之使用popupwindow创建菜单示例
- android popupWindow使用
- android中dumpsys函数介绍与使用
- android 开发 ExpandableListView的使用介绍
- Android控件系列之Button以及Android监听器使用介绍
- 【Android UI设计与开发】第07期:底部菜单栏(二)Fragment的详细介绍和使用方法
- Android PopupWindow使用之地区、学校选择二级联动
- android学习——MeasureSpec介绍及使用
- Android_RecyclerView的介绍与使用
- ym——Android之常用Tools【介绍及使用】
- Android中logcat的介绍和使用
- AndroidStudio中使用AndroidAnnotation的简单介绍