首页 > 解决方案 > 材料设计排版和小吃店

问题描述

根据https://material.io/develop/android/theming/typography/,我一直在通过修改 textAppearanceX 属性来自定义我的应用程序的样式。有谁知道要自定义哪个属性来设置应用程序的 Snackbars 样式或记录在哪里?

谢谢你。

标签: androidmaterial-designandroid-themeandroid-snackbarmaterial-components-android

解决方案


使用材料组件库,您可以textAppearance通过应用程序主题自定义文本和按钮使用的内容:

只需使用snackbarButtonStyleandsnackbarTextViewStyle属性在应用程序中全局定义:

<style name="AppTheme" parent="Theme.MaterialComponents.*">

    <!-- Style to use for action button within a Snackbar in this theme. -->
    <item name="snackbarButtonStyle">@style/Custom.TextButton.Snackbar</item>

    <!-- Style to use for message text within a Snackbar in this theme. -->
    <item name="snackbarTextViewStyle">@style/Custom.Snackbar.TextView</item>
    ....
</style>

然后对于按钮android:textAppearance,您可以定义应用属性的自定义样式

  <style name="Custom.TextButton.Snackbar" parent="@style/Widget.MaterialComponents.Button.TextButton.Snackbar">
      <item name="android:textAppearance">@style/snackbar_button_textappearance</item>
  </style>

这只是一个例子:

  <style name="snackbar_button_textappearance"  parent="@style/TextAppearance.MaterialComponents.Button">
    <item name="android:textStyle">italic</item>
  </style>

对于文本,您可以执行类似的操作:

  <style name="Custom.Snackbar.TextView" parent="@style/Widget.MaterialComponents.Snackbar.TextView">
     <item name="android:textAppearance">......</item>
  </style>

在此处输入图像描述

注意

  • snackbarButtonStyle属性需要版本1.1.0
  • snackbarTextViewStyle属性需要版本1.2.0

推荐阅读