关于自定义Dialog那些事

转载请注明出处:http://blog.csdn.net/binbinqq86/article/details/74120001

在做开发的过程中,经常会碰到各种各样的dialog,想要采用系统原生的懒省劲一下,可惜UI设计的五花八门,而且都是IOS风格,没办法,我们只能自己去发挥了(虽然目前Material风格的系统对话框看上去还不错了,但是仍旧满足不了设计师那奇葩的大脑,哈哈~)。下面以我自己写的一个为例子来说明,其实懂了这一个,千变万化也都是小菜一碟,看图:

这里写图片描述

怎么样,感觉还不错吧~一起来看看怎么实现的:

  • 继承系统Dialog
  • 编写style主题
  • 自定义dialog布局

总共就这三步,无论UI怎么设计,都能轻易满足他们的口味。看代码,具体的注释都写在代码里:

package com.example.tianbin.mydialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

/**
 * Created by TianBin on 2017/6/30.
 */

public class MyDialog extends Dialog {
    private static final String TAG = "MyDialog";
    public MyDialog(@NonNull Context context) {
        this(context,R.style.MyDialog);
    }

    public MyDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
        init(context);
    }

    private void init(Context mContext){

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_dialog);
        setCanceledOnTouchOutside(false);
        setCancelable(true);
        findViewById(R.id.iv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        Window win = getWindow();

        WindowManager.LayoutParams lp = win.getAttributes();

//        lp.width = WindowManager.LayoutParams.MATCH_PARENT;

//        lp.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,300,getContext().getResources().getDisplayMetrics());

        //设置背景半透明程度
        lp.dimAmount = 0.2f;

        win.setAttributes(lp);
        Log.e(TAG, "onCreate: "+lp.height);
    }
}

可以看到,上面构造函数里面传入了自己需要的主题,现在一般都是小清新的样式,我们来看看这个主题:

<!--自定义dialog背景全透明无边框theme -->
    <style name="MyDialog" parent="android:style/Theme.Dialog">
        <!--背景颜色及和透明程度-->
        <item name="android:windowBackground">@android:color/transparent</item>
        <!--是否去除标题 -->
        <item name="android:windowNoTitle">true</item>
        <!--是否去除边框-->
        <item name="android:windowFrame">@null</item>
        <!--是否浮现在activity之上-->
        <item name="android:windowIsFloating">true</item>
        <!-- 是否半透明 -->
        <item name="android:windowIsTranslucent">true</item>
        <!-- 背景是否模糊显示 ,如果为false则半透明效果消失-->
        <item name="android:backgroundDimEnabled">true</item>
    </style>

具体的注释都写在里面了,下面来看看最后一步:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:paddingLeft="40dp"
        android:paddingRight="40dp"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@drawable/bg"
            android:gravity="center_horizontal"
            android:orientation="vertical"
            android:padding="16dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher" />

            <com.example.tianbin.mydialog.MyScrollView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="hd;kljasfhg;\n\n\n\\n\n\n\\n\n\n\n\n\n\n\n\\n\n\n\\n\n\n\n\n\n\n\\n\n\n\n\n\n\n\\n\n\n\n\n\n\n\\n\n\n\\n\n\n\\n\n\n\\n\n\n\\n\n\n\kgldfkgjfkldfjkdfkgjldfg"
                    />
            </com.example.tianbin.mydialog.MyScrollView>

        </LinearLayout>

        <ImageView
            android:id="@+id/iv"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_centerHorizontal="true"
            android:src="@android:drawable/ic_menu_close_clear_cancel" />
    </LinearLayout>

</RelativeLayout>

这就是具体的内容布局了,可以看到,里面采用了一个自定义的ScrollView,这个是用来自适应屏幕的,当文字过长的时候保证高度不超过屏幕三分之一,过短时不至于留下太多空白。整个的自定义dialog就这么多内容,其实很简单,当对话框改变时,只需要去修改自己的布局就可以了,分分钟应对千变万化的需求~

最后,奉上源码,欢迎star,欢迎fork,欢迎watch,谢谢大家的支持~

源码下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值