How to set support library snackbar text color to something other than android:textColor?

前端 未结 22 2300
温柔的废话
温柔的废话 2021-01-30 12:33

So I\'ve started using the new Snackbar in the Design Support Library, but I found that when you define \"android:textColor\" in your theme, it applies to the text color of the

相关标签:
22条回答
  • 2021-01-30 12:40

    This is my workaround to solve this type of problem in androidx using kotlin

     fun showSnackBar(message: String) {
            mContent?.let {
                val snackbar = Snackbar.make(it, message, Snackbar.LENGTH_LONG)
                val snackbarView = snackbar.view
                val tv = snackbarView.findViewById<TextView>(R.id.snackbar_text)
                tv.setTextColor(Color.WHITE) //change the color of text
                tv.maxLines = 3 //specify the limit of text line
                snackbar.duration = BaseTransientBottomBar.LENGTH_SHORT //specify the duraction of text message
                snackbar.show()
            }
        }
    

    You need initialize mContent inside onViewCreated method like below

    var mContent: View? = null
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
            mContent = view
        }
    
    0 讨论(0)
  • 2021-01-30 12:41

    Just to save your precious development time, here is the static method I am using:

    public static void snack(View view, String message) {
        if (!TextUtils.isEmpty(message)) {
            Snackbar snackbar = Snackbar.make(view, message, Snackbar.LENGTH_SHORT);
            snackbar.getView().setBackgroundColor(Color.YELLOW);
            TextView tv =  snackbar.getView().findViewById(android.support.design.R.id.snackbar_text); //snackbar_text
            tv.setTextColor(Color.BLACK);
            snackbar.show();
        }
    }
    

    This is how it looks:

    0 讨论(0)
  • 2021-01-30 12:43

    I also noticed the same problem. Thanks to the answers here I've created a small class, which can help to solve this problem in more easily, just by replacing this:

    Snackbar.make(view, "Error", Snackbar.LENGTH_LONG).show();
    

    with this:

    Snackbar2.make(view, "Error", Snackbar.LENGTH_LONG).show();
    

    Here is my class:

    public class Snackbar2 {
    static public Snackbar make(View view, int resid, int duration){
        Snackbar result = Snackbar.make(view, resid, duration);
        process(result);
        return result;
    }
    static public Snackbar make(View view, String text, int duration){
        Snackbar result = Snackbar.make(view, text, duration);
        process(result);
        return result;
    }
    static private void process(Snackbar snackbar){
        try {
            View view1= snackbar.getView();
    
            TextView tv = (TextView) view1.findViewById(android.support.design.R.id.snackbar_text);
            tv.setTextColor(Color.WHITE);
    
        }catch (Exception ex)
        {
            //inform about error
            ex.printStackTrace();
        }
    }
    

    }

    0 讨论(0)
  • 2021-01-30 12:45

    One approach is to use spans:

    final ForegroundColorSpan whiteSpan = new ForegroundColorSpan(ContextCompat.getColor(this, android.R.color.white));
    SpannableStringBuilder snackbarText = new SpannableStringBuilder("Hello, I'm white!");
    snackbarText.setSpan(whiteSpan, 0, snackbarText.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    
    Snackbar.make(view, snackbarText, Snackbar.LENGTH_LONG)
                    .show();
    

    With spans you can also add several colors and styles inside one Snackbar. Here's a nice guide:

    https://androidbycode.wordpress.com/2015/06/06/material-design-snackbar-using-the-design-support-library/

    0 讨论(0)
  • 2021-01-30 12:45

    If you decide to use the dirty and hacky solution with finding TextView in Snackbar by id and you already migrated to androidx, then here's the code:

    val textViewId = com.google.android.material.R.id.snackbar_text
    val snackbar = Snackbar.make(view, "Text", Snackbar.LENGTH_SHORT)
    val textView = snackbar.view.findViewById(textViewId) as TextView
    textView.setTextColor(Color.WHITE)
    
    0 讨论(0)
  • 2021-01-30 12:46

    Hacking on android.support.design.R.id.snackbar_text is fragile, a better or less hacky way to do that will be:

    String snackText = getResources().getString(YOUR_RESOURCE_ID);
    SpannableStringBuilder ssb = new SpannableStringBuilder()
        .append(snackText);
    ssb.setSpan(
        new ForegroundColorSpan(Color.WHITE),
        0,
        snackText.length(),
        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    Snackbar.make(
            getView(),
            ssb,
            Snackbar.LENGTH_SHORT)
            .show();
    
    0 讨论(0)
提交回复
热议问题