【问题标题】:How to extend API dependend theme entries in an existing theme?如何在现有主题中扩展 API 依赖主题条目?
【发布时间】:2021-01-20 11:10:08
【问题描述】:

在 Android 开发中,我可以提供 API depending resources。我有一个自定义主题,我想用两个 API 29 及更高版本的新条目扩展为 described here

所以文件/文件夹结构如下所示:

res
 - values
   - dark_theme.xml
   - light_theme.xml
 - values-v29
   - dark_theme.xml
   - light_theme.xml

如何在values-v29\dark_theme.xmlvalues\light_theme.xml 中扩展values\dark_theme.xmlvalues\light_theme.xml 中定义的主题(向现有主题添加项目)?

假设我想使用"edge-to-edge" feature 的新选项扩展浅色主题。我需要如何设置主题,这样我就不需要将原始主题中的所有值复制到-v29文件夹中的主题中?

values\light_theme.xml的内容:

<resources>
  <style name="AppTheme_Light" parent="BaseAppTheme">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_light</item>
    ...
  </style>
</resources>

在不处理整个主题的情况下,使用values 文件夹中主题中已定义的项目,文件需要看起来如何?

values-29\light_theme.xml的内容:

<style name="AppTheme_Light" parent="BaseAppTheme">
  
  ...How to ensure all the previous items defined in values\light_theme.xml are used here?
  
  <item name="android:navigationBarColor">@android:color/transparent</item>
  <item name="android:statusBarColor">@android:color/transparent</item>
</style>

【问题讨论】:

    标签: android android-theme


    【解决方案1】:

    Apparently似乎要走的路是使用XML属性

    tools:ignore="NewApi"
    

    来自tools namespace

    所以我在我的案例中使用了这些行,而不是复制我的样式文件:

    <style name="AppTheme_Light" parent="BaseAppTheme">
      
      ...
      
      <item name="android:navigationBarColor" tools:ignore="NewApi">@android:color/transparent</item>
      <item name="android:statusBarColor" tools:ignore="NewApi">@android:color/transparent</item>
    </style>
    

    如果这是一个好的做法,似乎“to depend”,因为我隐藏了可能不兼容的小部件属性。

    【讨论】:

      猜你喜欢
      • 2021-05-23
      • 2019-11-01
      • 1970-01-01
      • 2012-03-02
      • 1970-01-01
      • 2017-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多