这篇文章主要围绕Android编程之PopupWindow隐藏及显示方法示例(showAtLocation,showAsDropDown)和androidpopupwindowshow展开,旨在为您提
这篇文章主要围绕Android编程之PopupWindow隐藏及显示方法示例(showAtLocation,showAsDropDown)和android popupwindow show展开,旨在为您提供一份详细的参考资料。我们将全面介绍Android编程之PopupWindow隐藏及显示方法示例(showAtLocation,showAsDropDown)的优缺点,解答android popupwindow show的相关问题,同时也会为您带来Android 7.0 PopupWindow 显示错位问题、Android Animation实战之屏幕底部弹出PopupWindow、android Dialog和Popupwindow,不显示、Android Nougat 7.1.1 showAtLocation(…)重力无法正常工作的实用方法。
本文目录一览:- Android编程之PopupWindow隐藏及显示方法示例(showAtLocation,showAsDropDown)(android popupwindow show)
- Android 7.0 PopupWindow 显示错位问题
- Android Animation实战之屏幕底部弹出PopupWindow
- android Dialog和Popupwindow,不显示
- Android Nougat 7.1.1 showAtLocation(…)重力无法正常工作
Android编程之PopupWindow隐藏及显示方法示例(showAtLocation,showAsDropDown)(android popupwindow show)
本文实例讲述了Android编程之PopupWindow隐藏及显示方法。分享给大家供大家参考,具体如下:
前面分析了PopupWindow的用法(位置、动画、焦点)下面说说PopupWindow的如何隐藏、显示及显示位置(showAtLocation/showAsDropDown)。
1、PopupWindow的隐藏
@H_301_8@final PopupWindow window = mPageStatWin; if(null != window && window.isShowing()) { win.dismiss(); }
Android 7.0 PopupWindow 显示错位问题
这是系统级 bug, 需要我们自定义 PopupWindow
public class PopView extends PopupWindow {
public PopView(Context context) {
super(context);
}
public PopView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public PopView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public PopView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public PopView() {
}
public PopView(View contentView) {
super(contentView);
}
public PopView(int width, int height) {
super(width, height);
}
public PopView(View contentView, int width, int height) {
super(contentView, width, height);
}
public PopView(View contentView, int width, int height, boolean focusable) {
super(contentView, width, height, focusable);
}
@Override
public void showAsDropDown(View anchor) {
if(Build.VERSION.SDK_INT >= 24) {
Rect rect = new Rect();
anchor.getGlobalVisibleRect(rect);
int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
setHeight(h);
}
super.showAsDropDown(anchor);
}
}
Android Animation实战之屏幕底部弹出PopupWindow
Android动画的一个实战内容,从屏幕底部滑动弹出PopupWindow。 相信这种效果大家在很多APP上都遇到过,比如需要拍照或者从SD卡选择图片,再比如需要分享某些东西时,大多会采用这么一种效果:
那这种效果如何实现呢?
我们仿写一个这种效果的实例吧:
1)我们首先定义一下,弹出窗口的页面布局组件:take_photo_pop.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="vertical"> <LinearLayout android:id="@+id/pop_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ffffff" android:layout_alignParentBottom="true" android:gravity="center_horizontal" android:orientation="vertical"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:clickable="false" android:gravity="center" android:text="修改头像" android:textColor="#8a8a8a" android:textSize="15sp" /> <View android:layout_width="fill_parent" android:layout_height="0.1dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:background="#00c7c0" /> <Button android:id="@+id/btn_take_photo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="拍照" android:textColor="#0e61aa" android:textSize="18sp" /> <Button android:id="@+id/btn_pick_photo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="从相册选择" android:textColor="#0e61aa" android:textSize="18sp" /> <Button android:id="@+id/btn_cancel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="15dip" android:layout_marginTop="15dip" android:text="取消" android:textColor="#0e61aa" android:textSize="18sp" android:text/> </LinearLayout> </RelativeLayout>
2)现在定义动画,要知道该Popupwindow出现时是从页面底部向上滑动,消失时是从上向下滑动消失,,所以我们需要定义两个动画文件:
退出动画pop_exit_anim.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="200" android:fromYDelta="0" android:toYDelta="50%p" /> <alpha android:duration="200" android:fromAlpha="1.0" android:toAlpha="0.0" /> </set> 显示动画pop_enter_anim.xml <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="200" android:fromYDelta="100%p" android:toYDelta="0" /> <alpha android:duration="200" android:fromAlpha="0.0" android:toAlpha="1.0" /> </set>
关于这两个动画,此处不再多做解析,读过我之前博文的都应该知道啦,很简单的,若是看不懂?请点击此文上方的链接学习之。
3)自定义弹出框Popupwindow:
import android.content.Context; import android.graphics.drawable.ColorDrawable; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.PopupWindow; import android.widget.RelativeLayout; public class TakePhotoPopWin extends PopupWindow { private Context mContext; private View view; private Button btn_take_photo,btn_pick_photo,btn_cancel; public TakePhotoPopWin(Context mContext,View.OnClickListener itemsOnClick) { this.view = LayoutInflater.from(mContext).inflate(R.layout.take_photo_pop,null); btn_take_photo = (Button) view.findViewById(R.id.btn_take_photo); btn_pick_photo = (Button) view.findViewById(R.id.btn_pick_photo); btn_cancel = (Button) view.findViewById(R.id.btn_cancel); // 取消按钮 btn_cancel.setonClickListener(new View.OnClickListener() { public void onClick(View v) { // 销毁弹出框 dismiss(); } }); // 设置按钮监听 btn_pick_photo.setonClickListener(itemsOnClick); btn_take_photo.setonClickListener(itemsOnClick); // 设置外部可点击 this.setoutsidetouchable(true); // mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框 this.view.setonTouchListener(new View.OnTouchListener() { public boolean onTouch(View v,MotionEvent event) { int height = view.findViewById(R.id.pop_layout).getTop(); int y = (int) event.getY(); if (event.getAction() == MotionEvent.ACTION_UP) { if (y < height) { dismiss(); } } return true; } }); /* 设置弹出窗口特征 */ // 设置视图 this.setContentView(this.view); // 设置弹出窗体的宽和高 this.setHeight(RelativeLayout.LayoutParams.MATCH_PARENT); this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT); // 设置弹出窗体可点击 this.setFocusable(true); // 实例化一个ColorDrawable颜色为半透明 ColorDrawable dw = new ColorDrawable(0xb0000000); // 设置弹出窗体的背景 this.setBackgroundDrawable(dw); // 设置弹出窗体显示时的动画,从底部向上弹出 this.setAnimationStyle(R.style.take_photo_anim); } }
定义要弹出的组件TakePhotoPopWin,它继承自PopupWindow,具体如何实现的,我备注信息很详细了。 有一个地方要提醒的是,就是最后要设置弹出窗体的显示动画,this.setAnimationStyle(R.style.take_photo_anim); 这是必不可少的,只有加上了它,才能应用动画效果!
看下take_photo_anim style的定义:
<style name="take_photo_anim" parent="android:Animation"> <item name="android:windowEnteranimation">@anim/pop_enter_anim</item> <item name="android:windowExitAnimation">@anim/pop_exit_anim</item> </style>
就这么几步,一个可以从屏幕底部滑动弹出的组件
public void showPopFormBottom(View view) { TakePhotoPopWin takePhotoPopWin = new TakePhotoPopWin(this,onClickListener); //showAtLocation(View parent,int gravity,int x,int y) takePhotoPopWin.showAtLocation(findViewById(R.id.main_view),Gravity.CENTER,0); } private View.OnClickListener onClickListener = new View.OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_take_photo: System.out.println("btn_take_photo"); break; case R.id.btn_pick_photo: System.out.println("btn_pick_photo"); break; } } };
这下子,效果就和我一开始传的图一致啦!有木有学会了呢!?
拓展:
玩过APP的大家都知道,在你进入新页面或者注册登录啥的时候,都会弹出一个等待的框框,表示网络请求中,你需要耐心等待下,比如微信的等待请求框效果如下:
这里面其中也有个地方用到了动画,那就是不停旋转的那个小图标,它其实用的就是旋转动画!
关于如何实现这么样一个旋转等待框,我以前写过一篇介绍的文章,可查看: 《Android自定义ProgressDialog进度等待框》
android Dialog和Popupwindow,不显示
测试的是华为荣耀平板,在BaseActivity中有个方法,在里面构造dialog或popupwindow,在子类activity中手动点击按钮调用该方法,每次都正常显示弹窗,但是在回调接口中调用基本就不显示,偶尔运气好能显示出来,打印看构造弹窗的context都一样。求大神解答!!!
Android Nougat 7.1.1 showAtLocation(…)重力无法正常工作
这与以下问题有关:
Android Nougat PopupWindow showAsDropDown(…) Gravity not working
但是,当我应用此修复程序时:
if (android.os.Build.VERSION.SDK_INT >=24) {
int[] a = new int[2];
anchorView.getLocationInWindow(a);
popUp.showAtLocation(((Activity) mContext).getwindow().getDecorView(), Gravity.NO_GraviTY, 0 , a[1]+anchorView.getHeight());
} else{
popUp.showAsDropDown(anchorView);
}
它在Android Nougat 7.1.1上不起作用.特别是在Google Pixel和Nexus 6p设备上.
有人对此有解决办法吗?请分享.
https://code.google.com/p/android/issues/detail?id=231487
解决方法:
当我将PopupWindow的高度从WindowManager.LayoutParams.MATCH_PARENT更改为WindowManager.LayoutParams.WRAP_CONTENT时,它可以在Android 7.1上运行,我不知道原因,但也许您可以尝试一下.
另外,您需要将代码更改为:
if (android.os.Build.VERSION.SDK_INT == 24) {
int[] a = new int[2];
anchorView.getLocationInWindow(a);
popUp.showAtLocation(((Activity)mContext).getwindow().getDecorView(), Gravity.NO_GraviTY, 0 , a[1]+anchorView.getHeight());
} else{
popUp.showAsDropDown(anchorView);
}
关于Android编程之PopupWindow隐藏及显示方法示例(showAtLocation,showAsDropDown)和android popupwindow show的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于Android 7.0 PopupWindow 显示错位问题、Android Animation实战之屏幕底部弹出PopupWindow、android Dialog和Popupwindow,不显示、Android Nougat 7.1.1 showAtLocation(…)重力无法正常工作等相关内容,可以在本站寻找。
本文标签: