如何设置支持库零食栏文本颜色的东西比android:textColor?

所以我开始在设计支持库中使用新的Snackbar,但是我发现当你在主题中定义“android:textColor”时,它适用于快餐栏的文本颜色。 这显然是一个问题,如果您的主要文字颜色是黑暗的。

在这里输入图像说明

有没有人知道解决这个问题的方法,或者如何为我的文本添加颜色?

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

虽然有一些自定义的解决方案可以解决以下问题,但为主题Snackbars提供正确的方法可能也不错。

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

但是,要将主题应用于Snackbar ,请参考第三方材料文档中的质量指南: http : //www.materialdoc.com/snackbar/ (按照程序化主题实现,不要依赖xml样式)

以供参考:

 // 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最后一个可靠的方法)。

或者,请参阅下面的答案,以获得类似或更快的答案来解决您的问题。

我知道这已经被回答了,但我发现的最简单的方法是使用HTML.fromHTML和使用字体标签

 Snackbar.make(view, Html.fromHtml("Tap to open").show() 

我在Android设计支持库的新function以及如何使用它的Snackbar中发现了这一点。

这对我改变一个小吃店的文字颜色。

 Snackbar snack = Snackbar.make(view, R.string.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(); 

好吧,我通过重新组织文本颜色的方式来修复它。

在我的灯光主题中,我将android:textColorPrimary设置为我想要的normal dark text ,并将android:textColor设置为white

我更新了所有的文本视图和按钮,以使android:textColor="?android:attr/textColorPrimary".

所以,因为小吃店从textColor绘制,我只是把我所有的其他文本设置为textColorPrimary

编辑1月2017年:———————————————- ——

所以正如评论所说,正如上面编辑过的原始问题所述,你可能不应该在主题中定义android:textColor ,因为这会改变主题中每个视图的文本颜色。

android.support.design.R.id.snackbar_text上黑客是脆弱的,一个更好或不那么hacky的做法将是:

 String snackText = getResources().getString(YOUR_RESOURCE_ID); SpannableStringBuilder ssb = new SpannableStringBuilder() .append(snackText); ssb.setSpan( new ForegroundColorSpan(Color.WHITE), 0, snackText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); Snackbar.make( getView(), ssb, Snackbar.LENGTH_SHORT) .show(); 

一种方法是使用跨度:

 final ForegroundColorSpan whiteSpan = new ForegroundColorSpan(ContextCompat.getColor(this, android.R.color.white)); SpannableStringBuilder snackbarText = new SpannableStringBuilder("Hello, I'm white!"); snackbarText.setSpan(whiteSpan, 0, snackbarText.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE); Snackbar.make(view, snackbarText, Snackbar.LENGTH_LONG) .show(); 

使用跨度,您还可以在一个小吃店内添加多种颜色和样式。 这是一个很好的指导:

https://androidbycode.wordpress.com/2015/06/06/material-design-snackbar-using-the-design-support-library/

我看到的唯一方法是使用getView()并通过其子进行循环。 我不知道它是否会起作用,而且看起来很糟糕。 我希望他们很快会添加一些关于这个问题的API。

 Snackbar snack = Snackbar.make(...); ViewGroup group = (ViewGroup) snack.getView(); for (int i = 0; i < group.getChildCount(); i++) { View v = group.getChildAt(i); if (v instanceof TextView) { TextView t = (TextView) v; t.setTextColor(...) } } snack.show(); 

这是我需要自定义颜色时使用的

  @NonNull public static Snackbar makeSnackbar(@NonNull View layout, @NonNull CharSequence text, int duration, int backgroundColor, int textColor/*, int actionTextColor*/){ Snackbar snackBarView = Snackbar.make(layout, text, duration); snackBarView.getView().setBackgroundColor(backgroundColor); //snackBarView.setActionTextColor(actionTextColor); TextView tv = (TextView) snackBarView.getView().findViewById(android.support.design.R.id.snackbar_text); tv.setTextColor(textColor); return snackBarView; } 

消耗为:

 CustomView.makeSnackbar(view, "Hello", Snackbar.LENGTH_LONG, Color.YELLOW,Color.CYAN).setAction("DO IT", myAction).show(); 

我改变了我的主题

 Theme.AppCompat.Light.NoActionBar 

 Theme.AppCompat.NoActionBar 

它的工作。试图使用简单的主题,而不是光或其他主题。

你可以使用这个库: https : //github.com/SandroMachado/restaurant

 new Restaurant(MainActivity.this, "Snackbar with custom text color", Snackbar.LENGTH_LONG) .setTextColor(Color.GREEN) .show(); 

免责声明:我做了图书馆。

创建了这个我在项目中使用的kotlin扩展函数:

 fun Snackbar.setTextColor(color: Int): Snackbar { val tv = view.findViewById(android.support.design.R.id.snackbar_text) as TextView tv.setTextColor(color) return this } 

像你这样的用法会期望:

Snackbar.make(view,R.string.your_string,Snackbar.LENGTH_LONG).setTextColor(Color.WHITE).show()

 Snackbar.make(getView(), "Hello", Snackbar.LENGTH_SHORT) .setActionTextColor(Color.WHITE) .show(); 

我有一个简单的代码,将有助于得到Snackbar的textview的一个实例,之后,你可以调用所有适用于textview的方法。

 Snackbar snackbar = Snackbar.make( ... ) // Create Snack bar snackbar.setActionTextColor(getResources().getColor(R.color.white)); //if you directly want to apply the color to Action Text TextView snackbarActionTextView = (TextView) snackbar.getView().findViewById( android.support.design.R.id.snackbar_action ); snackbarActionTextView.setTextColor(Color.RED); //This is another way of doing it snackbarActionTextView.setTypeface(snackbarActionTextView.getTypeface(), Typeface.BOLD); //Below Code is to modify the Text in Snack bar TextView snackbarTextView = (TextView) snackbar.getView().findViewById(android.support.design.R.id.snackbar_text); snackbarTextView.setTextSize( 16 ); snackbarTextView.setTextColor(getResources().getColor(R.color.white)); 

我也注意到了同样的问题。 感谢这里的答案,我创建了一个小class,可以帮助您更轻松地解决此问题,只需将其替换为:

 Snackbar.make(view, "Error", Snackbar.LENGTH_LONG).show(); 

有了这个:

 Snackbar2.make(view, "Error", Snackbar.LENGTH_LONG).show(); 

这是我的class级:

 public class Snackbar2 { static public Snackbar make(View view, int resid, int duration){ Snackbar result = Snackbar.make(view, resid, duration); process(result); return result; } static public Snackbar make(View view, String text, int duration){ Snackbar result = Snackbar.make(view, text, duration); process(result); return result; } static private void process(Snackbar snackbar){ try { View view1= snackbar.getView(); TextView tv = (TextView) view1.findViewById(android.support.design.R.id.snackbar_text); tv.setTextColor(Color.WHITE); }catch (Exception ex) { //inform about error ex.printStackTrace(); } } 

}