首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否可以将一组样式应用于ViewGroup中的所有内容?

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

Stack Overflow用户
提问于 2017-08-01 14:51:50
回答 1查看 384关注 0票数 2

从Android的文档中,样式资源只影响应用到的特定视图,而不影响子视图。相反,主题(实际上只是在活动或应用程序级别上应用的样式)会影响其领域内的一切。到目前一切尚好。

然而,我想知道的是,我是否可以应用一个“主题”,但只适用于我的UI的一个特定部分。例如,我使用一个LinearLayout作为一种特殊的智能条(将它看作一个美化的状态/工具栏),因此,我希望在其中添加的任何子视图都可以为tintColor属性使用一个特定的值。(所有子控件都具有通过getter/setter和相关属性定义的tint颜色。)

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

我希望简单的‘主题’的基础LinearLayout,但我没有看到任何方法来实现这一点。所以能做到吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-01 17:57:34

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

考虑下面的示例布局:

代码语言:javascript
复制
<?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中,我现在会看到以下内容:

​

​

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45441142

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档