自定义Dialog

最近写项目需要用到自定义的Dialog,我看网上的方法主要有两种,一种是继承Dialog自定义dialog的布局和them实现,不过这种方式设置完之后宽高总是不受控制自定义的布局不能完全显示;另一种方法是使用AlertDialog的window,在该窗口上设置自定义的布局,这种方式可以完全显示自定义的布局,不过也存在一些问题,在5.0以上的系统上会自动给AlertDialog和屏幕边缘一个边距,但是在5.0以下的系统中没有默认的边距所以显示上会有一些问题。
两种方式的问题都在于自定义Dialog宽高的显示问题,通过代码中的设置可以解决掉这个问题,所以使用这两种方式都能完成自定义Dialog的显示效果,我这里使用的是第二种AlertDialog的方法设置
1.自定义要显示的dialog的布局

<?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="match_parent"
    android:id="@+id/ll_layout"
    android:background="#fff"
    android:orientation="vertical"
    android:padding="20dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical">

        <ImageView
            android:id="@+id/iv_dialog_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop"
            android:src="@drawable/gm3_ic_dialog_info"
            android:visibility="gone" />

        <TextView
            android:id="@+id/tv_dialog_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:text="温馨提示:"
            android:textColor="#000"
            android:textSize="18sp" />

    </LinearLayout>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/tv_dialog_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:padding="3dp"
        android:textColor="#000"
        android:textSize="16sp" />
</ScrollView>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="right"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/btn_negative"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:background="#FFF"
            android:text="取消"
            android:textColor="#1cafa3"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/btn_positive"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#FFF"
            android:text="确定"
            android:textColor="#1cafa3"
            android:textSize="15sp" />


    </LinearLayout>

</LinearLayout>

2.使用AlertDialog的window代码中的设置,这里主要是通过WindowManager来设置dialog所以来的window的宽高(问题终于解决了)

  创建AlertDialog的对象
        dialog = new AlertDialog.Builder(context).create();
        dialog.show();
        dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialogInterface) {
                customClickListener.cancelClick();
            }
        });
        dialog.setCanceledOnTouchOutside(outCancel);
//        获取AlertDialog所依赖的window
        Window window = dialog.getWindow();
//        设置window的布局参数(不过即使设成match_parent也没有效果)
        window.setLayout(WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT);
//        为window设置自定义的布局
        window.setContentView(R.layout.kit_custom_dialog_layout);
        titleTV = (TextView) window.findViewById(R.id.tv_dialog_title);
        messageTV = (TextView) window.findViewById(R.id.tv_dialog_message);
        iconIV = (ImageView) window.findViewById(R.id.iv_dialog_icon);
//        通过windowmanager来动态设置window的布局参数(主要是在代码中设通这种方式来设置dialog的宽高)
        WindowManager manager = window.getWindowManager();
        Display d = manager.getDefaultDisplay();  //为获取屏幕宽、高
        android.view.WindowManager.LayoutParams p = dialog.getWindow().getAttributes();  //获取对话框当前的参数值
//        p.height = (int) (d.getHeight() * 0.3);   //高度设置为屏幕的0.3
        p.width = (int) (d.getWidth() * 0.8);    //宽度设置为屏幕的0.8
        dialog.getWindow().setAttributes(p);     //设置生效
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值