是否可以将一组样式应用于 ViewGroup 中的所有内容?

Is it possible to apply a set of styles to everything in a ViewGroup?

根据 Android 的文档,样式资源只影响它应用到的特定视图,而不影响其下的子视图。相比之下,主题(实际上只是一种应用于 activity 或应用程序级别的样式)会影响其域内的所有内容。到目前为止一切顺利。

然而我想知道的是,我是否可以应用 'theme' 但仅限于我的 UI 的特定部分。例如,我使用 LinearLayout 作为一种临时智能栏(将其视为美化的 status/toolbar),因此,我想要任何(孙)子视图在其中添加以使用 tintColor 属性的特定值。 (所有子控件都具有通过 getter/setter 和关联属性定义的色调。)

目前,这需要在添加时手动将 tintColor 属性添加到所有子项。当涉及到孙子时,事情会变得更加复杂,这可能是包含的布局的一部分,等等。

我希望简单地 'theme' 那个基本的 LinearLayout,但是我没有看到任何实现它的方法。那么可以吗?

在您的父视图上使用 android:theme 属性。

考虑这个示例布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text 1"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text 2"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text 3"/>

</LinearLayout>

这会产生以下预览:

但是,如果我将 android:theme="@style/BlueText" 添加到我的 LinearLayout,我现在会看到: