自定义AlertDialog指定布局大小和设置全屏

前言:

日常开发过程中经常使用dialog弹框,如果用的比较多的话,大家可以通过继承Dialog来封装一个CustomDialog的工具类。如果用的比较少的话,大家可以采用自定义AlertDialog的方式来实现弹框。

在使用自定义AlertDialog的过程中,大家可能会遇到一下问题:
1.自定义布局不能够指定大小。
2.自定义布局不能够全屏。

针对者两种问题该怎么解决?

Step 1:自定义布局不能够指定大小
代码如下:

 /**
     * 弹出AlertDialog
     */
    private void createAlertDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View v = LayoutInflater.from(this).inflate(R.layout.dialog_recharge, null);
        final Dialog dialog = builder.create();
        dialog.show();

        Window window = dialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();

        lp.width = DensityUtil.dp2px(this, 200);
        lp.height = DensityUtil.dp2px(this, 200);
        window.setGravity(Gravity.CENTER);
        window.setAttributes(lp);
        window.setContentView(v);
    }

其中DensityUtil是我封装的一个dp—->px相互装换的工具类。网上一堆。如果想要的话,我直接贴出来:

/**
 * 常用单位转换的辅助类
 */
public class DensityUtil {

    private DensityUtil() {
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    /**
     * dp转px
     */
    public static int dp2px(Context context, float dpVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dpVal, context.getResources().getDisplayMetrics());
    }

    /**
     * sp转px
     */
    public static int sp2px(Context context, float spVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
                spVal, context.getResources().getDisplayMetrics());
    }

    /**
     * px转dp
     */
    public static float px2dp(Context context, float pxVal) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (pxVal / scale);
    }

    /**
     * px转sp
     */
    public static float px2sp(Context context, float pxVal) {
        return (pxVal / context.getResources().getDisplayMetrics().scaledDensity);
    }
}

Step 1:自定义布局不能够全屏

针对此种情况,除了将自定义view的宽高设定为屏幕宽高以外,还需要指定AlertDialog的Style样式。

代码如下

 /**
     * 弹出AlertDialog
     */
    private void createAlertDialog1() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this,R.style.Dialog);//指定stley样式
        View v = LayoutInflater.from(this).inflate(R.layout.dialog_recharge, null);
        final Dialog dialog = builder.create();
        dialog.show();


        Window window = dialog.getWindow();
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();

        WindowManager windowManager = getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        lp.width = display.getWidth();
        lp.height=display.getHeight();

        window.setAttributes(lp);
        window.setContentView(v);
    }

贴出Dilaog的style样式:

<style name="Dialog">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:backgroundDimEnabled">true</item>
    </style>

好了,至此完结,小伙伴有问题的话可以留言。

猜你喜欢

转载自blog.csdn.net/zhangqunshuai/article/details/81905874