从Android的文档中,样式资源只影响应用到的特定视图,而不影响子视图。相反,主题(实际上只是在活动或应用程序级别上应用的样式)会影响其领域内的一切。到目前一切尚好。
然而,我想知道的是,我是否可以应用一个“主题”,但只适用于我的UI的一个特定部分。例如,我使用一个LinearLayout作为一种特殊的智能条(将它看作一个美化的状态/工具栏),因此,我希望在其中添加的任何子视图都可以为tintColor属性使用一个特定的值。(所有子控件都具有通过getter/setter和相关属性定义的tint颜色。)
目前,这需要在添加tintColor属性时手动将它们添加到所有子属性中。当涉及到孙辈时,事情会变得更加复杂,而孙辈可能是包含的布局的一部分,等等。
我希望简单的‘主题’的基础LinearLayout,但我没有看到任何方法来实现这一点。所以能做到吗?
发布于 2017-08-01 17:57:34
在父视图上使用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中,我现在会看到以下内容:

https://stackoverflow.com/questions/45441142
复制相似问题