styles.xml和themes.xml之间有什么区别

时间:2013-03-02 03:40:24

标签: android android-theme android-styles

我可以知道styles.xmlthemes.xml之间有什么区别吗?对我来说,它们看起来一样,因为两种XML格式相同。

<style name="...
    <item name="...

所以,在我的应用程序中提供自定义着色,大小,可绘制,......我是否同时需要styles.xmlthemes.xml?我该如何决定将哪个XML放在哪个文件中?

6 个答案:

答案 0 :(得分:79)

Styles and Themes的整个页面。你可能正在寻找这条线。

  

将样式应用于布局中的单个视图时,属性   由样式定义的仅应用于该视图。如果是一种风格   应用于ViewGroup,子视图元素将不会继承   style属性 - 仅直接应用的元素   style将应用其属性。但是,您可以应用样式   它适用于所有View元素 - 将样式应用为主题。

当您作为主题应用时,它会更改范围内的所有内容,具体取决于您是否将其应用于活动或应用程序。风格更“本地化”。

答案 1 :(得分:26)

引自Android API指南:

  

要创建一组样式,请将XML文件保存在项目的res/values/目录中。 XML文件的名称是任意的,但必须使用.xml扩展名并保存在res/values/文件夹中。   XML文件的根节点必须为<resources>

Full documentation

所以我想如果你在任何文件中放置任何样式都没关系,只要它是位于res/values/文件夹中的xml文件。

答案 2 :(得分:12)

styles.xml和themes.xml之间没有功能差异,正如许多答案所示。

值得注意的是,Google的iosched2014应用只有一个styles.xml(没有themes.xml)。

https://github.com/google/iosched

答案 3 :(得分:4)

取自Styles and Themes文档,在“定义样式”部分中:

  

要创建一组样式,请将XML文件保存在项目的res / values /目录中。 XML文件的名称是任意的,但必须使用.xml扩展名并保存在res / values /文件夹中。

The root node of the XML file must be <resources>.

答案 4 :(得分:0)

我只是意识到例如工具栏文本颜色进入主题声明,而其他方面则进入样式声明

<style name="ToolbarTheme" parent="ThemeOverlay.AppCompat.Light">
    <item name="android:textColorPrimary">@android:color/white</item>
    <item name="android:textColorSecondary">@android:color/white</item>
</style>

<style name="ToolbarStyle" parent="Base.Widget.AppCompat.Toolbar">
    <item name="android:minHeight">?attr/actionBarSize</item>
    <item name="android:background">@color/colorPrimary</item>
</style>

在工具栏声明中

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/ToolbarStyle"
    android:theme="@style/ToolbarTheme">
</android.support.v7.widget.Toolbar>

我试图通过将其项目移动到样式中来省略主题,但这不起作用。所以在这种情况下,似乎这些只是作为主题的样式,因为它们影响工具栏中的继承子项,例如textfields。

答案 5 :(得分:-4)

嗨,这里有一篇关于样式和主题XML之间差异的文章在android please go through

此处还有android documentation ,关于Android中的样式和主题。