如何显示这样的自定义Toast

最佳答案

我们可以将SpannableString用作此消息的文本。
就像这个街区:

public void showToast(){
   SpannableString span=new SpannableString("This is a Colorful Spannable text!");
    span.setSpan(new ForegroundColorSpan(Color.CYAN), 10, 11, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    span.setSpan(new ForegroundColorSpan(Color.GRAY), 11, 12, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    span.setSpan(new ForegroundColorSpan(Color.GREEN), 12, 13, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    span.setSpan(new ForegroundColorSpan(Color.MAGENTA), 13, 14, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    span.setSpan(new ForegroundColorSpan(Color.RED), 14, 15, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    span.setSpan(new ForegroundColorSpan(Color.LTGRAY), 15, 16, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    span.setSpan(new ForegroundColorSpan(Color.BLUE), 16, 17, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    span.setSpan(new ForegroundColorSpan(Color.YELLOW), 17, 18, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    Toast.makeText(this,span, Toast.LENGTH_LONG).show();
}

showToast();

结果是:

10-06 03:43