Toast消息提示
工具类封装
Tips:Toast只可以在UI线程中运行,所以不同考虑线程同步
public class ToastUtil {
private static Toast toast;
public static void showShortToast(Context context, String msg) {
initToast(context, msg, Toast.LENGTH_SHORT);
}
public static void showLongToast(Context context, String msg) {
initToast(context, msg, Toast.LENGTH_LONG);
}
public static Toast getToast(Context context) {
return toast != null ? toast : Toast.makeText(context, "", Toast.LENGTH_SHORT);
}
private static void initToast(Context context, String msg, int duration) {
if (toast != null) {
toast.cancel();
}
toast = Toast.makeText(context, msg, duration);
toast.show();
}
}
四种用法
- 默认样式
ToastUtil.showLongToast(MainActivity.this,"");
- 自定义位置
Toast toast = ToastUtil.getToast(MainActivity.this);
//setGravity的后两个参数分别是xOffset和yOffset
//分别表示提示框偏移设置位置(这里是CENTER:屏幕中心)的量
//都设置为0表示提示框不偏移
toast.setGravity(Gravity.CENTER,0,0);
toast.setText("hello");
toast.show();
- 带图片
Toast toast = ToastUtil.getToast(MainActivity.this);
ImageView imageView = new ImageView(getApplicationContext());
imageView.setImageResource(R.mipmap.ic_launcher);
toast.setView(imageView);
toast.show();
- 完全自定义
<!-- 自定义Toast的View:custom_toast.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="match_parent"
android:orientation="vertical"
android:id="@+id/ll_root">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_title"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/iv_left"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_msg"
/>
</LinearLayout>
</LinearLayout>
//自定义了标题和图片
Toast toast = ToastUtil.getToast(MainActivity.this);
View view = getLayoutInflater().inflate(R.layout.custom_toast,null);
TextView title = view.findViewById(R.id.tv_title);
ImageView imageView = view.findViewById(R.id.iv_left);
title.setText("hello title");
imageView.setImageResource(R.mipmap.ic_launcher);
toast.setView(view);
toast.show();
SeekBar消息提示
简单用法
- 获取view(SnackBar的make方法的第一个参数)
//第一种:获取系统的根布局
View decorView = getWindow().getDecorView();
View view = decorView.findViewById(android.R.id.content);
//第二种:直接在布局文件中加入CoordinatorLayout
//cl_root是在xml中定义的一个布局
CoordinatorLayout coordinatorLayout = decorView.findViewById(R.id.cl_root);
- 显示提示
Snackbar.make([coordinatorLayout/view],"展示SnackBar",Snackbar.LENGTH_LONG)
.setAction("close", new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG,"点击了关闭");
}
}).show();
自定义用法
snackbar.getView().setPadding(0,0,0,0);
// 将SnackbarView的背景颜色设置为透明,避免在自定义布局中有圆角或者自适应宽度的时候显示一块黑色背景的情况
snackbar.getView().setBackgroundColor(Color.TRANSPARENT);
// 获取到Snackbar.getView获取的Snackbar的view
Snackbar.SnackbarLayout snackbarView = (Snackbar.SnackbarLayout) snackbar.getView();
// 获取到SnackbarView的LayoutParams
ViewGroup.LayoutParams layoutParams = snackbarView.getLayoutParams();
// 新建一个LayoutParams将SnackbarView的LayoutParams的宽高传入
FrameLayout.LayoutParams fl = new FrameLayout.LayoutParams(layoutParams.width, layoutParams.height);
// 设置新的元素位置
// Gravity有许多属性,基本上可以满足大众需求, 我们这里设置了处于屏幕的中央
fl.gravity = Gravity.CENTER;
// 将新的LayoutParams设置给SnackbarView
snackbarView.setLayoutParams(fl);
原文链接:https://blog.csdn.net/azhoup/article/details/117929028
// 加载咱们自定义的布局
View inflate = LayoutInflater.from(snackbar.getView().getContext()).inflate(R.layout.snacbar_layout, null);
// 通过自定义布局中的控件
TextView text = inflate.findViewById(R.id.textView);
// 设置自定义的文案
text.setText("自定义布局的Snackbar");
// 通过自定义布局中的控件
ImageView imageView = inflate.findViewById(R.id.imageView);
// 设置点击事件
imageView.setOnClickListener(v1 -> Log.d("TAG", "点击了自定义布局中的控件"));
// 将获取的自定义布局view添加到SnackbarView中
snackbarView.addView(inflate);
// 展示
snackbar.show();