2

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

在此处输入图像描述

我使用这个代码:

        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();

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

4

5 回答 5

2

我遇到过同样的问题。发现它可能是新材料主题的错误。我的应用程序的主题是:

<style name="AppTheme" parent="@style/Theme.MaterialComponents.Light.NoActionBar">

如果我将其更改为 AppCompat 小吃按钮的灰色背景消失。最后我发现是因为这个bug。

我的解决方案是(我需要 Material 主题,不能简单地将其更改为 AppCompat):找到小吃的按钮 ID。它是“@id/snackbar_action”:

val snackButton: Button = yourSnackbar.getView().findViewById(R.id.snackbar_action)

然后将其背景更改为空:

snackButton.setBackground(null)
于 2019-08-08T17:03:00.757 回答
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);

参考点击这里

于 2016-09-27T07:06:35.050 回答
0

借助 Material Components Library,您可以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>

在此处输入图像描述

于 2020-09-13T08:26:23.963 回答
0

对于 Xamarin,你可以试试这个

 snackbar.View.SetBackgroundColor(Android.Graphics.Color.ParseColor("#32CD32"));
于 2020-08-11T21:42:07.437 回答
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));
于 2020-08-30T00:25:50.740 回答