简体   繁体   English

Snackbar 操作文本颜色未更改

[英]Snackbar action text color not changing

I want to change the action text color for my snackbar, but it is not working for some reason.我想更改小吃栏的操作文本颜色,但由于某种原因它不起作用。

I use the following code to display a snackbar:我使用以下代码来显示一个小吃店:

Snackbar.make(findViewById(R.id.root), "text", Snackbar.LENGTH_LONG).setActionTextColor(R.color.yellow).setAction("OK", new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    }
}).show();

The argument of setActionTextColor is the int that represents the color, not the resource ID. setActionTextColor的参数是表示颜色的int ,而不是资源 ID。

Instead of this:取而代之的是:

.setActionTextColor(R.color.yellow)

try:尝试:

.setActionTextColor(Color.YELLOW)

If you want to use resources anyway, try:如果您无论如何都想使用资源,请尝试:

.setActionTextColor(ContextCompat.getColor(context, R.color.color_name));

Note: To use ContextCompat, I assume you have included Support library to your build.gradle file (It is optional if you have already appcompat (v7) library too).注意:要使用 ContextCompat,我假设您已经在build.gradle文件中包含了 Support 库(如果您也已经拥有 appcompat (v7) 库,则它是可选的)。

Use

.setActionTextColor(getResources().getColor(R.color.red))

instead of just而不仅仅是

.setActionTextColor(R.color.red)

None of above answers helped me.以上答案都没有帮助我。 I found this solution, and it works by changing manually the TextView's text color我找到了这个解决方案,它的工作原理是手动更改 TextView 的文本颜色

Snackbar snack = Snackbar.make(v, "Snackbar message", Snackbar.LENGTH_LONG);
View view = snack.getView();
TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.WHITE);
snack.show();

If you want to change action button text color..如果要更改操作按钮文本颜色..

snackbar.setActionTextColor(getResources().getColor(R.color.colorAccent));

If you want to change action button background color..如果要更改操作按钮背景颜色..

View sbView = snackbar.getView();
Button button=
(Button) sbView.findViewById(com.google.android.material.R.id.snackbar_action);
button.setBackgroundColor(getResources().getColor(R.color.white));

Try this,尝试这个,

 Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), "Permission required!", 3000 /*Snackbar.LENGTH_INDEFINITE*/);
 snackbar.setAction("OK", new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // perform any action when the button on the snackbar is clicked
            Toast.makeText(MainActivity.this, "Permission granted.", Toast.LENGTH_SHORT).show();
        }
    });
 snackbar.setBackgroundTint(getResources().getColor(R.color.black));      // set the background tint color for the snackbar
 snackbar.setActionTextColor(getResources().getColor(R.color.purple_500)); // set the action button text color
 snackbar.show();

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

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