繁体   English   中英

从样式更改小吃店的动作颜色

[英]Change action color of snackbar from style

我需要AppTheme.colorAccent为棕色,但我需要我的Snackbar动作颜色为蓝色。 如何在不更改AppTheme.colorAccent情况下从样式更改Snackbar操作按钮颜色?

我试过这段代码,但它不起作用:

<style name="TextAppearance.Design.Snackbar" parent="android:TextAppearance" tools:override="true">
    <item name="colorAccent">#3097ff</item>
</style>

使用材料组件库,您可以做到。

只需在您的主题应用程序中添加snackbarButtonStyle属性。

<style name="AppTheme" parent="Theme.MaterialComponents.*">
    <!-- Style to use for action button within a Snackbar in this theme. -->
    <item name="snackbarButtonStyle">@style/snackbar_button</item>
    ...
</style>

然后定义您的自定义样式:

  <style name="snackbar_button" parent="@style/Widget.MaterialComponents.Button.TextButton.Snackbar">
      <item name="backgroundTint">@color/secondaryLightColor</item>
      <item name="android:textColor">@color/primaryDarkColor</item>
  </style>

在此处输入图片说明

它需要库的 1.1.0 版。

您可以定义在颜色colors.xml ,并在使用它snackbar ,如下所示:

val mySnackbar = Snackbar.make(findViewById(R.id.container),"Item added to cart.", Snackbar.LENGTH_SHORT)
mySnackbar.setAction("view cart", View.OnClickListener {/*action to be triggered*/  })
mySnackbar.setActionTextColor(/*color defined*/)
mySnackbar.show()

我在 Kotlin 中实现了这个。

您使用以下代码为小吃店设置背景颜色和动作文本颜色..

 btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar snackbar = Snackbar.make(
                    view,
                    "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);

            snackbar.setAction("OK", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(
                            getApplicationContext(),
                            "snackbar OK clicked",
                            Toast.LENGTH_LONG).show();
                }
            });

            snackbar.show();
        }
    });

暂无
暂无

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

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