Android - Styles and Themes

来源:互联网 发布:网络阅读的利与弊300 编辑:程序博客网 时间:2024/05/12 22:43

今天发现好像没什么写的了,仔细想想却发现有太多这东西需要了解。做安卓这么久,许多细节性的东西都没有仔细去学习,都是考用的时候再去搜索,api文档也没有看完,一是因为英文太差,二,也是最主要的原因,自己太懒了。好吧言归正传,为滥鱼充数,今天就扒API文档吧。


Style

A style is a collection of properties that specify the look and format for a View or window. A style can specify properties such as height, padding, font color, font size, background color, and much more. A style is defined in an XML resource that is separate from the XML that specifies the layout.

以上是定义,来自api文档。主要讲解方面

  • 是一个设定View和Window的显示和格式的属性集合
  • 定义在xml中

Style与Web中的css是类似的。用来将表现与内容分离。
按照设定级别主要分为两大类:
Theme - Window( to an entire Activity or application - application也指所有的Activity)
Style - View


为什么要使用Style

相信您只要做过任何一个稍微正规项目,你就会明白为什么要使用它。

  • 统一UI显示风格
    任何一个项目,其外大体风格是固定的。如果在布局中设定它的显示,经常会因为粗心导致一些小的显示问题,同样及代码重复也是很严重的问题。所以要将同样的代码抽离到Style
  • 便于UI修改
    如果有十几个按钮的样式是一样的,某一天,突然要求修改其样式,一个一个去复制粘贴,绝对是最蠢的方式。但如果你用了style,只需要修改一处,所有的位置都修改了。不会拉掉,不对改错。

Style定义

语法

<?xml version="1.0" encoding="utf-8"?><resources>    <style        name="style_name"        parent="@[package:]style/style_to_inherit">        <item            name="[package:]style_property_name"            >style_value</item>    </style></resources>

save an XML file in the res/values/ directory of your project.

<?xml version="1.0" encoding="utf-8"?><resources>    <style name="CodeFont" parent="@android:style/TextAppearance.Medium">        <item name="android:layout_width">fill_parent</item>        <item name="android:layout_height">wrap_content</item>        <item name="android:textColor">#00FF00</item>        <item name="android:typeface">monospace</item>    </style></resources>

Style继承

1. 继承自系统的Style

使用parent以及@android的命名空间

  <style name="GreenText" parent="@android:style/TextAppearance">        <item name="android:textColor">#00FF00</item>  </style>

2. 继承自自定义的Style

  • 使用.来继承
 <style name="CodeFont.Red">        <item name="android:textColor">#FF0000</item> </style>  <style name="CodeFont.Red.Big">        <item name="android:textSize">30sp</item>  </style>
  • 使用parent属性来继承
 <style name="Red" parent="@style/CodeFont">        <item name="android:textColor">#FF0000</item> </style>

Style属性

 Style基本可添加任何attr属性,包括自定义的.但是不一定所有的属性都会生效。如果添加的不支持属性,那么系统会自动忽略掉他,只要支持的属性生效。
 如果要查找系统定义的属性,可以在R.attr中找到。

比如:TextView支持的属性可以在TextView XML attributes中找到。

  • For Theme
    R.attr以Window开头的
  • For View
    去对应的XML attributes定义中查找。

使用Style

  • For Theme
    对activity或application中添加 android:theme属性.
<application android:theme="@style/CustomTheme"><activity android:theme="@android:style/Theme.Dialog">
  • For View
<TextView    style="@style/CodeFont"    android:text="@string/hello" />

不版本平台使用不同主题

通过自定义主题在不同的平台版本下,继承自不同的主题来实现

  • res/values
<style name="LightThemeSelector" parent="android:Theme.Light">    ...</style>
  • es/values-v11
<style name="LightThemeSelector" parent="android:Theme.Holo.Light">    ...</style>

如何玩转Style

想要最快的玩转它,除了抄袭就是模仿,嗯,有区别吗?
好吧!问题来了,在哪里看呢?
结果是

Android Styles (styles.xml)
Android Themes (themes.xml)

0 0
原创粉丝点击