android:Style and Theme

如果视图界面风格需要统一的规划,就需要使用android视图技术中的style。style的做法,是将这些style内容写到单独的xml文件中,放置在res/values/styles.xml中:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <style name="itemTitle">
           <item name="android:textSize">25sp</item>
           <item name="android:textStyle">bold</item>
   </style>
</resources>

使用:在布局文件中

<TextView style="@style/itemTitle" ................/>

R.java中有

public static final class style {
public static final int itemTitle=0x7f050000;
}

 

 

如果要针对整个Activity,对它的背景颜色和字体等做统一的样式约定,就需要使用另外一个技术,theme。

首先,要编写theme文件,和style文件类似,是放在res/values目录下:(res/values/theme.xml)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="custom_background_color">#FFFFFFFF</color>
    <style name="RiverTheme" parent="android:Theme.Light">
        <item name="android:windowBackground">@color/custom_background_color</item>
    </style>
</resources>
 

这里继承了系统的theme.light,一般theme是继承的,这样可以对默认的风格不必重复定义。

本例定义了一个背景色。这里背景色要单独声明,不能在item元素中直接写颜色值,会提示语法错误。

引用该theme,在manifest文件中指定的Activity中:

<activity .............
   android:theme="@style/RiverTheme"

theme也可以在application中整体使用。。。

参考:http://marshal.easymorse.com/archives/4111

原文地址:https://www.cnblogs.com/mybkn/p/2491063.html