如何将支持库snackbar文本颜色设置为android:textColor以外的其他东西?

问题描述 投票:79回答:17

所以我开始在设计支持库中使用新的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布局,请参阅上面的链接。如果这种方法感觉过于hacky并且您希望通过可能的支持库更新获得自定义Snackbar的可靠方法,请执行此操作。)

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

android android-support-library android-design-library snackbar
17个回答
35
投票

我知道这已经回答了,但我找到的最简单的方法是使用Html.fromHtml方法和font标签直接在make中

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

2
投票

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

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

2
投票

我改变了主题

Theme.AppCompat.Light.NoActionBar

Theme.AppCompat.NoActionBar 

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


2
投票

如果你决定使用脏和hacky解决方案,通过id在Snackbar中找到TextView,你已经迁移到androidx,那么这里是代码:

val textViewId = com.google.android.material.R.id.snackbar_text
val snackbar = Snackbar.make(view, "Text", Snackbar.LENGTH_SHORT)
val textView = snackbar.view.findViewById(textViewId) as TextView
textView.setTextColor(Color.WHITE)

1
投票

您可以使用此库:https://github.com/SandroMachado/restaurant

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

免责声明:我制作了图书馆。


0
投票

我有一个简单的代码,有助于获取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));

0
投票

为了节省您宝贵的开发时间,以下是我使用的静态方法:

public static void snack(View view, String message) {
    if (!TextUtils.isEmpty(message)) {
        Snackbar snackbar = Snackbar.make(view, message, Snackbar.LENGTH_SHORT);
        snackbar.getView().setBackgroundColor(Color.YELLOW);
        TextView tv =  snackbar.getView().findViewById(android.support.design.R.id.snackbar_text); //snackbar_text
        tv.setTextColor(Color.BLACK);
        snackbar.show();
    }
}

这是它的样子:

yellow snackbar with black text


0
投票

如果您在Kotlin,您可以创建一个扩展:

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

用法:

yourSnackBar.withTextColor(Color.WHITE).show()

0
投票

按ID查找并不适合我,所以我找到了另一个解决方案:

Snackbar snackbar = Snackbar.make(view, text, duration);//just ordinary creation

ViewGroup snackbarView = (ViewGroup) snackbar.getView();
SnackbarContentLayout contentLayout = (SnackbarContentLayout) snackbarView.getChildAt(0);
TextView tvText = contentLayout.getMessageView();
tvText.setTextColor(/*your color here*/);

//set another colors, show, etc

0
投票

根据新的AndroidX Jitpack组件

implementation 'com.google.android.material:material:1.0.0'

使用我创建的这个扩展

inline fun View.snack(message: String, length: Int = Snackbar.LENGTH_LONG,
 f: Snackbar.() -> Unit) {
val snack = Snackbar.make(this, message, length)
snack.f()
snack.show()
}

fun Snackbar.action(action: String, actionColor: Int? = null, textColor: Int? = null, listener: (View) -> Unit) {
setAction(action, listener)
actionColor?.let {
    setActionTextColor(it)
    }
textColor?.let {
    this.view.findViewById<TextView>(R.id.snackbar_text).setTextColor(it)
    }
}

像这样使用它

btn_login.snack(
        getString(R.string.fields_empty_login),
        ContextCompat.getColor(this@LoginActivity, R.color.whiteColor)
    ) {
        action(getString(R.string.text_ok), ContextCompat.getColor(this@LoginActivity, R.color.gray_300),ContextCompat.getColor(this@LoginActivity, R.color.yellow_400)) {
            [email protected]()
        }
    }

-1
投票

我也注意到了同样的问题。感谢这里的答案我已经创建了一个小类,它可以帮助更容易地解决这个问题,只需替换它:

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

有了这个:

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

这是我的班级:

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

}


147
投票

我在What are the new features of Android Design Support Library and how to use its Snackbar?找到了这个

这对我来说更改了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();

更新:ANDROIDX:正如dblackker在评论中指出的那样,使用新的AndroidX支持库,找到Snackbar TextView ID的代码更改为:

TextView tv = snack.findViewById(com.google.android.material.R.id.snackbar_text);

15
投票

创建了我在我的项目中使用的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()


13
投票

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

13
投票

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

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

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

所以因为snackbar来自textColor,我只是把我所有的其他文本都设置为textColorPrimary

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

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


10
投票

一种方法是使用跨度:

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

使用跨度,您还可以在一个Snackbar中添加多种颜色和样式。这是一个很好的指南:

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


9
投票

如果您迁移到androidX,请使用com.google.android.material.R.id.snackbar_text而不是android.support.design.R.id.snackbar_text来更改snackbar上的文本颜色。


6
投票

我看到的唯一方法是使用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();

6
投票

如果要将代码迁移到AndroidX,TextView属性现在是:

com.google.android.material.R.id.snackbar_text
© www.soinside.com 2019 - 2024. All rights reserved.