码迷,mamicode.com
首页 > 移动开发 > 详细

Android篇---Styles和Themes常见用法可能疑点小结

时间:2015-10-08 00:30:59      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:

1.style和theme的区别:

简而言之,style指的就是安卓中一个UI控件的样式,而themes指的是安卓中一个activity界面或者整个安卓应用整体的样式。theme的范围比style的范围大。

2.style的继承用法:(全由笔者根据官方文档亲测,可放心使用,注意:样式的定义都在/res/values/style.xml中,而样式的使用在activity的布局文件里)

  •   对于继承安卓原装style,用法如下代码块,代码的意思是将安卓系统自带的TextAppearance样式中的textColor属性改成绿色,其他的属性不变:
    <style name="GreenText" parent="@android:style/TextAppearance">
            <item name="android:textColor">#00FF00</item>
        </style>

     

    然后在UI控件中通过<style>标签的name属性的值引用该样式,例如,在<TextView>中引用上面定义的样式代码块如下:
    <TextView
            android:text="hello style"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/GreenText"
            />

     

  •   对于继承自定义的样式,有两种方式,例如如果我们已经有自定义好的父样式,代码如下:
    <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>

     

    第一种是跟上面方法一样的,用parent属性,后面指定父样式,例如:
    <style name="Red" parent="@style/CodeFont">
            <item name="android:textColor">#FF0000</item>
        </style>

     

    第二种是继承自定义父样式特有的方式,使用符号.表示继承关系。例如:
    <style name="CodeFont.Red">
            <item name="android:textColor">#FF0000</item>
        </style>

     

    样式的使用方法依然是在UI控件中通过<style>标签的name属性的值引用该样式,注意,名字是<style>标签中name属性的值

3.什么时候用@android:style什么时候用@style?

@android:style是引用安卓系统自带的样式的,而@style是引用我们自己在/res/values/styles.xml文件中的样式,实际原理是这样的,资源引用的格式是这样的:
@[package:]style/style_name

 

UI控件中引用样式时是根据name引用的,而不是xml的文件名,xml的文件名可以任意,但是为了看名知意约定为styles.xml里面定义样式。安卓原生的样式在名为android的包里,所以引用时写成了@android:style。

Android篇---Styles和Themes常见用法可能疑点小结

标签:

原文地址:http://www.cnblogs.com/superpang/p/4859799.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!