Android仿Windows8点击效果,掌握Popupwindow实现弹出列表效果及自定义效果
1、PopupWindow与AlertDialog的区别PopupWindow和AlertDialog很相似,都是一个悬浮的窗口遮挡在当前页面,但是它们是有区别的,最关键的区别是AlertDialog不能指定显示位置,只能默认显示在屏幕最中间(当然也可以通过设置WindowManager参数来改变位置)。而PopupWindow是可以指定显示位置的,随便哪个位置都可以,更加灵活。2.显示函数ima
1、PopupWindow与AlertDialog的区别
PopupWindow和AlertDialog很相似,都是一个悬浮的窗口遮挡在当前页面,但是它们是有区别的,最关键的区别是AlertDialog不能指定显示位置,只能默认显示在屏幕最中间(当然也可以通过设置WindowManager参数来改变位置)。而PopupWindow是可以指定显示位置的,随便哪个位置都可以,更加灵活。
2.显示函数
image.png
3.设置点击外部消失PopupWindow
//必须先设置背景
popupWindow.setBackgroundDrawable(null);
popupWindow.setOutsideTouchable(true);
4.为菜单添加阴影(了解)
View inflate = LayoutInflater.from(this).inflate(R.layout.pop, null, false);
final PopupWindow popupWindow = new PopupWindow(inflate, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
inflate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
//popupWindow.setBackgroundDrawable(null);
popupWindow.setOutsideTouchable(true);
WindowManager.LayoutParams attributes = getWindow().getAttributes();
attributes.alpha = 0.3f;
getWindow().addFlags(WindowManager.LayoutParams.FLAGS_CHANGED);
getWindow().setAttributes(attributes);
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams attributes = getWindow().getAttributes();
attributes.alpha = 1.0f;
getWindow().addFlags(WindowManager.LayoutParams.FLAGS_CHANGED);
getWindow().setAttributes(attributes);
}
});
//在某个view的下方,如果有偏移量,在某个view下方开始加减对应偏移量即popwindow的位置 popupWindow.setAnimationStyle(R.style.PopAnimation);
popupWindow.showAsDropDown(inflate,Gravity.CENTER,0,0);
5.加动画
popupWindow.setAnimationStyle(R.style.PopAnimation);
@anim/anim_eixt
@anim/anim_enter
anim_exit.xml:
android:duration="1500"
>
android:fromXDelta="0"
android:toXDelta="100%p"
android:fromYDelta="0"
android:toYDelta="100%p"
/>
android:fromAlpha="1.0"
android:toAlpha="0"
/>
anim_enter.xml
android:duration="1500"
>
android:fromXDelta="100%p"
android:toXDelta="0"
android:fromYDelta="100%p"
android:toYDelta="0"
/>
android:fromAlpha="0"
android:toAlpha="1.0"
/>
6.又想有阴影,又想在某个随着屏幕滑动位置变化的某个view下面
View inflate = LayoutInflater.from(this).inflate(R.layout.pop, null, false);
final PopupWindow popupWindow = new PopupWindow(inflate, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
//点击非菜单部分退出
inflate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
popupWindow.setBackgroundDrawable(null);
popupWindow.setOutsideTouchable(true);
popupWindow.setAnimationStyle(R.style.PopAnimation);
//相对于屏幕的位置
int[] position = new int[2];
mBtn2.getLocationOnScreen(position);
LinearLayout llCotainer = inflate.findViewById(R.id.ll_container);
//获取状态栏高度
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
LinearLayout.LayoutParams par = (LinearLayout.LayoutParams) llCotainer.getLayoutParams();
par.setMargins(position[0],position[1]+mBtn2.getHeight()-result,0,0);
popupWindow.showAtLocation(mBtn2, Gravity.NO_GRAVITY,0,0);
更多推荐
所有评论(0)