Android PopupWindow居中显示

  • 摘要:要实现的效果图:方法一:showAsDropDownprivatevoidshowChoose(){DisplayMetricsdm=newDisplayMetrics();getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);intscreenHeight=dm.heightPixels;LayoutInflatermLayoutInflater=(LayoutInflater)getActi
  • 要实现的效果图:


    Android PopupWindow居中显示


    方法一: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()); 
    }

猜你喜欢

转载自blog.csdn.net/u014644594/article/details/80513239