【问题标题】:Style SnackBar in theme app主题应用程序中的样式 SnackBar
【发布时间】:2015-12-02 05:08:02
【问题描述】:

我需要帮助。如何在样式应用程序中更改快餐栏中的文本设计?我对代码的更改不感兴趣。我找到了以下代码。但这对我不起作用。这是为什么?我的主题源自@style/Theme.AppCompat.Light.DarkActionBar"。非常感谢您的帮助。

<style name="TextAppearance.Design.Snackbar.Message" parent="android:TextAppearance">
        <item name="android:textSize">10sp</item>
        <item name="android:textColor">#FEFEFE</item>
    </style>
    <style name="TextAppearance.Design.Snackbar.Action" parent="android:TextAppearance">
        <item name="android:textSize">16sp</item>
        <item name="android:textColor">#FEFEFE</item>
    </style>

【问题讨论】:

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


【解决方案1】:

使用Material Components Library,您可以全局更改应用主题中的小吃栏样式:

<style name="AppTheme" parent="Theme.MaterialComponents.*">
    <!-- Style to use for Snackbars in this theme. -->
    <item name="snackbarStyle">@style/Widget.MaterialComponents.Snackbar</item>
    <!-- Style to use for action button within a Snackbar in this theme. -->
    <item name="snackbarButtonStyle">@style/Widget.MaterialComponents.Button.TextButton.Snackbar</item>
    <!-- Style to use for message text within a Snackbar in this theme. -->
    <item name="snackbarTextViewStyle">@style/Widget.MaterialComponents.Snackbar.TextView</item>
    ....
</style>

注意

  • snackbarStylesnackbarButtonStyle 需要版本 1.1.0
  • snackbarTextViewStyle 需要 1.2.0 版本。

例如:

  <style name="snackbar_style" parent="@style/Widget.MaterialComponents.Snackbar">
    <item name="android:layout_margin">32dp</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>

  <style name="snackbar_text" parent="@style/Widget.MaterialComponents.Snackbar.TextView">
    <item name="android:textColor">@color/secondaryLightColor</item>
  </style>

这只是一个如何更改操作按钮的父样式的示例。例如,您可以使用标准的Widget.MaterialComponents.Button

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

要更改SnackBar背景颜色,您可以使用:

<style name="snackbar_style" parent="@style/Widget.MaterialComponents.Snackbar">
    <!-- using backgroundTint the alpha layer is ignored -->
    <item name="backgroundTint">@color/....</item>
</style>

或者,如果您愿意:

   <style name="MySnackbar" parent="@style/Widget.MaterialComponents.Snackbar">
        <item name="materialThemeOverlay">@style/snackbar_overlay</item>
        <!-- If you want to avoid the alpha level for the color that is overlaid on top of the background color-->
        <item name="backgroundOverlayColorAlpha">1.0</item>
    </style>
    <style name="snackbar_overlay">
        <item name="colorOnSurface">....</item>
    </style>

【讨论】:

  • 太棒了!!。最后在主题级别完全设计小吃店。玩弄上面的建议,我发现我们可以在小吃店样式中指定自定义背景形状/颜色。
  • 在应用snackbar_button时,颜色确实发生了变化。但是颜色稍微亮一点,用颜色选择器检查发现颜色不是选择的颜色。
  • 感谢超级详细的帖子。救生员
【解决方案2】:

你需要这个:tools:override="true"

<resources xmlns:tools="http://schemas.android.com/tools">
    <style name="TextAppearance.Design.Snackbar.Message" parent="android:TextAppearance" tools:override="true">
        <item name="android:textColor">@color/text</item>
        <item name="android:textSize">50sp</item>
    </style>
</resources>

【讨论】:

  • 这是目前在 XML 中设置样式的唯一方法。有一个 bug report 可以正确设置样式。
【解决方案3】:

请参阅此link 了解更多信息:

// 创建实例

Snackbar snackbar = Snackbar.make(view, text, duration);

//设置动作按钮颜色

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

// 获取小吃店视图

View snackbarView = snackbar.getView();

// 改变snackbar文本颜色

int snackbarTextId = android.support.design.R.id.snackbar_text;
TextView textView = (TextView)snackbarView.findViewById(snackbarTextId);
textView.setTextColor(getResources().getColor(R.color.indigo));

// 更改小吃店背景

snackbarView.setBackgroundColor(Color.MAGENTA);

【讨论】:

  • 如果您使用新的材质组件,请使用 com.google.android.material.R.id.snackbar_text 而不是 android.support.design.R.id.snackbar_text
【解决方案4】:

感谢shadowsheep 我用Material Components 编写了这些样式。我还删除了边距。你可以编译他的app来研究Snackbar

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <!-- Snackbar -->
    <item name="snackbarStyle">@style/MaterialSnackbarTheme</item>
    <item name="snackbarButtonStyle">@style/MaterialSnackbarTextButtonTheme</item>
    <item name="snackbarTextViewStyle">@style/MaterialSnackbarTextViewTheme</item>
</style>

<style name="MaterialSnackbarTheme" parent="@style/Widget.MaterialComponents.Snackbar">
    <!-- <item name="backgroundTint">#00cc77</item>-->
    <!-- <item name="android:background">@drawable/snackbar_background</item>-->
    <item name="android:background">#00cc77</item>
    <item name="cornerRadius">0dp</item>
    <item name="android:layout_margin">0dp</item>
    <item name="actionTextColorAlpha">1.0</item>
</style>

<style name="MaterialSnackbarTextButtonTheme" parent="@style/Widget.MaterialComponents.Button.TextButton.Snackbar">
    <item name="backgroundTint">#7777ff</item>
    <item name="android:textColor">#ffffff</item>
</style>

<style name="MaterialSnackbarTextViewTheme" parent="@style/Widget.MaterialComponents.Snackbar.TextView">
    <item name="android:textColor">#ffffff</item>
    <item name="android:alpha">1.0</item>
</style>

drawable/snackbar_background.xml 在哪里:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    >
    <solid android:color="#00cc77" />
</shape>

如果您已添加,请不要忘记从 Snackbar.make() 中删除:

view.setBackgroundColor(ContextCompat.getColor(context, R.color.bg_color))
setActionTextColor(ContextCompat.getColor(context, R.color.button_color))

AlertDialog 不同,SnackbarAppTheme 中包含snackbarButtonStylesnackbarTextViewStyle 设置(这很奇怪,因为昨天它们在MaterialSnackbarTheme 中运行良好)。

正如@StayCool 在 cmets 中所说,Snackbar 目前使用透明度作为背景和文本颜色 (alpha = 0.5 - 0.6)。他们还添加了圆角和边距。要删除背景透明度,请使用 &lt;item name="actionTextColorAlpha"&gt;1.0&lt;/item&gt; 或 drawable/snackbar_background.xml。你可以看到他的variant

【讨论】:

  • 如何仅覆盖小吃店的主要/强调色?我尝试了您的解决方案,但 @color/white 对按钮 textColor 不起作用。也许是 ThemeOverlay?
  • @StayCool,我刚刚检查了我的示例,这两种颜色不会影响 SnackBar。我改变了它们,没有任何改变。你能问一个问题吗?
  • @StayCool 关于背景颜色不正确。背景颜色基于colorOnSurface 您可以在您的风格中使用&lt;item name="materialThemeOverlay"&gt;@style/...&lt;/item&gt; 覆盖颜色(您可以在上面查看我的答案)。使用backgroundTint alpha 层被忽略。在任何情况下,您都可以使用 attr backgroundOverlayColorAlpha 覆盖它。最后你应该避免使用android:background
  • @StayCool 使用 backgroundOverlayColorAlpha 和 backgroundTint 您不能使用渐变(与材质组件库中的所有其他组件一样)。
  • @StayCool 目前是唯一的方法。
【解决方案5】:

我深入研究了 Snackbar 的来源,这就是我发现的 Snackbar 背景由 2 层组成:base 和 overlay,它们的颜色是混合的。

为了指定这些颜色,只需添加到您的主题 2 参数:

colorSurface - 背景颜色,默认 = 0xFFFFFFFF

colorOnSurface - 叠加,默认 = 0xFF000000

所以在默认情况下,默认应用 0.8 alpha,我们得到的颜色是 0xFF333333,介于白色和黑色之间。

尽情享受混合和造型你的小吃吧:)

【讨论】:

    猜你喜欢
    • 2017-05-08
    • 2010-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-21
    相关资源
    最近更新 更多