Snackbar action text color not changing

青春壹個敷衍的年華 提交于 2019-12-05 08:28:10

问题


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

回答1:


The argument of setActionTextColor is the int that represents the color, not the resource 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).




回答2:


Use

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

instead of just

.setActionTextColor(R.color.red)



回答3:


None of above answers helped me. I found this solution, and it works by changing manually the TextView's text color

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


来源:https://stackoverflow.com/questions/31116774/snackbar-action-text-color-not-changing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!