style文件需要保存在res/values目录下,文件名任意,但是必须是xml文件,sytle文件的根标记必
须是。
Style是View中一些属性的集合,包括height,padding,font color,background等等,将设计和
内容分开,便于修改和重复使用。
例如:
声明style是CodeFont,对应的是style文件中的style name。mystyle.xml文件中定义了style
name是CodeFont:
fill_parent
wrap_content
#00FF00
monospace
/mce:style
1
<style name="CodeFont" parent="@android:style/TextAppearance.Medium"
mce_bogus="1">
fill_parent
wrap_content
#00FF00
monospace
parent属性表示style之间可以继承,同时可以覆盖parent style的一些属性。
style是针对view来说的,比如TextView,EditText这些,而theme必须针对整个activity或者整个
程序,你必须在AndroidManifest.xml中的或者中定义.
style继承有两种方式:
style的继承可以通过parent属性,用来继承android已经定义好的style,例如:
第一种:继承了android中定义好的属性,当然也可以同时覆盖了它的某些属性。
#00FF00
/mce:style
#00FF00
继承了android中的TextAppearance,同时覆盖了android:textColor属性。
第二种:继承自定义的style,不需要通过parent属性,只要继承的style的name开始后跟新的style
的name,中间用 . 隔开。注意:这种方式只适用与自定义的style继承。
#FF0000
/mce:style
#FF0000
新的style继承了CodeFont,在使用时这样写 style="@style/CodeFont.Red"
style可以多级继承:
30sp
/mce:style
30sp
sytle的更多属性见android包下的R.attr。需要注意,并不是所有的View都支持定义的style的属性
,如果自定义的sytle中包含View不支持的属性,程序会自动忽略它。
如果声明一个style作为Theme,需要配置mainfest文件中 或 的
android:theme 属性。将自定义的style作为application的theme:
在mainfest 的application中添加 android:theme属性:
其中CodeFont为style样式。
在每个标签中使用android:theme属性:
android:theme还可以配置android中已经存在的theme:
如果想调整android已经定义好的theme,则可以通过自定义style来实现,例如:
#b0b0ff
@color/custom_theme_color
@color/custom_theme_color
/mce:style
@color/custom_theme_color
@color/custom_theme_color
如果你要在java代码中加载主题的话,只要用setTheme(R.style.CustomTheme)就可以了,不过记
得一定要在初始化任何view之前,比如一定要放在我们常用的setContentView()之前。通常,我们不
建议这么做。
android 中theme.xml与style.xml的区别:
相同点:
两者的定义相同。
不同点:
一、使用的地方不同(整个应用或某个Activity存在全局性影响|单独的View)
二、 在R.attr定义中以window开头的一些属性只对theme有效。
三、如果一个应用使用了theme,同时应用下的view也使用了style,那么当theme与样式style发生冲