我为此创建了一个解决方法,我创建了一个使用处理程序和 postDelayed 设置具有自定义持续时间的小吃店的类:
public class SnackBarMaker {
public static void snack(View content, String message, String actionText, int actionTextColor, View.OnClickListener onClick){
Snackbar.make(content, message, Snackbar.LENGTH_LONG)
.setAction(actionText, onClick)
.setActionTextColor(actionTextColor)
.show();
}
public static void snackWithCustomTiming(View content, String message, int duration){
final Snackbar snackbar = Snackbar.make(content, message, Snackbar.LENGTH_INDEFINITE);
snackbar.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
snackbar.dismiss();
}
},duration);
}
}
像这样使用:
//your duration
int duration = 4000
SnackBarMaker.snackWithCustomTiming(getActivity().findViewById(android.R.id.content)
, getString(R.string.your_message), duration);