[英]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
文件中包含了 Support 库(如果您也已经拥有 appcompat (v7) 库,则它是可选的)。
用
.setActionTextColor(getResources().getColor(R.color.red))
而不仅仅是
.setActionTextColor(R.color.red)
以上答案都没有帮助我。 我找到了这个解决方案,它的工作原理是手动更改 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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.