第三方開源庫:Dialog相關:DialogPlus + SweetAlertDialog +Alerter
阿新 • • 發佈:2019-01-04
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();