【问题标题】:Change the background color of action button in snackbar更改快餐栏中操作按钮的背景颜色
【发布时间】:2017-02-04 16:28:23
【问题描述】:

如何更改快餐栏中操作按钮的背景颜色或使其消失(灰色背景)?

我使用这个代码:

        Snackbar mysnack = Snackbar.make(main_layout, getResources().getString(R.string.snack_1), 5000);
            View view = mysnack.getView();
            TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
            tv.setTextColor(getResources().getColor(R.color.text_light));
            mysnack.setActionTextColor(getResources().getColor(R.color.text_light));
            mysnack.setAction("RATE", new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Uri uri = Uri.parse(getResources().getString(R.string.snack_url));
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                }
            });
            TypedValue typedValue = new TypedValue();
            getTheme().resolveAttribute(R.attr.colorPrimaryDark, typedValue, true);
            final int color = typedValue.data;
            mysnack.getView().setBackgroundColor(color);
            mysnack.show();

我的问题不是重复的。我要求背景颜色而不是文本颜色。首先我们阅读,然后理解,然后我们思考,然后我们决定写下某人的问题是重复的。

【问题讨论】:

标签: android background-color android-styles material-components-android android-snackbar


【解决方案1】:

我遇到了同样的问题。发现它可能是新材料主题的错误。 我的应用程序的主题是:

<style name="AppTheme" parent="@style/Theme.MaterialComponents.Light.NoActionBar">

如果我将其更改为 AppCompat 小吃按钮的灰色背景消失。最后我发现是bug的原因。

我的解决方案是(我需要 Material 主题,不能简单地将其更改为 AppCompat): 找到小吃的按钮 ID。它是“@id/snackbar_action”:

val snackButton: Button = yourSnackbar.getView().findViewById(R.id.snackbar_action)

然后将其背景更改为空:

snackButton.setBackground(null)

【讨论】:

  • 不错的解决方案?
【解决方案2】:

这段代码 sn-p 可以帮助你:

Snackbar snackbar = Snackbar.make(
                    coordinatorLayout,
                    "Snackbar: floatingActionButton1 (normal) clicked",
                    Snackbar.LENGTH_LONG);
            snackbar.setActionTextColor(Color.RED);
            View snackbarView = snackbar.getView();
            snackbarView.setBackgroundColor(Color.WHITE);
            TextView textView = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
            textView.setTextColor(Color.BLUE);

供参考Click Here

【讨论】:

    【解决方案3】:

    对于 Xamarin,你可以试试这个

     snackbar.View.SetBackgroundColor(Android.Graphics.Color.ParseColor("#32CD32"));
    

    【讨论】:

      【解决方案4】:

      如果你想改变动作按钮的背景颜色..

      View sbView = snackbar.getView();
      Button button=
      (Button) sbView.findViewById(com.google.android.material.R.id.snackbar_action);
      button.setBackgroundColor(getResources().getColor(R.color.white));
      

      如果你想改变动作按钮的文字颜色..

      snackbar.setActionTextColor(getResources().getColor(R.color.colorAccent));
      

      【讨论】:

        【解决方案5】:

        借助材质组件库,您可以在应用主题中使用 snackbarButtonStyle 属性。
        比如:

        <style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
            <!-- Style to use for action button within a Snackbar in this theme. -->
            <item name="snackbarButtonStyle">@style/Widget.App.Button.TextButton.Snackbar</item>
        </style>
        
        
        <style name="Widget.App.Button.TextButton.Snackbar" parent="Widget.MaterialComponents.Button.TextButton.Snackbar">
            <item name="backgroundTint">@color/colorSecondary</item>
        </style>
        

        【讨论】:

          猜你喜欢
          • 2014-07-01
          • 2017-11-10
          • 1970-01-01
          • 2015-02-25
          • 2014-11-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多