自定义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>

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

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是一个简单的自定义AlertDialog布局示例,其中包含一个标题、一个消息、两个按钮: ```xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="16dp"> <TextView android:id="@+id/alert_dialog_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是自定义AlertDialog" android:textSize="18sp" android:textColor="@color/colorPrimary"/> <TextView android:id="@+id/alert_dialog_message" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:text="这是自定义AlertDialog的消息内容" android:textSize="16sp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="16dp"> <Button android:id="@+id/alert_dialog_cancel" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="取消"/> <Button android:id="@+id/alert_dialog_confirm" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="确定"/> </LinearLayout> </LinearLayout> ``` 这个布局包含一个LinearLayout,其中包含一个标题和一个消息的TextView,以及两个按钮的LinearLayout。注意,按钮使用了layout_weight属性,让它们均分父布局宽度。在代码中,你可以通过findViewById()方法找到这些控件,设置它们的文本、图标、事件监听器等。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值