1. 程式人生 > >dialog設定最大高度佔比

dialog設定最大高度佔比

    /**
     * 在dialog.show()前呼叫此方法
     *
     * @param mView dialog要顯示的view
     */
    private void setDialogSize(final View mView) {
        mView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop,
                                       int oldRight, int oldBottom) {
                int heightNow = v.getHeight();//dialog當前的高度
                int widthNow = v.getWidth();//dialog當前的寬度
                int needWidth = (int) (getWindowManager().getDefaultDisplay().getWidth() * 0.7);//最小寬度為螢幕的0.7倍
                int needHeight = (int) (getWindowManager().getDefaultDisplay().getHeight() * 0.6);//最大高度為螢幕的0.6倍
                if (widthNow < needWidth || heightNow > needHeight) {
                    if (widthNow > needWidth) {
                        needWidth = FrameLayout.LayoutParams.WRAP_CONTENT;
                    }
                    if (heightNow < needHeight) {
                        needHeight = FrameLayout.LayoutParams.WRAP_CONTENT;
                    }
                    mView.setLayoutParams(new FrameLayout.LayoutParams(needWidth,
                            needHeight));
                }
            }
        });
    }