- 摘要:要实现的效果图:方法一:showAsDropDownprivatevoidshowChoose(){DisplayMetricsdm=newDisplayMetrics();getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);intscreenHeight=dm.heightPixels;LayoutInflatermLayoutInflater=(LayoutInflater)getActi
-
要实现的效果图:
方法一:showAsDropDown
private void showChoose() {
DisplayMetrics dm = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenHeight =dm.heightPixels;
LayoutInflater mLayoutInflater = (LayoutInflater) getActivity().getSystemService(LAYOUT_INFLATER_SERVICE);
ViewGroup menuView = (ViewGroup) mLayoutInflater.inflate(
R.layout.view_choose_photo_video, null, true);
PopupWindow pw = new PopupWindow(menuView, ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT, true);
pw.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
pw.setOutsideTouchable(false); // 设置是否允许在外点击使其消失,到底有用没?
pw.setAnimationStyle(R.style.nornal_style); // 设置动画
// 将pixels转为dip
int xoffInDip = ScreenTools.getInstance().pxTodip(screenHeight);
FrameLayout frameLayout = findView(R.id.nav_title_bar);
pw.showAsDropDown(frameLayout, 0, xoffInDip);
//添加pop窗口关闭事件
pw.setOnDismissListener(new poponDismissListener());
backgroundAlpha(0.5f);
}整个窗体设置透明度方法
public void backgroundAlpha(float bgAlpha) {
WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
lp.alpha = bgAlpha; //0.0-1.0
getActivity().getWindow().setAttributes(lp);
}如果对屏幕像素与dp不太清楚者,查看https://my.oschina.net/yuerliang/blog/796468
像素转换成dp实现类
public class ScreenTools {
private static ScreenTools mScreenTools;
private Context mContext;
private ScreenTools(Context mContext) {
this.mContext = mContext;
}
public static ScreenTools getInstance() {
if (mScreenTools == null) {
mScreenTools = new ScreenTools(ControlApplication.context());
}
return mScreenTools;
}
/**
* 根据手机的分辨率从 px(像素) 的单位 转成为 dp
*/
public int pxTodip(float pxValue) {
final float scale = mContext.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
}方法二:showAtLocation
private void showChoose() {
LayoutInflater mLayoutInflater = (LayoutInflater) getActivity().getSystemService(LAYOUT_INFLATER_SERVICE);
ViewGroup menuView = (ViewGroup) mLayoutInflater.inflate(
R.layout.view_choose_photo_video, null, true);
PopupWindow pw = new PopupWindow(menuView, ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT, true);
pw.setOutsideTouchable(true); // 设置是否允许在外点击使其消失,到底有用没?
pw.setFocusable(true);
pw.setAnimationStyle(R.style.nornal_style); // 设置动画
// 计算x轴方向的偏移量,使得PopupWindow在Title的正下方显示,此处的单位是pixels
backgroundAlpha(0.5f);
FrameLayout frameLayout = findView(R.id.fragment_circle);
//方法二
pw.showAtLocation(frameLayout, Gravity.CENTER,0,0);
//添加pop窗口关闭事件监听
pw.setOnDismissListener(new poponDismissListener());
}
相关文章
- android的充电图标显示
- Android ListView 显示多种数据类型
- Android进阶笔记10:ListView篇之ListView显示多种类型的条目(item)
- android问题之 mount时显示:mount: Permission denied
- Android编程中常用的PopupWindow和Dialog对话框
- Android 10.0 状态栏系统图标显示分析
- Android Animation动画实战(二):从屏幕底部弹出PopupWindow
- android 二维码制作,显示到UI,并保存SD卡,拿来就能用!!
- 【CSS】定位元素居中显示
- 仿微信的IM聊天时间显示格式(含iOS/Android/Web实现)[图文+源码]