- 摘要:要实现的效果图:方法一: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 PopupWindow居中显示
- Android AlertDialog 里弹Popwindow数据显示不全问题
- android listView多层嵌套listView显示不全问题
- Android在ListView显示图片(重复混乱闪烁问题)
- Android开发:Android Studio 导入模块依赖后显示不出来
- Android 自定义 ListView 显示网络上 JSON 格式歌曲列表
- android打开flutter项目显示dart SDK is not configured
- Android中显示Html内容(总结)
- 关于Android应用开发中,应用显示名称的问题
- Android 显示view的粒子爆炸/绽放效果