自定义dialog

很简单,只需要进行四步


第一步:自己画出想要弹出的界面,即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:gravity="center_horizontal"
    android:background="@drawable/my_dialog_background"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="提示"
        android:gravity="center"
        android:textStyle="bold"
        android:textSize="18sp"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="0.1dp"
        android:background="@color/gray"/>

    <TextView
        android:id="@+id/dialog_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="您确定要退出么?"
        android:gravity="center"
        android:layout_margin="20dp"
        android:textSize="15sp"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="0.1dp"
        android:background="@color/gray"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/dialog_below_left_background"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/dialog_cancel"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:text="取消"
            android:textColor="@color/colorWhite"
            android:gravity="center"
            android:textSize="18sp"
            android:layout_height="50dp" />
        <View
            android:layout_width="0.1dp"
            android:layout_height="match_parent"
            android:background="@color/gray"/>
        <TextView
            android:id="@+id/dialog_comfire"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:text="确定"
            android:textColor="@color/colorWhite"
            android:gravity="center"
            android:textSize="18sp"
            android:layout_height="50dp" />
    </LinearLayout>

</LinearLayout>

第二步:定义背景样式:即shape的xml文件

其中我上面引用了两个,下面我只展示一个的代码,另一个一样,根据自己的需求进行自己编写

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!--<!–solid 背景–>-->
    <solid android:color="#fff"></solid>
    <!--<!–radius 边框角度–>-->
    <corners android:radius="40px"></corners>
    <!--<solid android:color="@color/colorWhite"></solid>-->
    <!--stroke 边框宽度 及颜色-->
    <stroke android:color="#b6b2b2" android:width="1px"></stroke>
    <!--间距-->
    <!--<padding
        android:left="0dp"
        android:top="0dp"
        android:right="0dp"
        android:bottom="0dp" />-->
</shape>

第三部,将布局layout设置到dialog中

其中一定要注意设置一个style个dialog

<span style="white-space:pre">	</span>View dialogView = null;
<span style="white-space:pre">	</span>AlertDialog.Builder builder = new AlertDialog.Builder(context,R.style.Dialog_FS);
<span style="white-space:pre">	</span>dialogView = LayoutInflater.from(context).inflate(R.layout.ok_cancle_dialog, null);<pre name="code" class="java">        /*为dialog设置View*/
        builder.setView(dialogView);
        dialog = builder.create();

        /*显示对话框*/
        dialog.show();

 


第四部:定义一个style

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






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值