如何将支持库的快餐栏文本颜色设置为android:textColor以外的其他颜色? [英] How to set support library snackbar text color to something other than android:textColor?

本文介绍了如何将支持库的快餐栏文本颜色设置为android:textColor以外的其他颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我开始在设计支持库中使用新的Snackbar,但是我发现,当您在主题中定义"android:textColor"时,它将应用于快餐栏的文本颜色.如果您的主要文字颜色为深色,这显然是一个问题.

So I've started using the new Snackbar in the Design Support Library, but I found that when you define "android:textColor" in your theme, it applies to the text color of the snackbar. This is obviously a problem if your primary text color is dark.

有人知道如何解决这个问题或对我应该如何为文本加上颜色提出建议吗?

Does anyone know a way around this or have advice for how I should color my text?

编辑2017年1月:(回答后)

虽然有一些自定义解决方案可以解决以下问题,但是最好为主题小吃店提供正确的方法.

While there are some custom solutions to fix the problem below, it's probably good to provide the correct way to theme Snackbars.

首先,您可能根本不应该在主题中定义android:textColor(除非您真的知道使用主题的范围).这设置了基本上每个连接到主题的视图的文本颜色.如果要在非默认视图中定义文本颜色,请使用android:primaryTextColor并在自定义视图中引用该属性.

Firstly, you probably shouldn't be defining android:textColor in your themes at all (unless you really know the scope of what is using the theme). This sets the text color of basically every view that connects to your theme. If you want to define text colors in your views that are not default, then use android:primaryTextColor and reference that attribute in your custom views.

但是,要将主题应用于Snackbar,请从第三方材料doc中引用此质量指南: http ://www.materialdoc.com/snackbar/(遵循程序化主题实现,使其不依赖xml样式)

However, for applying themes to Snackbar, please reference this quality guide from a third party material doc: http://www.materialdoc.com/snackbar/ (Follow the programmatic theme implementation to have it not rely on an xml style)

供参考:

// create instance
Snackbar snackbar = Snackbar.make(view, text, duration);

// set action button color
snackbar.setActionTextColor(getResources().getColor(R.color.indigo));

// get snackbar view
View snackbarView = snackbar.getView();

// change snackbar text color
int snackbarTextId = android.support.design.R.id.snackbar_text;  
TextView textView = (TextView)snackbarView.findViewById(snackbarTextId);  
textView.setTextColor(getResources().getColor(R.color.indigo));

// change snackbar background
snackbarView.setBackgroundColor(Color.MAGENTA);  

(您也可以创建自己的自定义Snackbar布局,请参见上面的链接.如果此方法感觉太过棘手,并且您希望通过一种可靠的方法来确保自定义Snackbar能够通过可能的支持库更新而持续下去,则可以这样做).

(You can also create your own custom Snackbar layouts too, see the above link. Do so if this method feels too hacky and you want a surely reliable way to have your custom Snackbar last through possible support library updates).

或者,请参见下面的答案,以获取解决问题的类似且可能更快的答案.

And alternatively, see answers below for similar and perhaps faster answers to solve your problem.

推荐答案

我知道已经回答了这个问题,但是我发现最简单的方法是直接使用Html.fromHtml方法和font标记

I know this has been answered already but the easiest way I found was directly in the make using the Html.fromHtml method and a font tag

Snackbar.make(view, 
       Html.fromHtml("<font color=\"#ffffff\">Tap to open</font>").show()

这篇关于如何将支持库的快餐栏文本颜色设置为android:textColor以外的其他颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆