繁体   English   中英

更改快餐栏中操作按钮的背景颜色

[英]Change the background color of action button in snackbar

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

在此处输入图片说明

我使用这个代码:

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

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

我遇到过同样的问题。 发现它可能是新材料主题的错误。 我的申请的主题是:

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

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

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

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

然后将其背景更改为空:

snackButton.setBackground(null)

此代码片段可能会帮助您:

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

参考点击这里

对于 Xamarin,你可以试试这个

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

如果要更改操作按钮背景颜色..

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

通过材料组件库,您可以在应用程序主题中使用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>

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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