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

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

    I have a simple code that will help to get an instance of both the textview of Snackbar, after that you can call all methods that are applicable on a textview.

    Snackbar snackbar = Snackbar.make( ... )    // Create Snack bar
    
    
    snackbar.setActionTextColor(getResources().getColor(R.color.white));  //if you directly want to apply the color to Action Text
    
    TextView snackbarActionTextView = (TextView) snackbar.getView().findViewById( android.support.design.R.id.snackbar_action );
    
    snackbarActionTextView.setTextColor(Color.RED);  //This is another way of doing it
    
    snackbarActionTextView.setTypeface(snackbarActionTextView.getTypeface(), Typeface.BOLD);
    
    //Below Code is to modify the Text in Snack bar
    TextView snackbarTextView = (TextView) snackbar.getView().findViewById(android.support.design.R.id.snackbar_text);
    snackbarTextView.setTextSize( 16 );
    snackbarTextView.setTextColor(getResources().getColor(R.color.white));
    
    0 讨论(0)
  • 2021-01-30 13:00

    You can use this library: https://github.com/SandroMachado/restaurant

    new Restaurant(MainActivity.this, "Snackbar with custom text color", Snackbar.LENGTH_LONG)
        .setTextColor(Color.GREEN)
        .show();
    

    Disclaimer: I made the library.

    0 讨论(0)
  • 2021-01-30 13:03

    If you migrated to androidX use com.google.android.material.R.id.snackbar_text instead of android.support.design.R.id.snackbar_text for changing color of text on snackbar.

    0 讨论(0)
  • 2021-01-30 13:03

    If you are in Kotlin, you can create an extension :

    fun Snackbar.withTextColor(color: Int): Snackbar {
        val tv = this.view.findViewById(android.support.design.R.id.snackbar_text) as TextView
        tv.setTextColor(color)
        return this
    }
    

    Usage :

    yourSnackBar.withTextColor(Color.WHITE).show()
    
    0 讨论(0)
提交回复
热议问题