关于actionbar中overflow menu(溢出菜单)中的一些问题

/** * 弹出自定义溢出菜单 */public void popupOverflowMenu() {// 显示溢出菜单的时候设置背景半透明setWindowAlpha(0.5f); // 获取状态栏高度Rect frame = new Rect();getActivity().getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);// 状态栏高度 frame.topint xOffset = frame.top + getActivity().getActionBar().getHeight() – 25; // 减去阴影宽度,适配UIint yOffset = Dp2Dx(getActivity(), 8f); // 设置x方向offset为5dpView parentView = getActivity().getLayoutInflater().inflate(R.layout.fragment_portfolio, null);View popView = getActivity().getLayoutInflater().inflate(R.layout.action_overflow_menu, null);// popView即popupWindow布局PopupWindow popupWindow = new PopupWindow(popView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);// 必须设计BackgroundDrawable后setOutsideTouchable(true)才会有效。这里在XML中定义背景,所以这里为nullpopupWindow.setBackgroundDrawable(new ColorDrawable(0000000000));popupWindow.setFocusable(true);popupWindow.setOutsideTouchable(true); // 点击外部关闭popupWindow.setAnimationStyle(android.R.style.Animation_Dialog);// 设置Gravity,让它显示在右上角popupWindow.showAtLocation(parentView, Gravity.RIGHT | Gravity.TOP, yOffset, xOffset);popupWindow.setOnDismissListener(new OnDismissListener() {@Overridepublic void onDismiss() {// popupWindow消失时,设置为全透明setWindowAlpha(1f);}});}/** * 设置屏幕透明度 * @param alpha */private void setWindowAlpha(float alpha) {WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();lp.alpha = alpha; //0.0-1.0getActivity().getWindow().setAttributes(lp);}public int Dp2Dx(Context context, float dp) {final float scale = context.getResources().getDisplayMetrics().density;return (int) (dp * scale + 0.5f);}这里就是通过自定义pupupwindow,,指定popupwindow的xml布局,这个自己来定,根据父布局来显示popupwindow的位置,当popupwindow显示的时候,设置屏幕为半透明,监听它的消失事件,消失的时候,设置屏幕为全透明效果。最终实现适配魅族手机,这里是小巫想到的一个解决方案,有更好的方法,可以留言交流一下。

积极思考造成积极人生,消极思考造成消极人生。

关于actionbar中overflow menu(溢出菜单)中的一些问题

相关文章:

你感兴趣的文章:

标签云: