简体   繁体   中英

Custom snackbar has a text overlay

I am making a custom snackbar with the following

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>

using it like so:

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

the custom snackbar showsup in the screen but there is a text overlay and I can't figure where it's coming from. Extra text Extra text Extra text Extra text Extra text Extra text Extra text Extra text

截屏

Your issue is here:

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

Use:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM