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

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

    As per new AndroidX Jitpack components

    implementation 'com.google.android.material:material:1.0.0'
    

    Use this extension which i had created

    inline fun View.snack(message: String, length: Int = Snackbar.LENGTH_LONG,
     f: Snackbar.() -> Unit) {
    val snack = Snackbar.make(this, message, length)
    snack.f()
    snack.show()
    }
    
    fun Snackbar.action(action: String, actionColor: Int? = null, textColor: Int? = null, listener: (View) -> Unit) {
    setAction(action, listener)
    actionColor?.let {
        setActionTextColor(it)
        }
    textColor?.let {
        this.view.findViewById(R.id.snackbar_text).setTextColor(it)
        }
    }
    

    Use it like this

    btn_login.snack(
            getString(R.string.fields_empty_login),
            ContextCompat.getColor(this@LoginActivity, R.color.whiteColor)
        ) {
            action(getString(R.string.text_ok), ContextCompat.getColor(this@LoginActivity, R.color.gray_300),ContextCompat.getColor(this@LoginActivity, R.color.yellow_400)) {
                this@snack.dismiss()
            }
        }
    

提交回复
热议问题