自定义小吃店有文字覆盖

Custom snackbar has a text overlay

我正在使用以下内容制作自定义小吃店

fun showSnack(view: View, text:String){
 
    val snackbar = Snackbar.make(view, text, Snackbar.LENGTH_LONG)
    val snackbarView = snackbar.view
     snackbarView.setBackgroundColor(Color.TRANSPARENT)
    val inflater = LayoutInflater.from(view.context)
    val layout = inflater.inflate(R.layout.custom_snack,null,false)
    layout.custom_text.text = text
    val slayout = snackbar.view as SnackbarLayout
    slayout.addView(layout,0)
    snackbar.show()
}

custom_snack.xml :

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:minHeight="0dp"
    android:layout_margin="2dp"
    app:cardBackgroundColor="@color/off_white"
    android:theme="@style/Theme.MaterialComponents.Light"
    app:cardCornerRadius="16dp"
    android:background="@color/transparent"
    app:cardElevation="0dp"
    >

<androidx.appcompat.widget.LinearLayoutCompat
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="0dp"
    android:padding="8dp"
    android:orientation="horizontal">
    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/appCompatImageView"
        android:layout_width="50dp"
        android:layout_height="25dp"
        android:src="@drawable/home" />

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/toast_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="sample text"
        android:gravity="center"
        android:paddingStart="8dp"
        android:layout_gravity="center"
        tools:ignore="HardcodedText,RtlSymmetry" />
</androidx.appcompat.widget.LinearLayoutCompat>



</com.google.android.material.card.MaterialCardView>

像这样使用它:

showSnack(findViewById(android.R.id.content),"Access Granted")

自定义快餐栏显示在屏幕上,但有一个文本覆盖,我不知道它是从哪里来的。额外文字额外文字额外文字额外文字额外文字额外文字额外文字额外文字

您的问题在这里:

val snackbar = Snackbar.make(view, text, Snackbar.LENGTH_LONG)

使用:

val snackbar = Snackbar.make(view, "", Snackbar.LENGTH_LONG)