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

前端 未结 22 2303
温柔的废话
温柔的废话 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:53

    Find by id did't work for me so I found another solution:

    Snackbar snackbar = Snackbar.make(view, text, duration);//just ordinary creation
    
    ViewGroup snackbarView = (ViewGroup) snackbar.getView();
    SnackbarContentLayout contentLayout = (SnackbarContentLayout) snackbarView.getChildAt(0);
    TextView tvText = contentLayout.getMessageView();
    tvText.setTextColor(/*your color here*/);
    
    //set another colors, show, etc
    
    0 讨论(0)
  • 2021-01-30 12:55

    I changed my theme

    Theme.AppCompat.Light.NoActionBar
    

    to

    Theme.AppCompat.NoActionBar 
    

    It worked.Try to use simple theme instead of light or other theme.

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

    I found this at What are the new features of Android Design Support Library and how to use its Snackbar?

    This worked for me for changing the text color in a Snackbar.

    Snackbar snack = Snackbar.make(view, R.string.message, Snackbar.LENGTH_LONG);
    View view = snack.getView();
    TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
    tv.setTextColor(Color.WHITE);
    snack.show();
    



    UPDATE: ANDROIDX: As dblackker points out in the comments, with the new AndroidX support library, code to find the ID of Snackbar TextView changes to:

    TextView tv = view.findViewById(com.google.android.material.R.id.snackbar_text);
    tv.setTextColor(ContextCompat.getColor(requireContext(), R.color.someColor))
    
    0 讨论(0)
  • 2021-01-30 12:57

    Use the Snackbar included in the Material Components Library and apply the

    • setTextColor to define the text color
    • setActionTextColor to define the text color used by the action. You can use a color or a color selector
    • setBackgroundTint to define the background color of the Snackbar

    Something like:

    Snackbar snackbar = Snackbar.make(view, "My custom Snackbar", Snackbar.LENGTH_LONG);        
    snackbar.setTextColor(ContextCompat.getColor(this,R.color.xxxxx));
    snackbar.setActionTextColor(ContextCompat.getColor(this,R.color.my_selector));
    snackbar.setBackgroundTint(ContextCompat.getColor(this,R.color.xxxx));
    snackbar.show();
    

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

    I know this has been answered already but the easiest way I found was directly in the make using the Html.fromHtml method and a font tag

    Snackbar.make(view, 
           Html.fromHtml("<font color=\"#ffffff\">Tap to open</font>").show()
    
    0 讨论(0)
  • 2021-01-30 12:58

    This is what I use when I need custom colors

        @NonNull
        public static Snackbar makeSnackbar(@NonNull View layout, @NonNull CharSequence  text, int duration, int backgroundColor, int textColor/*, int actionTextColor*/){
            Snackbar snackBarView = Snackbar.make(layout, text, duration);
            snackBarView.getView().setBackgroundColor(backgroundColor);
            //snackBarView.setActionTextColor(actionTextColor);
            TextView tv = (TextView) snackBarView.getView().findViewById(android.support.design.R.id.snackbar_text);
            tv.setTextColor(textColor);
            return snackBarView;
        }
    

    And consumed as:

    CustomView.makeSnackbar(view, "Hello", Snackbar.LENGTH_LONG, Color.YELLOW,Color.CYAN).setAction("DO IT", myAction).show();
    
    0 讨论(0)
提交回复
热议问题