更改小吃栏中动作按钮的背景颜色 [英] Change the background color of action button in snackbar

本文介绍了更改小吃栏中动作按钮的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更改小吃店中操作"按钮的背景颜色或使其消失(灰色背景)?

how can I change the background color of action button in snackbar or make it dissapear (grey background)?

我使用以下代码:

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

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

and my question is not duplicate. I ask for BACKGROUND color and not the text color. First we read, then we understand, then we think and then we decide to write that someone's question is a duplicate.

推荐答案

我遇到了同样的问题.发现这可能是新的Material主题的错误. 我的应用程序的主题是:

I had the same issue. Found out it can be a bug of new Material theme. Main theme of my application is:

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

如果我将其更改为AppCompat零食按钮的灰色背景将消失.最终,我发现这是由于该错误所致.

If I change it to AppCompat snack button's gray background dissapears. Eventually I found that it was because of the bug.

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

My solution was (I needed Material theme and can not simply change it to AppCompat): Found button id for the snack. It is "@id/snackbar_action":

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

,然后将其背景更改为null:

and then changed it background to null:

snackButton.setBackground(null)

这篇关于更改小吃栏中动作按钮的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆