This is my Snackbar code :
这是我的Snackbar代码:
Snackbar.make(viewHolder.productView, "Some Text Here ..", Snackbar.LENGTH_SHORT)
.setAction("I want be a icon here instead TEXT", new View.OnClickListener() {
@Override
public void onClick(View v) {
// Perform anything for the action selected
}
})
.show();
Is there anyway to add icon instead of text in .setAction
?
无论如何在.setAction中添加图标而不是文本?
I know google suggested not use icon in Snackbar, but I want to use it.
我知道谷歌建议不要在Snackbar中使用图标,但我想使用它。
3 个解决方案
#1
31
ImageSpan may not look right with longer text (multiple lines). Instead, use a left compound drawable (same as android:drawableLeft
).
使用较长的文本(多行),ImageSpan可能看起来不正确。相反,使用左复合drawable(与android:drawableLeft相同)。
Snackbar snackbar = Snackbar.make(layout, R.string.test, Snackbar.LENGTH_LONG);
View snackbarLayout = snackbar.getView();
TextView textView = (TextView)snackbarLayout.findViewById(android.support.design.R.id.snackbar_text);
textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.white_checkbox, 0, 0, 0);
textView.setCompoundDrawablePadding(getResources().getDimensionPixelOffset(R.dimen.snackbar_icon_padding));
snackbar.show();
#2
16
Have you heard of ImageSpan? It may help you you to achieve your goal! See below code:
你听说过ImageSpan吗?它可以帮助您实现目标!见下面的代码:
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append("My message ").append(" ");
builder.setSpan(new ImageSpan(MainActivity.this, R.drawable.ic_launcher), builder.length() - 1, builder.length(), 0);
builder.append(" next message");
Snackbar.make(parent view, builder, Snackbar.LENGTH_LONG).show();]
Dont use big icons as they will not maintain gravity.
不要使用大图标,因为它们不会保持重力。
Ref:How to display image in Android's TextView?
参考:如何在Android的TextView中显示图像?
Hope it helped.
希望它有所帮助。
#3
2
This is all you can change of a Snackbar
:
这是你可以改变Snackbar的全部内容:
Snackbar snackbar = Snackbar
.make(parentLayout, R.string.snackbar_text, Snackbar.LENGTH_LONG)
.setAction(R.string.snackbar_action, myOnClickListener);
snackbar.setActionTextColor(Color.CYAN);
View snackbarView = snackbar.getView();
snackbarView.setBackgroundColor(Color.YELLOW);//change Snackbar's background color;
TextView textView = (TextView)snackbarView .findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(Color.BLUE);//change Snackbar's text color;
snackbar.show(); // Don’t forget to show!
Unfortunately, we still can't customize contents of a Snackbar
by far like we do to Toast
. In the future? I don't know.
不幸的是,我们仍然无法像Toast那样自定义Snackbar的内容。在将来?我不知道。
#1
31
ImageSpan may not look right with longer text (multiple lines). Instead, use a left compound drawable (same as android:drawableLeft
).
使用较长的文本(多行),ImageSpan可能看起来不正确。相反,使用左复合drawable(与android:drawableLeft相同)。
Snackbar snackbar = Snackbar.make(layout, R.string.test, Snackbar.LENGTH_LONG);
View snackbarLayout = snackbar.getView();
TextView textView = (TextView)snackbarLayout.findViewById(android.support.design.R.id.snackbar_text);
textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.white_checkbox, 0, 0, 0);
textView.setCompoundDrawablePadding(getResources().getDimensionPixelOffset(R.dimen.snackbar_icon_padding));
snackbar.show();
#2
16
Have you heard of ImageSpan? It may help you you to achieve your goal! See below code:
你听说过ImageSpan吗?它可以帮助您实现目标!见下面的代码:
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append("My message ").append(" ");
builder.setSpan(new ImageSpan(MainActivity.this, R.drawable.ic_launcher), builder.length() - 1, builder.length(), 0);
builder.append(" next message");
Snackbar.make(parent view, builder, Snackbar.LENGTH_LONG).show();]
Dont use big icons as they will not maintain gravity.
不要使用大图标,因为它们不会保持重力。
Ref:How to display image in Android's TextView?
参考:如何在Android的TextView中显示图像?
Hope it helped.
希望它有所帮助。
#3
2
This is all you can change of a Snackbar
:
这是你可以改变Snackbar的全部内容:
Snackbar snackbar = Snackbar
.make(parentLayout, R.string.snackbar_text, Snackbar.LENGTH_LONG)
.setAction(R.string.snackbar_action, myOnClickListener);
snackbar.setActionTextColor(Color.CYAN);
View snackbarView = snackbar.getView();
snackbarView.setBackgroundColor(Color.YELLOW);//change Snackbar's background color;
TextView textView = (TextView)snackbarView .findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(Color.BLUE);//change Snackbar's text color;
snackbar.show(); // Don’t forget to show!
Unfortunately, we still can't customize contents of a Snackbar
by far like we do to Toast
. In the future? I don't know.
不幸的是,我们仍然无法像Toast那样自定义Snackbar的内容。在将来?我不知道。