现在的位置: 首页 > 综合 > 正文

Styles and Themes

2018年02月10日 ⁄ 综合 ⁄ 共 1562字 ⁄ 字号 评论关闭

1.概述

style是属性的集合,用来指定view或者window的外观和版式。style可以指定诸如高度,填充,字体颜色,字体大小,背景颜色等属性。style定义在与layout文件分开的xml资源文件中。

例如可以如下使用style:

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#00FF00"
    android:typeface="monospace"
    android:text="@string/hello" />

使用style之后:

<TextView
    style="@style/CodeFont"
    android:text="@string/hello" />

所有的style属性都被从layout xml文件中移除,然后写到一个style xml文件中,命名为:CodeFont

theme是一种style,运用在所有的activity或者application。

2.创建style

在工程目录res/values/下,创建一个文件,名字随意,保存style属性集。xml文件的根节点必须是<resources>

例子如下:

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

在编译的时候<resources>的子元素都会被转换成一个application资源对象。可以通过name来引用。parent属性可选,可以指定为另一个style文件的id。

3.继承

<style>元素中的parent属性可以指定一个style,我们的定义的style继承于这个style。可以使用这一属性继承已有的style,而只用定义或者改变部分的属性。

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

比如上面例子,继承了TextAppearance,修改了textColor属性。

如果是使用继承自己定义的style,就没有必要使用parent属性了。只需要在你的新style的名字前加上已定义的style的名字。例子如下:

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

4.style的属性值

现在已经知道如何定义一个属性文件了,下面来看看在style文件中可以定义那些属性item。查看指定view的属性的最好的方法就是查看class 参考文档。比如TextView所有可利用的属性都在TextView
XML attributes
列出来了。

参考文档http://developer.android.com/guide/topics/ui/themes.html#PlatformStyles

【上篇】
【下篇】

抱歉!评论已关闭.