首先在项目build.gradlew中添加**
//这个是依赖弹窗所有的
implementation 'com.github.yangchong211.YCDialog:DialogLib:3.6.8'
//如果你想使用某个lib,可以选择下面这种依赖
implementation 'com.github.yangchong211.YCDialog:PopupWindow:3.6.8'
implementation 'com.github.yangchong211.YCDialog:ToastUtils:3.6.8'
implementation 'com.github.yangchong211.YCDialog:SnackBar:3.6.8'
implementation 'com.github.yangchong211.YCDialog:DialogFragment:3.6.8'
implementation 'com.github.yangchong211.YCDialog:BaseDialog:3.6.8'
private void showCustomDialog() {
final List<String> names = new ArrayList<>();
names.add("拍照");
names.add("相册");
names.add("其他");
showDialog(new CustomSelectDialog.SelectDialogListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
}, names);
}
private CustomSelectDialog showDialog(CustomSelectDialog.SelectDialogListener listener,
List<String> names) {
CustomSelectDialog dialog = new CustomSelectDialog(this,
R.style.transparentFrameWindowStyle, listener, names);
dialog.setItemColor(R.color.colorAccent,R.color.colorPrimary);
//判断activity是否finish
if (!this.isFinishing()) {
dialog.show();
}
return dialog;
}
支持设置吐司的背景颜色,支持设置自定义吐司,同时支持设置吐司的多种属性等等,具体的用法如下所示:
//可以自由设置吐司的背景颜色,默认是纯黑色
ToastUtils.setToastBackColor(this.getResources().getColor(R.color.color_7f000000));
//直接设置最简单吐司,只有吐司内容
ToastUtils.showRoundRectToast("自定义吐司");
//设置吐司标题和内容
ToastUtils.showRoundRectToast("吐司一下","潇湘剑雨杨充是个逗比");
//第三种直接设置自定义布局的吐司
ToastUtils.showRoundRectToast(R.layout.view_layout_toast_delete);
//或者直接采用bulider模式创建
ToastUtils.Builder builder = new ToastUtils.Builder(this.getApplication());
builder
.setDuration(Toast.LENGTH_SHORT)
.setFill(false)
.setGravity(Gravity.CENTER)
.setOffset(0)
.setDesc("内容内容")
.setTitle("标题")
.setTextColor(Color.WHITE)
.setBackgroundColor(this.getResources().getColor(R.color.blackText))
.build()
.show();
只需要继承BasePopDialog即可,实现其中的两个抽象方法
//第一步,自定义pop,继承BasePopDialog类,重新抽象方法
public class CustomPop extends BasePopDialog {
public CustomPop(Context context) {
super(context);
}
@Override
public int getViewResId() {
return R.layout.view_pop_custom;
}
@Override
public void initData(View contentView) {
TextView tv_pop = (TextView) contentView.findViewById(R.id.tv_pop);
tv_pop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ToastUtils.showRoundRectToast("滚犊子吧");
}
});
}
}
//第二步,创建pop并且展示
CustomPop customPop = new CustomPop(this);
customPop.setDelayedMsDismiss(2500);
customPop.setBgAlpha(0.5f);
customPop.showAsDropDown(tv6, 0, -tv6.getMeasuredHeight() - tv6.getHeight());
View contentView = LayoutInflater.from(this).inflate(R.layout.pop_layout,null);
//创建并显示popWindow
popWindow = new CustomPopupWindow.PopupWindowBuilder(this)
//.setView(R.layout.pop_layout)
.setView(contentView)
.setFocusable(true)
//弹出popWindow时,背景是否变暗
.enableBackgroundDark(true)
//控制亮度
.setBgDarkAlpha(0.7f)
.setOutsideTouchable(true)
.setAnimationStyle(R.style.popWindowStyle)
.setOnDissmissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
//对话框销毁时
}
})
.create()
.showAsDropDown(tv6,0,10);
代码如下所示
new CustomBottomDialog(MainActivity.this)
.title("这个是标题")
.setCancel(true,"取消选择")
.orientation(CustomBottomDialog.VERTICAL)
.inflateMenu(R.menu.menu_share, new OnItemClickListener() {
@Override
public void click(CustomItem item) {
}
})
.show();
final BottomDialogFragment dialog = new BottomDialogFragment();
dialog.setFragmentManager(getSupportFragmentManager());
dialog.setViewListener(new BottomDialogFragment.ViewListener() {
@Override
public void bindView(View v) {
TextView tv_cancel = (TextView) v.findViewById(R.id.tv_cancel);
tv_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismissDialogFragment();
}
});
}
});
dialog.setLayoutRes(R.layout.dialog_bottom_layout);
dialog.setDimAmount(0.5f);
dialog.setTag("BottomDialog");
dialog.setCancelOutside(true);
//这个高度可以自己设置,十分灵活
dialog.setHeight(getScreenHeight() / 2);
dialog.show();
比较常见,比如下载音乐,下载视频等等,弹窗页面可以滚动,ok,就可以使用这个,代码也不是很复杂,具体使用场景可以参考我的另外一个demo:https://github.com/yangchong211/YCVideoPlayer
final List<DialogBean> list = new ArrayList<>();
for(int a=0 ; a<20 ; a++){
DialogBean dialogBean = new DialogBean("ooo","杨充","title");
list.add(dialogBean);
}
BottomDialogFragment.create(getSupportFragmentManager())
.setViewListener(new BottomDialogFragment.ViewListener() {
@Override
public void bindView(View v) {
RecyclerView recyclerView = (RecyclerView) v.findViewById(R.id.recyclerView);
ImageView ivCancel = (ImageView) v.findViewById(R.id.iv_cancel);
ImageView ivDownload = (ImageView) v.findViewById(R.id.iv_download);
recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
DialogListAdapter mAdapter = new DialogListAdapter(MainActivity.this, list);
recyclerView.setAdapter(mAdapter);
final RecycleViewItemLine line = new RecycleViewItemLine(
MainActivity.this, LinearLayout.HORIZONTAL, 2,
MainActivity.this.getResources().getColor(R.color.grayLine));
recyclerView.addItemDecoration(line);
mAdapter.setOnItemClickListener(new DialogListAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
}
});
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.iv_cancel:
break;
case R.id.iv_download:
ToastUtils.showToast("下载");
break;
default:
break;
}
}
};
ivCancel.setOnClickListener(listener);
ivDownload.setOnClickListener(listener);
}
})
.setLayoutRes(R.layout.dialog_bottom_layout_list)
.setDimAmount(0.5f)
.setTag("BottomDialog")
.setCancelOutside(true)
.setHeight(getScreenHeight() / 2)
.show();
CustomDialogFragment
.create(getSupportFragmentManager())
.setTitle("这个是是标题")
.setContent("这个是弹窗的内容")
.setOtherContent("其他")
.setDimAmount(0.2f)
.setTag("dialog")
.setCancelOutside(true)
.setCancelListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CustomDialogFragment.dismissDialogFragment();
ToastUtils.showRoundRectToast("取消了");
}
})
.setOkListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CustomDialogFragment.dismissDialogFragment();
ToastUtils.showRoundRectToast("确定了");
}
})
.setOtherListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CustomDialogFragment.dismissDialogFragment();
ToastUtils.showRoundRectToast("其他内容");
}
})
.show();
//开始loading
ViewLoading.show(this);
ViewLoading.show(this,"加载中");
//结束loading
ViewLoading.dismiss(this);
可以一行代码调用,也可以自己使用链式编程调用。支持设置显示时长属性;可以设置背景色;可以设置文字大小,颜色;可以设置action内容,文字大小,颜色,还有点击事件;可以设置icon;代码如下所示,更多内容可以直接运行demo哦!
//1.只设置text
SnackBarUtils.showSnackBar(this,"滚犊子");
//2.设置text,action,和点击事件
SnackBarUtils.showSnackBar(this, "滚犊子", "ACTION", new View.OnClickListener() {
@Override
public void onClick(View v) {
ToastUtils.showRoundRectToast("滚犊子啦?");
}
});
//3.设置text,action,和点击事件,和icon
SnackBarUtils.showSnackBar(this, "滚犊子", "ACTION",R.drawable.icon_cancel, new View.OnClickListener() {
@Override
public void onClick(View v) {
ToastUtils.showRoundRectToast("滚犊子啦?");
}
});
//4.链式调用
SnackBarUtils.builder()
.setBackgroundColor(this.getResources().getColor(R.color.color_7f000000))
.setTextSize(14)
.setTextColor(this.getResources().getColor(R.color.white))
.setTextTypefaceStyle(Typeface.BOLD)
.setText("滚犊子")
.setMaxLines(4)
.centerText()
.setActionText("收到")
.setActionTextColor(this.getResources().getColor(R.color.color_f25057))
.setActionTextSize(16)
.setActionTextTypefaceStyle(Typeface.BOLD)
.setActionClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ToastUtils.showRoundRectToast("滚犊子啦?");
}
})
.setIcon(R.drawable.icon_cancel)
.setActivity(MainActivity.this)
.setDuration(SnackBarUtils.DurationType.LENGTH_INDEFINITE)
.build()
.show();