简体   繁体   English

自定义主题干扰快餐栏背景颜色

[英]Custom theme interferes with snackbar background color

Trying out the new Design Support Library, I added a snackbar; 试用新的设计支持库,我添加了一个小吃吧; but unlike its main background, the text area is not colored with the default value of #323232 . 但与其主背景不同,文本区域未使用默认值#323232着色。 Instead, it looks like this . 相反,它看起来像这样 It seems to take its color from the android:background value defined in the custom theme in my styles.xml , which goes like this: 它似乎从我的styles.xml中的自定义主题中定义的android:background值中获取颜色,如下所示:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    ...
    <item name="android:background">#4f4f5e</item>
    ...
</style>

If I try to forcefully color it with 如果我试着用它强行着色

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

it only impacts the main background, like this , and the text background still gets colored by the custom theme. 它只影响主背景, 像这样 ,文本背景仍然被自定义主题着色。 Is there a way to both keep my custom theme, and have a standard snackbar? 有没有办法保持我的自定义主题,并有一个标准的小吃吧? Thanks! 谢谢!

To change the Snackbar's background colour you can do the following from code: 要更改Snackbar的背景颜色,您可以从代码中执行以下操作:

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

Instead of red you can use the Snackbar's default colour: #323232 您可以使用Snackbar的默认颜色代替红色:#323232

.setBackgroundColor allows you to change background color of snackbar .setBackgroundColor允许您更改snackbar的背景颜色

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

or 要么

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

Here is complete tutorial to use snackbar using design support library. 是使用设计支持库使用snackbar的完整教程。

The snackbar contains a TextView, so you need to change the background color for both, the snackbar the way you already did, and then the TextView like this: 小吃店包含一个TextView,所以你需要改变两者的背景颜色,就像你已经做的那样改变小吃吧,然后像这样改变TextView:

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

Here is a complete sample: 这是一个完整的示例:

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

replace MainActivity.this with your currently activity or getAppContext() MainActivity.this替换为您当前的活动或getAppContext()

You can simply create your own Snackbar class and simulate Snackbar's make method. 您可以简单地创建自己的Snackbar类并模拟Snackbar的make方法。 Doing this, you just have to use this class instead of android's snackbar widget. 这样做,你只需要使用这个类而不是android的snackbar小部件。

Snackbar.class 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 {}

}

Use: 使用:

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

Hope this helps! 希望这可以帮助!

This effect happens when in style attribute android:background is set. 在样式属性android:background中设置时会发生此效果。

Removing that will of course affect all layouts in your application but snackbar will be fixed. 删除它当然会影响应用程序中的所有布局,但小吃栏将被修复。

You can use this library: https://github.com/SandroMachado/restaurant 您可以使用此库: https//github.com/SandroMachado/restaurant

new Restaurant(MainActivity.this, "Snackbar with custom background and text color", Snackbar.LENGTH_LONG)
    .setBackgroundColor(Color.GRAY)
    .show();

Disclaimer: I made the library. 免责声明:我制作了图书馆。

this is how i'm using custom 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();

                            }
                        });

Action Text Color 动作文字颜色

 snackbar_network.setActionTextColor(Color.RED);

Action Message Text Color 动作消息文本颜色

  final View sbView = snackbar_network.getView();
                final TextView tv = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
                tv.setTextColor(Color.YELLOW);

Set Snackbar Background 设置小吃背景

sbView.setBackgroundColor(ContextCompat.getColor(MapsActivity.this, R.color.black));

        snackbar_network.show();

Works this way for me: 以这种方式为我工作:

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

I also faced similar issue & unfortunately no solution works for me Hence I write my own solution where I set background color for parent view too. 我也面临类似的问题,不幸的是没有解决方案适合我。因此我编写自己的解决方案,我也为父视图设置背景颜色。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM