【问题标题】:Snackbar action text color not changingSnackbar 操作文本颜色不变
【发布时间】:2015-09-15 23:32:35
【问题描述】:

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

我使用下面的代码来显示一个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();

【问题讨论】:

    标签: android colors android-design-library androiddesignsupport android-snackbar


    【解决方案1】:

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

    而不是这个:

    .setActionTextColor(R.color.yellow)
    

    尝试:

    .setActionTextColor(Color.YELLOW)
    

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

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

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

    【讨论】:

    • snackbar.setActionTextColor(getResources().getColor(R.color.colorPrimary));为我工作......!
    • 这仅适用于 OP 的黄色“定义”与系统相同。
    • 关于 getColor 被弃用,不要使用@SuppressWarning,而是使用 ContextCompat.getColor(context, R.color.youColor)
    • 您正在为 Snackbar (@ColorRes) 分配颜色资源引用,但如果您查看方法 #setActionTextColor 正在请求 @ColorInt Integer,它基本上直接要求颜色的十六进制表示。
    【解决方案2】:

    使用

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

    而不仅仅是

    .setActionTextColor(R.color.red)
    

    【讨论】:

    • getColor(int) 已弃用,请改用ContextCompat.getColor(context, R.color.red)
    【解决方案3】:

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

    【讨论】:

    • 如果有人想使用相同的技术来更改操作按钮,只需添加类似TextView action = view.findViewById(android.support.design.R.id.snackbar_action); action.setTextColor(view.getContext().getResources().getColor(android.R.color.holo_red_dark));
    【解决方案4】:

    如果你想改变动作按钮的文字颜色..

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

    【讨论】:

      猜你喜欢
      • 2016-10-04
      • 2015-08-22
      • 1970-01-01
      • 2016-05-14
      • 2015-04-27
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多