自定义主题干扰了snackbar的背景颜色

14
尝试使用新的Design Support Library,我添加了一个snackbar;但与其主要背景不同,文本区域没有用默认值#323232着色。相反,它看起来像这样。它似乎从styles.xml中定义的自定义主题中的android:background值获取颜色,如下所示:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    ...
    <item name="android:background">#4f4f5e</item>
    ...
</style>

如果我试图强行着色它

View snackbarView = snackbar.getView(); 
snackbarView.setBackgroundColor(Color.YELLOW);

它只影响主要的背景,像这样,文本背景仍然会受到自定义主题的颜色影响。是否有一种方法既保留我的自定义主题,又拥有标准的 Snackbar?谢谢!

10个回答

18

要在代码中更改Snackbar的背景颜色,可以执行以下操作:

Snackbar snack = Snackbar.make(...);
ViewGroup group = (ViewGroup) snack.getView();
group.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.red));
snack.show();

你可以使用 Snackbar 默认的颜色(#323232)代替红色。


getColor()已被弃用。 - Toni Alvarez
3
如果您使用support4库,那么您可以使用ContextCompat.getColor(getContext(), R.color.colorRed)来获取颜色。 - SBotirov

7

Snackbar包含一个TextView,因此您需要同时更改Snackbar的背景色(就像您已经做过的那样)和TextView的背景色,代码如下:

View snackbarView = snackbar.getView(); 
TextView textView = (TextView)snackbarView.findViewById(android.support.design.R.id.snackbar_text); 
textView.setBackgroundColor(Color.YELLOW);

1
这个!唯一正确的答案,其他所有上面的回答都不理解冲突的原因,也没有明确说明如何更改文本背景!谢谢! - Kamajabu

7

.setBackgroundColor 允许你改变 snackbar 的背景颜色。

msnackBar.setBackgroundColor(Color.parseColor("#009688"));

或者

 msnackBar.setBackgroundColor(getResources().getColor(R.color.BLUE)););

这里是使用设计支持库来使用snackbar的完整教程。 点击这里查看。

2
您可以简单地创建自己的Snackbar类并模拟Snackbar的make方法。这样做,您只需使用此类代替Android的snackbar小部件即可。
Snackbar.class
import android.graphics.Color;
import android.support.annotation.IntDef;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.view.View;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

public class Snackbar {

    /** Snackbar's lengths **/
    public static final int LENGTH_SHORT = android.support.design.widget.Snackbar.LENGTH_SHORT;
    public static final int LENGTH_LONG = android.support.design.widget.Snackbar.LENGTH_LONG;
    public static final int LENGTH_INDEFINITE = android.support.design.widget.Snackbar.LENGTH_INDEFINITE;

    @NonNull
    public static android.support.design.widget.Snackbar make(@NonNull View view, @NonNull CharSequence text,
                                                              @Duration int duration) {
        android.support.design.widget.Snackbar snackbar = android.support.design.widget.Snackbar.make(view, text, duration);
        // TODO: This is where you have to customize your snackbar
        snackbar.getView().setBackgroundColor(Color.RED);
        return snackbar;
    }

    @NonNull
    public static android.support.design.widget.Snackbar make(@NonNull View view, @StringRes int resId, @Duration int duration) {
        return make(view, view.getResources().getText(resId), duration);
    }

    // Optional
    @IntDef({LENGTH_INDEFINITE, LENGTH_SHORT, LENGTH_LONG})
    @IntRange(from = 1)
    @Retention(RetentionPolicy.SOURCE)
    public @interface Duration {}

}

用途:

// WARNING: Make sure you're using your snackbar's package
import com.mypackage.custom_views.Snackbar;

public class MyActivity extends Activity {
    ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        Snackbar.make(view, R.string.my_msg, Snackbar.LENGTH_LONG).show();
    }
}

希望这能帮到你!

2

这里是一个完整的示例:

Snackbar snack = Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null);
                ViewGroup group = (ViewGroup) snack.getView();
                group.setBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.blue));
                snack.show();

MainActivity.this替换为您当前的活动或getAppContext()


2

当在样式属性android:background中设置时,就会发生这种情况。

当然,删除它会影响您应用程序中的所有布局,但是Snackbar将被修复。


1
你可以使用这个库:https://github.com/SandroMachado/restaurant
new Restaurant(MainActivity.this, "Snackbar with custom background and text color", Snackbar.LENGTH_LONG)
    .setBackgroundColor(Color.GRAY)
    .show();

免责声明:该库由我创建。

1
这是我如何使用自定义 Snackbar。
  Snackbar snackbar_network = Snackbar.make(rLayout, "Your Message", Snackbar.LENGTH_SHORT)
                        .setAction("EXIT", new View.OnClickListener() {
                            @Override
                            public void onClick(final View v) {


                                  finish();

                            }
                        });

动作文本颜色。
 snackbar_network.setActionTextColor(Color.RED);

动作消息文本颜色
  final View sbView = snackbar_network.getView();
                final TextView tv = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
                tv.setTextColor(Color.YELLOW);

设置 Snackbar 背景
sbView.setBackgroundColor(ContextCompat.getColor(MapsActivity.this, R.color.black));

        snackbar_network.show();

0

我也遇到了类似的问题,不幸的是没有一个解决方案对我有效。因此我写了自己的解决方案,其中还设置了父视图的背景颜色。

    TextView snackbarTextView = snackbar.getView().findViewById(android.support.design.R.id.snackbar_text);
    snackbarTextView.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary));

    ViewParent parentView = snackbarTextView.getParent();
    if (parentView instanceof View) {
        ((View) parentView).setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary));
    }

    View snackbarView = snackbar.getView();
    snackbarView.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary));

    snackbar.show();

0

对我来说是这样的:

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll);
        Snackbar snackbar = Snackbar.make(lineatLayout, "TEXT", Snackbar.LENGTH_LONG);
        ViewGroup group = (ViewGroup) snackbar.getView();
        group.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.yourColor));
        TextView textView = (TextView) group.findViewById(android.support.design.R.id.snackbar_text);
        textView.setTextColor(ContextCompat.getColor(this, R.color.yor collor));

        snackbar.show();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接