2017-08-01 23 views
1

从Android的文档中,样式资源只影响其应用于的特定视图,而不影响其下的子视图。相反,主题(这实际上只是一种适用于活动或应用程序级别的风格)会影响其域中的所有内容。到现在为止还挺好。是否可以将一组样式应用于ViewGroup中的所有内容?

但是我想知道的是,如果我可以应用“主题”,但仅适用于我的用户界面的特定部分。例如,我使用LinearLayout作为一种特别的Smart-Bar(将其视为一种荣耀的状态/工具栏),因此,我希望在其中添加任何(大)子视图以使用tintColor属性的具体值。 (所有子控件都有一个通过getter/setter和相关属性定义的色调颜色。)

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

我希望只是'主题'的基础LinearLayout,但我没有看到任何方式来实现这一点。那么可以这样做吗?

+0

据我所知它不能;风格仅适用于您分配给它的项目,而不是孩子。您可以为每个元素创建单独的样式并分配它们,还可以将tintColor存储在xml中以便重用它。 – RobVoisey

回答

1

在父视图中使用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> 

这将产生以下预览:

enter image description here

但是,如果我添加android:theme="@style/BlueText"LinearLayout,我现在看到这一点:

enter image description here

相关问题