1. 程式人生 > >第三方開源庫:Dialog相關:DialogPlus + SweetAlertDialog +Alerter

第三方開源庫:Dialog相關:DialogPlus + SweetAlertDialog +Alerter

DialogPlus

效果圖:

github原圖:
這裡寫圖片描述

demo效果圖:
這裡寫圖片描述 這裡寫圖片描述

簡介

總有3個Holder:ViewHolder、ListHolder、GridHolder,其中ViewHolder是用於自定義檢視,ListHolder用於ListView,GridHolder用於GridView

注意:setExpanded(true)僅適用於ListHolder、GridHolder

gradle

compile 'com.orhanobut:dialogplus:1.11@aar'

使用

DialogPlus dialog = DialogPlus.newDialog
(this) .setContentHolder(new ViewHolder(R.layout.dialog1)) .setContentHeight(ViewGroup.LayoutParams.WRAP_CONTENT) .setContentWidth(ViewGroup.LayoutParams.MATCH_PARENT) .setHeader(R.layout.header) .setFooter(R.layout.footer) .setAdapter(adapter) .setOnItemClickListener(this) .setGravity
(Gravity.BOTTOM) .setExpanded(true) .create(); dialog.show();

屬性+方法

方法 說明
newDialog(Context context) 建立dialog
setContentHolder(Holder holder) 設定holder,必要
setContentWidth(int width) 寬:ViewGroup.LayoutParams.WRAP_CONTENT等
setContentHeight(int height)
setHeader(int resourceId) 頭的佈局或View
setFooter(int resourceId) 尾的佈局或View
setGravity(int gravity) dialog的位置
setExpanded(boolean expanded) 是否可擴充套件,預設是false,僅適用於ListView和GridView
setCancelable(boolean isCancelable) 點選外部區域是否可以取消dialog
setAdapter(BaseAdapter adapter) ListView或GridView的adapter,ViewHolder不需要
setOnItemClickListener(OnItemClickListener listener) ListView或GridView的item的點選事件
setOnClickListener(OnClickListener listener) 點選事件
setOnDismissListener(OnDismissListener listener) dismiss的監聽
setOnCancelListener(OnCancelListener listener) 取消的監聽
getHolderView() 獲取檢視View
getHeaderView() 獲取頭佈局
getFooterView() 獲取尾佈局
setMargin(left, top, right, bottom) Add margins to your dialog. They are set to 0 except when gravity is center. In that case basic margins are applied
setPadding(left, top, right, bottom) Set padding to the holder
setInAnimation(R.anim.abc_fade_in) 進入動畫
setOutAnimation(R.anim.abc_fade_out) 移除動畫
setContentBackgroundResource(resource) dialog的背景色
setOverlayBackgroundResource(resource) dialog意外的背景色

原始碼

SweetAlertDialog

效果圖

這裡寫圖片描述

gradle

repositories {
    mavenCentral()
}

dependencies {
    compile 'cn.pedant.sweetalert:library:1.3'
}

注意:直接這樣新增依賴會報錯:
這裡寫圖片描述

解決方法:

在AndroidManifest.xml中新增
xmlns:tools="http://schemas.android.com/tools"
tools:replace="android:icon"

這裡寫圖片描述

基本使用

SweetAlertDialog pDialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE);
pDialog.getProgressHelper().setBarColor(Color.parseColor("#A5DC86"));
pDialog.setTitleText("Loading");
pDialog.setCancelable(false);
pDialog.show();

NORMAL_TYPE

new SweetAlertDialog(MainActivity.this, SweetAlertDialog.NORMAL_TYPE);

這裡寫圖片描述

PROGRESS_TYPE

new SweetAlertDialog(MainActivity.this, SweetAlertDialog.PROGRESS_TYPE);

這裡寫圖片描述

SUCCESS_TYPE

new SweetAlertDialog(MainActivity.this, SweetAlertDialog.SUCCESS_TYPE);

這裡寫圖片描述

WARNING_TYPE

new SweetAlertDialog(MainActivity.this, SweetAlertDialog.WARNING_TYPE);

這裡寫圖片描述

ERROR_TYPE

new SweetAlertDialog(MainActivity.this, SweetAlertDialog.ERROR_TYPE);

這裡寫圖片描述

CUSTOM_IMAGE_TYPE

new SweetAlertDialog(MainActivity.this, SweetAlertDialog.CUSTOM_IMAGE_TYPE);

這裡寫圖片描述

其他常用方法

dialog.getProgressHelper().setBarColor(R.color.colorPrimary);
dialog.setTitleText("title")
        .setContentText("ContentText")
        .setConfirmText("確定")
        .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
            @Override
            public void onClick(SweetAlertDialog sweetAlertDialog) {
                 dialog.dismissWithAnimation();
            }
        })
        .showCancelButton(true)
        .setCancelText("取消")
        .setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
            @Override
            public void onClick(SweetAlertDialog sweetAlertDialog) {
                dialog.dismissWithAnimation();
            }
        });
dialog.show();

點選確認後切換到對話方塊樣式:

.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
    @Override
    public void onClick(SweetAlertDialog sDialog) {
        sDialog
            .setTitleText("Deleted!")
            .setContentText("Your imaginary file has been deleted!")
            .setConfirmText("OK")
            .setConfirmClickListener(null)
            .changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
    }
})

原始碼

Alerter

頂部彈出的View,嚴格來說跟Dialogz這個類無關,只是UI效果相似,可以設定icon,但是icon都是白色的。API>=16
github:https://github.com/Tapadoo/Alerter

效果圖

這裡寫圖片描述

使用

Activity中使用Alerter.create(MainActivity.this),在Fragment中使用Alerter.create(getActivity)

Alerter.create(MainActivity.this)
        .setTitle("Title")
        .setText("message")
        .setBackgroundColor(R.color.colorAccent)
        .setIcon(R.mipmap.ic_launcher_round)
        .setDuration(3000)
        .setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "click", Toast.LENGTH_SHORT).show();
            }
        })
        .show();

原始碼