如何更改Snackbar的背景颜色?

123

我正在DialogFragment中,在警报对话框的正面接触中显示snackbar。这是我的代码片段:

Snackbar snackbar = Snackbar.make(view, "Please enter customer name", Snackbar.LENGTH_LONG)
                .setAction("Action", null);
View sbView = snackbar.getView();
sbView.setBackgroundColor(Color.BLACK);
snackbar.show();

正如您所见,我的snackbar的背景颜色显示为白色

我正在将DialogFragment的视图传递给 snackbar。我想要将背景色设置为黑色。我该怎么做?我在DialogFragment中返回了alertDialog。并且我设置的对话框主题如下:

<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">

    <!-- Used for the buttons -->
    <item name="colorAccent">@color/accent</item>
    <!-- Used for the title and text -->
    <item name="android:textColorPrimary">@color/primary</item>
    <!-- Used for the background -->
    <item name="android:background">@color/white</item>
</style>

虽然我将对话框的背景色设置为白色,但通过将背景色设置为 snackbar ,它应该被覆盖。


3
http://www.technotalkative.com/part-3-styling-snackbar/ - M D
我已经尝试过了,没有帮助...我正在从对话框片段+其中的AlertDialog调用snackbar,并将正按钮单击视图传递给snackbar。 - Ajinkya
21个回答

2

将其放入一个实用程序类中:

public class Utility {
    public static void showSnackBar(Context context, View view, String text) {
        Snackbar sb = Snackbar.make(view, text, Snackbar.LENGTH_SHORT);
        sb.getView().setBackgroundColor(ContextCompat.getColor(context, R.color.colorAccent));
        sb.show();
    }
}

使用方法如下:

Utility.showSnackBar(getApplicationContext(), findViewById(android.R.id.content), "Add success!!!");

2

使用Java的旧格式

mySnackbar.setBackgroundColor(ContextCompat.getColor(getActivity(), android.R.color.black));

使用kt实际格式化

mySnackbar.setBackgroundTint(ContextCompat.getColor(applicationContext, android.R.color.black));

2
对于 Kotlin:
Snackbar.make(view,"simple text",Snackbar.LENGTH_SHORT).setBackgroundTint(Color.RED).show()

1
基本上,提供的解决方案有一个缺点。它们改变了 snackbar 的形状并去除了圆角。
个人而言,更喜欢像这样的东西。
val snackbar = Snackbar.make(view, text, Snackbar.LENGTH_LONG);
val view = snackbar.getView();
val color = view.resources.getColor(colorId)
view.background.colorFilter = PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP)

@KishanSolanki 你可以使用这个代替。 - Vaios

1
"

setBackgroundResource()同样有效。

"
Snackbar snackbar = Snackbar.make(view, text, Snackbar.LENGTH_LONG);
View sbView = snackbar.getView();
sbView.setBackgroundResource(R.color.background);
snackbar.show();

1

对我来说,其他解决方案都没有真正起作用。如果我只设置Snackbar的背景颜色,则TextView和Button下方的布局仍为默认颜色。如果我设置TextView的背景,则在显示SnackBar后它会稍微闪烁。而按钮周围的布局仍然是默认颜色。

最后我发现,对我来说最好的方法是更改TextView父元素(SnackbarContentLayout)的背景颜色。现在整个Snackbar的颜色都正确,并且在显示时不会闪烁。

snack = Snackbar.make(view, text, duration)
View view = snack.getView();
view.setBackgroundColor(BACKGROUND_COLOR);
TextView tv = view.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(TEXT_COLOR);
((SnackbarContentLayout) tv.getParent()).setBackgroundColor(BACKGROUND_COLOR);

1

我不知道为什么在我的项目中找不到setBackgroundColor()。这就是为什么我创建了一个扩展函数,现在一切都好了。

fun View.showSnackBar(message: String) {
    val snackBar = Snackbar.make(this, message, Snackbar.LENGTH_LONG)
    snackBar.setBackgroundTint(ContextCompat.getColor(this.context, R.color.colorAccent))
    snackBar.show()
}

将其翻译为中文:"

并像下面这样调用它

"。

activity_login.xml

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout 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:id="@+id/login_holder_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   // your UI

</FrameLayout>

LoginActivity.kt

login_holder_layout.showSnackBar("Invalid Email") 

1

2023年7月:Kotlin工作代码示例

enter image description here

private fun showSnackBar(message: String) {
    rootView?.let {
        val snackBar = Snackbar.make(it, message, Snackbar.LENGTH_INDEFINITE).apply {
            setAction("Reload") {}
            view.setBackgroundColor(ContextCompat.getColor(it.context, R.color.yellow))
            show()
        }
    }
}

0
如果您使用 Material 3 主题
<style name="ApplicationTheme" parent="Theme.Material3.Dark.NoActionBar">
    <item name="snackbarStyle">@style/SnackbarStyle</item>
    <item name="snackbarTextViewStyle">@style/SnackbarTextViewStyle</item>
</style>

<style name="SnackbarStyle" parent="Widget.MaterialComponents.Snackbar">
    <item name="backgroundTint">@android:color/black</item>
</style>

<style name="SnackbarTextViewStyle" parent="Widget.MaterialComponents.Snackbar.TextView">
    <item name="android:textColor">@android:color/white</item>
</style>

0
public class CustomBar {

public static void show(View view, String message, boolean isLong) {
    Snackbar s = Snackbar.make(view, message, isLong ? Snackbar.LENGTH_LONG : Snackbar.LENGTH_SHORT);
    s.getView().setBackgroundColor(ContextCompat.getColor(view.getContext(), R.color.red_900));
    s.show();
}

public static void show(View view, @StringRes int message, boolean isLong) {
    Snackbar s = Snackbar.make(view, message, isLong ? Snackbar.LENGTH_LONG : Snackbar.LENGTH_SHORT);
    s.getView().setBackgroundColor(ContextCompat.getColor(view.getContext(), R.color.red_900));
    s.show();
}

}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接