我正试图在我的活动中设置一个自定义SnackBar,虽然它没有覆盖我所有的屏幕,但这一切都很好.我的问题是什么呢?

这是我的Snackbar配置:

private fun configureSnackbarWarning() {
        val snackbar = Snackbar.make(binding.root, "", Snackbar.LENGTH_LONG)
        snackbar.view.setBackgroundColor(Color.TRANSPARENT)

        val customSnackView: View = layoutInflater.inflate(R.layout.layout_movements_snackbar, null)

        val snackbarLayout = snackbar.view as Snackbar.SnackbarLayout
        snackbarLayout.setPadding(0, 0, 0, 0)
        snackbarLayout.minimumWidth = ViewGroup.LayoutParams.MATCH_PARENT

        // Add the custom view to the Snackbar's layout
        snackbarLayout.addView(customSnackView)
        snackbar.show()
    }

这是我的Snackbar XML(Layout_Moves_Snackbar):

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/default_background">

    <TextView
        android:id="@+id/txt_warning_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/snackbar_movements_bg_warning"
        android:drawableStart="@drawable/ic_no_movement_warning"
        android:drawablePadding="16dp"
        android:fontFamily="@font/poppins_bold"
        android:padding="16dp"
        android:text="@string/pagination_warning"
        android:textColor="@color/snackbar_text_warning"
        android:textSize="14sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

我已经try 将Snackbar填充和边距设置为0,但仍然没有好的结果

推荐答案

试着改变这个

android:layout_width="match_parent"

对此

android:layout_width="0dp"

因为您正在执行以下操作:

app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"

再加上这个

val customSnackView: View = layoutInflater.inflate(R.layout.layout_movements_snackbar, null)
customSnackView.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)

编辑

这是因为您使用的是material 组件,它们默认情况下增加了毛利.因此,要覆盖此设置,应按如下方式创建样式:

    <style name="CustomSnackBar" parent="Widget.MaterialComponents.Snackbar">
        <item name="android:layout_margin">0dp</item>
    </style>

然后,如果你想要一个Snackbar的宽度,那就把它加到你的应用程序的AppTheme中,但要注意this will affect all the Snackbars.

    <style name="Base.Theme.YourApp" parent="Theme.Material3.DayNight.NoActionBar">
        <!-- Customize your light theme here. -->
        <!-- <item name="colorPrimary">@color/my_light_primary</item> -->
        <item name="snackbarStyle">@style/CustomSnackBar</item>
    </style>

Android相关问答推荐

致命异常:java.lang. SecureExcellent::用户10021和当前进程都没有android. permissions.Change_WIFI_STATE

在Android中点击滑动时触发的手势

ArrayList.remove()(Kotlin中的removeAt())导致奇怪的IndexOutOfBoundsResponse异常

了解数据加载在Kotlin中的工作原理

在模块中找到重复的类com.google.Firebase.auth.ktx.AuthKt||Android Studio

Android写/读二进制文件到共享存储

在Android中使用Room从SQlite数据库中获取实体列表的正确方式是什么?

如何共享没有';t是否存在?(仅信息)在Android?

SDK 33 的问题 - 面向 S+(版本 31 及更高版本)要求在创建 PendingIntent 时指定 FLAG_IMMUTABLE 或 FLAG_MUTABLE 之一

制作圆形SupportMapFragment

如何知道我的应用程序的新版本是否显示广告?

如何只允许拖动 BottomSheetScaffold 中 BottomContent 的 SheetPeek 的一部分?

如何在 Android Jetpack compose 中为列表初始填充设置动画

我们也可以或应该对主要小部件使用预览 compose 功能吗?

如何在 Jetpack Compose 中的 VisualTransformation 之后将光标保持在文本字段的末尾

在 Kotlin 中循环遍历字符串并用新行替换句号

喷气背包组成影子奇怪的行为

Android Jetpack Compose - 每次文本字段值更改时,可组合函数都会重新组合

TextField 溢出和软包装不适用于 Compose 约束布局

如何在jetpack compose中创建自定义rememberState?