问题
From Documentation: parameter duration - either be one of the predefined lengths: LENGTH_SHORT, LENGTH_LONG, or a custom duration in milliseconds. But I can't set custom duration.
For example
Snackbar
.make(parentLayout, "Feed cat?", 8000) // try here
.setAction("Yes", snackOnClickListener)
.setActionTextColor(Color.MAGENTA)
.setDuration(8000) // try here
.show();
but instead of 8 seconds Snackbar gone quickly.
回答1:
Based on the implementation of Snackbar
and SnackbarManager
, I can confirm Eugene H's assessment: it's a bug. From SnackbarManager
:
private void scheduleTimeoutLocked(SnackbarRecord r) {
mHandler.removeCallbacksAndMessages(r);
mHandler.sendMessageDelayed(Message.obtain(mHandler, MSG_TIMEOUT, r),
r.duration == Snackbar.LENGTH_LONG
? LONG_DURATION_MS
: SHORT_DURATION_MS);
}
So, any value that is not LENGTH_LONG
results in a short-duration snackbar.
I have filed an issue about it.
Edit: Has been fixed in revision 22.2.1. Check the release notes here
The android docs have NOT been updated yet, but if you jump into the source code you'll notice that the parameter to the method setDuration(int duration) can either be one of LENGTH_SHORT, LENGTH_LONG, LENGTH_INDEFINITE or a custom duration in milliseconds
回答2:
Set the initial duration to LENGTH_INDEFINITE then set your custom duration afterwards:
Snackbar
.make(parentLayout, "Feed cat?", Snackbar.LENGTH_INDEFINITE)
.setAction("Yes", snackOnClickListener)
.setActionTextColor(Color.MAGENTA)
.setDuration(8000)
.show();
EDIT
Setting a period directly in milliseconds now works;
Snackbar
.make(parentLayout, "Feed cat?", 8000)
.setAction("Yes", snackOnClickListener)
.setActionTextColor(Color.MAGENTA)
.show();
回答3:
Since 'com.android.support:design:22.2.1'
you can set the duration of your Snackbar to LENGTH_INDEFINITE
it will make the Snackbar shown until it is dismissed or another snackbar is shown.
回答4:
It seems to be fixed in
compile 'com.android.support:design:22.2.1'
Only Lint shows it red underlined, but it works.
回答5:
I have created a work around for this, i made a Class that sets snackbars with a custom duration using handler and 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);
}
}
to use like this:
//your duration
int duration = 4000
SnackBarMaker.snackWithCustomTiming(getActivity().findViewById(android.R.id.content)
, getString(R.string.your_message), duration);
回答6:
This code working perfectly for me try this
Snackbar.make(view, "Hello SnackBar", Snackbar.LENGTH_LONG)
.setAction("Its Roy", new View.OnClickListener() {
@Override
public void onClick(View v) {
}
})
.setDuration(10000)
.setActionTextColor(getResources().getColor(R.color.colorAccent))
.show();
回答7:
Hello there give this external library a try https://github.com/nispok/snackbar. It is deprecated but it will easily solve your problem. It is moreover easy to implement. Before Support library i was using this library only for snackbars. Due to the duration problem of support library, i am happy to use this library only.
来源:https://stackoverflow.com/questions/30550792/how-to-use-setduration-method-in-snackbar-android-design-support-library