PopupWindow的wrap_content問題

在使用popupWindow的時候發(fā)現(xiàn)一個問題:

mPopupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);

由于顯示的頁面的長寬是不確定的,所以只能使用wrap_content。但是在使用wrap_content時候會出現(xiàn)顯示不完整的問題,可見其在measure的時候會出現(xiàn)問題。

一開始的時候,設(shè)想通過content_view的getMeasureWidth(),getMeasuredHeight()獲取長寬,發(fā)現(xiàn)獲取到的為0,原因是在獲取長寬的時候,content_view的measure還未完成。

解決方法:先進行measure再次獲取getMeasureWidth()

比較完整代碼:

public class MyPopupView {
    private Context mContext;

    private PopupWindow mPopupWindow;

    private View mRootView;

    private boolean isShowing;

    public MyPopupView(View rootView, Context mContext){
        this.mContext = mContext;
        this.mRootView = rootView;
        this.isShowing = false;
    }

    public void showView(){
        if(isShowing == true)
            return;
        if (mPopupWindow == null){
            View contentView = LayoutInflater.from(mContext).inflate(R.layout.popup_view,null);
            contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);

            mPopupWindow = new PopupWindow(contentView, contentView.getMeasuredWidth(), ViewGroup.LayoutParams.WRAP_CONTENT);
//            mPopupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
            mPopupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
            mPopupWindow.setFocusable(true);
        }
        isShowing = true;
        mPopupWindow.showAsDropDown(mRootView);//以contentview為參照系
//      或者  mPopupWindow.showAtLocation();
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容