【问题标题】:Custom theme interferes with snackbar background color自定义主题会干扰小吃店背景颜色
【发布时间】:2015-09-13 10:56:22
【问题描述】:

试用新的设计支持库,我添加了一个快餐栏;但与主背景不同的是,文本区域没有使用默认值#323232 着色。相反,它看起来像like this。它的颜色似乎来自我的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);

它只影响主背景like this,并且文本背景仍然会被自定义主题着色。有没有办法既保留我的自定义主题,又拥有标准的小吃店?谢谢!

【问题讨论】:

    标签: android material-design android-design-library androiddesignsupport android-snackbar


    【解决方案1】:

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

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

    您可以使用 Snackbar 的默认颜色来代替红色:#323232

    【讨论】:

    • getColor() 已弃用。
    • 如果你使用 support4 库,那么你可以使用 ContextCompat.getColor(getContext(), R.color.colorRed);
    【解决方案2】:

    .setBackgroundColor 可以让你改变snackbar的背景颜色

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

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

    Here 是使用设计支持库使用snackbar 的完整教程。

    【讨论】:

      【解决方案3】:

      snackbar 包含一个 TextView,因此您需要更改两者的背景颜色,snackbar 就像您已经做的那样,然后是 TextView,如下所示:

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

      【讨论】:

      • ^这个!唯一正确的答案,上面的每个人都不明白是什么导致了冲突,也没有明确说要更改文本背景!谢谢!
      【解决方案4】:

      当设置了样式属性android:background时会发生这种效果。

      删除它当然会影响应用程序中的所有布局,但快餐栏将得到修复。

      【讨论】:

        【解决方案5】:

        这是一个完整的示例:

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

        【讨论】:

          【解决方案6】:

          您可以简单地创建自己的 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();
              }
          }
          

          希望这会有所帮助!

          【讨论】:

            【解决方案7】:

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

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

            免责声明:我制作了这个库。

            【讨论】:

              【解决方案8】:

              这就是我使用自定义小吃店的方式

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

              【讨论】:

                【解决方案9】:

                对我来说是这样的:

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

                【讨论】:

                  【解决方案10】:

                  我也遇到了类似的问题,不幸的是没有适合我的解决方案因此我编写了自己的解决方案,我也为父视图设置了背景颜色。

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

                  【讨论】:

                    猜你喜欢
                    • 2018-06-19
                    • 2016-03-05
                    • 2021-12-04
                    • 2022-11-06
                    • 2013-05-12
                    • 1970-01-01
                    • 2011-09-12
                    • 1970-01-01
                    • 2013-03-11
                    相关资源
                    最近更新 更多