Style SnackBar in theme app

烈酒焚心 提交于 2019-12-03 23:33:36

问题


I need help. How can I change the design of the text in snackbar in styles app? The change in the code does not interest me. I found the following code. But it is not working for me. Why is that? My theme is derived from @style/Theme.AppCompat.Light.DarkActionBar". I would be very grateful for the help.

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

回答1:


you need this: 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>



回答2:


2018 new way :

https://materialdoc.com/components/snackbars-and-toasts/#with-code

// create instance

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

// set action button color

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

// get snackbar view

View snackbarView = snackbar.getView();

// change snackbar text color

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

// change snackbar background

snackbarView.setBackgroundColor(Color.MAGENTA);



回答3:


With the Material Components Library you can globally change the snackbar style in your app theme:

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

Note: snackbarStyle and snackbarButtonStyle require the version 1.1.0, snackbarTextViewStyle requires the version 1.2.0.

For example:

  <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">
      <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>




回答4:


I digged into Snackbar sources and here's what I found Snackbar background consists of 2 layers: base and overlay, which colors are mixing.

In order to specify these colors, just add to your Theme 2 parameters:

colorSurface - background color, default = 0xFFFFFFFF

colorOnSurface - overlay, default = 0xFF000000

So in a default case, with applied 0.8 alpha by default, a color we get is 0xFF333333, which is in a middle between white and black.

Have fun mixing and styling your Snackbar :)



来源:https://stackoverflow.com/questions/32425191/style-snackbar-in-theme-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!