【问题标题】:How to set a different theme to a Layout如何为布局设置不同的主题
【发布时间】:2013-09-01 16:38:35
【问题描述】:

我为整个应用设置了默认主题。在styles.xml中定义如下:

    <style name="DefaultTheme" parent="@android:style/Theme.Holo.Light">
        <!-- Customization here -->
    </style>

我还定义了一个深色主题:

    <style name="DarkTheme" parent="@android:style/Theme.Holo">
        <!-- Customization here -->
    </style>

在清单中,它被声明为应用程序的主主题:

    <application
    ...
    android:theme="@style/DefaultTheme" >

现在这工作正常,但在活动中,我需要为单个布局设置不同的主题。像这样的:

    +--------------------------------------------------+
    |         Parent Linear layout (default theme)     |
    |                                                  |
    | +------------------------------------+ +-------+ |
    | |                                    | |       | |
    | |     Left linear layout             | |       | |
    | |     (default theme)                | |       | |
    | |                                    | |       | |
    | |                                    | |       | |
    | |                                    | |    ·<----------- Right Linear Layout
    | |                                    | |       | |        (Need it in dark theme)
    | |                                    | |       | |
    | |                                    | |       | |
    | +------------------------------------+ +-------+ |
    +--------------------------------------------------+

在布局 xml 文件中,我正在尝试为最右边的子 LinearLayout 设置主题:

    <LinearLayout
    style="@style/DarkTheme">
    ...

我希望这可以正常工作,并将深色主题仅应用于正确的布局(及其子视图),但它不起作用。我尝试用内置的 @android:style 替换 @style 无济于事。我已经在布局编辑器和真实设备/模拟器上对此进行了测试。

是否可以将自定义主题或样式应用于单个布局?

【问题讨论】:

  • 我知道这是个老问题,你能找到答案吗?

标签: android user-interface styles themes android-linearlayout


【解决方案1】:

您可以在以编程方式创建布局时使用ContextThemeWrapper() 应用主题。

LinearLayout darkThemeLayout = new LinearLayout(new ContextThemeWrapper(context, R.style.DarkTheme));

【讨论】:

    【解决方案2】:

    现在可以通过在视图上使用android:theme 属性并将其设置为您喜欢的任何主题。请注意,子视图将继承其父视图的主题。

    【讨论】:

    • 从 Android Marshmallow 开始
    • 这对于一个公认的答案来说真的很模糊。我在stackoverflow.com/a/55973877/461982 提供了更全面的答案
    • 如何以编程方式完成?
    【解决方案3】:

    借助支持库,您可以做到:

    app:theme="R.style.MyTheme"
    

    【讨论】:

      【解决方案4】:

      API 24 添加了按视图主题参数,但需要 Marshmallow 或更高版本。
      另一种方法是使用 android 支持库以获得最大的兼容性。

      为视图设置支持库的主题参数

      <LinearLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          ...
      >
          <LinearLayout
              app:theme="R.style.MyTheme"
              ...
          >
              <!-- content-->
          </LinearLayout>
      ...
      </LinearLayout>
      

      它引用了在资源 xml 中定义为样式的主题

      <resources>
          <style name="MyTheme">
              <item name="colorPrimary">@color/colorPrimary</item>
              <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
              <item name="colorAccent">@color/colorAccent</item>
          </style>
      </resources>
      

      【讨论】:

      • 我也能够从 API 16 应用您的方法。我还没有尝试过它下面的任何 API,但无论如何都不限于 API 24。至少像我一样使用 androidx。
      • @RubénViguera 很高兴听到。我尽量避免在我的答案中使用第三方库,因为它们可能并不总是与 API 一起提供。谷歌有一个诀窍,就是在不考虑影响的情况下拒绝“测试”包。
      【解决方案5】:

      直接加这个就可以解决问题了。

            //  app:trackColorActive="#ff4500"
            //  app:trackColorInactive="#fbb999"
          
              android:theme="@style/Theme.MaterialComponents.DayNight"
      

      ..

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-08
        • 2018-07-03
        • 1970-01-01
        • 1970-01-01
        • 2018-06-25
        • 1970-01-01
        • 1970-01-01
        • 2019-10-26
        相关资源
        最近更新 更多