Styles and Themes

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


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值