Snackbar 操作文本颜色未更改

Snackbar action text color not changing

我想更改我的小吃栏的操作文本颜色,但由于某种原因它不起作用。

我使用以下代码来显示一个小吃店:

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

setActionTextColor的参数是表示颜色的int,不是资源ID。

而不是这个:

.setActionTextColor(R.color.yellow)

尝试:

.setActionTextColor(Color.YELLOW)

如果您仍然想使用资源,请尝试:

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

注意:要使用 ContextCompat,我假设您已将支持库包含到您的 build.gradle 文件中(如果您也已经拥有 appcompat (v7) 库,则它是可选的)。

使用

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

而不仅仅是

.setActionTextColor(R.color.red)

None 以上答案对我有帮助。 我找到了这个解决方案,它通过手动更改 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();

如果您想更改操作按钮文本颜色..

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

如果您想更改操作按钮的背景颜色..

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