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

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

提交回复
热议问题