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

前端 未结 22 2598
温柔的废话
温柔的废话 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: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();
        }
    }
    

    }

提交回复
热议问题