每天读文档

来源:互联网 发布:上古卷轴5大叔捏脸数据 编辑:程序博客网 时间:2024/06/06 07:13

Styles and Themes

设计初衷:they allow you to separate the design from the content.

All of the attributes related to style have been removed from the layout XML and put into a style definition
将属性从layout移出来,组织成style再使用

Defining Styles

<?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>

规范1:To create a set of styles, save an XML file in the res/values/ directory of your project. The name of the XML file is arbitrary, but it must use the.xml extension and be saved in the res/values/ folder

必须为放到指定文件目录res/values/下的xml文件,文件名任意

规范2:The root node of the XML file must be <resources>.For each style you want to create, add a<style> element to the file with a name that uniquely identifies the style (this attribute is required). Then add an<item> element for each property of that style, with a name that declares the style property and a value to go with it (this attribute is required). The value for the<item> can be a keyword string, a hex color, a reference to another resource type, or other value depending on the style property. Here's an example file with a single style:

用<resources>作为文件的根节点,用<style>作为一个style的跟节点,内部定义若干<item>键值对,值可以为字符串,16进制颜色或者其他的资源文件

Inheritance

注意1:You can use this to inherit properties from an existing style and then define only the properties that you want to change or add.

可以继承父类主题然后只修改你想修改的属性

注意2:If you want to inherit from styles that you've defined yourself, you do not have to use theparent attribute.

继承自己的主题不需要写parent

<style name="CodeFont.Red">        <item name="android:textColor">#FF0000</item>    </style>

You can't inherit Android built-in styles this way

但是仅限于继承你自定义的主题


Style Properties

规范1:The best place to find properties that apply to a specific View is the corresponding class reference, which lists all of the supported XML attributes
查看控件什么属性可以修改最好的办法就是看看这个控件xml中有什么属性是支持设置的
规范2:Keep in mind that all View objects don't accept all the same style attributes, so you should normally refer to the specificView class for supported style properties. However, if you apply a style to a View that does not support all of the style properties, the View will apply only those properties that are supported and simply ignore the others.
并不是所有属性在各个控件中都是以通用的,最佳实践针对特定的控件定义style,因为控件只认识他有的属性,忽略他没有的
规范3:Some style properties, however, are not supported by any View element and can only be applied as a theme. These style properties apply to the entire window and not to any type of View.To discover these theme-only style properties, look at theR.attr reference for attributes that begin withwindow.
window开头的属性只支持主题不支持单独控件

Applying Styles and Themes to the UI

规范1:If a style is applied to a ViewGroup, the childView elements willnot inherit the style properties—only the element to which you directly apply the style will apply its properties. However, youcan apply a style so that it applies to all View elements—by applying the style as a theme.
view会继承上层style
规范2:To apply a style definition as a theme, you must apply the style to an Activity or application in the Android manifest.
主题只能应用于activity和application

Select a theme based on platform version

规范1:To have this theme use the newer holographic theme when the application is running on Android 3.0 (API Level 11) or higher, you can place an alternative declaration for the theme in an XML file inres/values-v11, but make the parent theme the holographic theme:
<style name="LightThemeSelector" parent="android:Theme.Light">    ...</style><style name="LightThemeSelector" parent="android:Theme.Holo.Light">    ...</style>

不同的文件夹(按一定规则命名),相同的style名,不同的父类。来实现在不同系统版本上的自动切换



0 0
原创粉丝点击