更改 Snackbar 中操作按钮的背景颜色

3
如何更改 Snackbar 中操作按钮的背景颜色或使其消失(灰色背景)?

enter image description here

我使用这段代码:
        Snackbar mysnack = Snackbar.make(main_layout, getResources().getString(R.string.snack_1), 5000);
            View view = mysnack.getView();
            TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
            tv.setTextColor(getResources().getColor(R.color.text_light));
            mysnack.setActionTextColor(getResources().getColor(R.color.text_light));
            mysnack.setAction("RATE", new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Uri uri = Uri.parse(getResources().getString(R.string.snack_url));
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                }
            });
            TypedValue typedValue = new TypedValue();
            getTheme().resolveAttribute(R.attr.colorPrimaryDark, typedValue, true);
            final int color = typedValue.data;
            mysnack.getView().setBackgroundColor(color);
            mysnack.show();

我的问题不是重复的,我问的是背景颜色而不是文本颜色。首先我们要阅读,然后理解,然后思考,最后才决定是否认为某人的问题是重复的。

2
可能是Snackbar动作文本颜色不变的重复问题。 - R. Zagórski
请问您可以添加代码吗? - Haresh Chhelana
1
你可以尝试制作一个自定义的Snackbar。https://dev59.com/4FwY5IYBdhLWcg3wlolg#33441214 - Ibrahim Gharyali
1
尝试一下这个:https://dev59.com/4FwY5IYBdhLWcg3wlolg - Preetika Kaur
5个回答

4
我也遇到了同样的问题。后来发现这可能是新版Material主题的一个bug。我的应用程序的主题是:
<style name="AppTheme" parent="@style/Theme.MaterialComponents.Light.NoActionBar">

如果我将它更改为AppCompat snack按钮,灰色背景会消失。最终我发现这是因为有一个bug。
我的解决方案是(我需要Material主题,不能简单地改为AppCompat): 找到了snack的按钮id。它是"@id/snackbar_action":
val snackButton: Button = yourSnackbar.getView().findViewById(R.id.snackbar_action)

最初的回答是:然后将其背景更改为null:
snackButton.setBackground(null)

好的解决方案 - Elias Fazel

1
这段代码片段可能对您有所帮助:
Snackbar snackbar = Snackbar.make(
                    coordinatorLayout,
                    "Snackbar: floatingActionButton1 (normal) clicked",
                    Snackbar.LENGTH_LONG);
            snackbar.setActionTextColor(Color.RED);
            View snackbarView = snackbar.getView();
            snackbarView.setBackgroundColor(Color.WHITE);
            TextView textView = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
            textView.setTextColor(Color.BLUE);

作为参考,请点击这里

1
使用 Material Components 库,您可以在应用程序主题中使用 snackbarButtonStyle 属性。
例如:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
    <!-- Style to use for action button within a Snackbar in this theme. -->
    <item name="snackbarButtonStyle">@style/Widget.App.Button.TextButton.Snackbar</item>
</style>


<style name="Widget.App.Button.TextButton.Snackbar" parent="Widget.MaterialComponents.Button.TextButton.Snackbar">
    <item name="backgroundTint">@color/colorSecondary</item>
</style>

enter image description here


0

对于Xamarin,你可以试试这个

 snackbar.View.SetBackgroundColor(Android.Graphics.Color.ParseColor("#32CD32"));

0
如果你想要改变动作按钮的背景颜色..
View sbView = snackbar.getView();
Button button=
(Button) sbView.findViewById(com.google.android.material.R.id.snackbar_action);
button.setBackgroundColor(getResources().getColor(R.color.white));

如果你想要改变动作按钮的文字颜色..
snackbar.setActionTextColor(getResources().getColor(R.color.colorAccent));

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