Style:

Style是View中一些属性的集合,包括height,padding,font color,background等等,Style单独定义在xml文件中,类似与web页面中css的角色,将设计和内容分开,便于修改和重复使用。

定义Style:

style文件需要保存在res/values目录下,文件名任意,但是必须是xml文件,sytle文件的根标记必须是<resources>。写了一个简单示例,效果如下:

程序目录结构如下图,其中mystyle.xml是自定义的style文件。

main.xml文件代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <TextView
  7. style="@style/CodeFont"
  8. android:text="测试style">
  9. </TextView>
  10. </LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
style="@style/CodeFont"
android:text="测试style">
</TextView>
</LinearLayout>

声明style是CodeFont,对应的是style文件中的style name。mystyle.xml文件中定义了style name是CodeFont:

parent属性表示style之间可以继承,同时可以覆盖parent style的一些属性。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <style name="CodeFont" parent="@android:style/TextAppearance.Medium">
  4. <item name="android:layout_width">fill_parent</item>
  5. <item name="android:layout_height">wrap_content</item>
  6. <item name="android:textColor">#00FF00</item>
  7. <item name="android:typeface">monospace</item>
  8. </style>
  9. </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>

Style的继承:

style继承有两种方式:

  • style的继承可以通过parent属性,用来继承android已经定义好的style,例如:
  1. <style name="GreenText" parent="@android:style/TextAppearance">
  2. <item name="android:textColor">#00FF00</item>
  3. </style>

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

继承了android中的TextAppearance,同时覆盖了android:textColor属性。

  • 如果要继承自定义的style,不需要通过parent属性,只要style的name以需要继承的style的name开始后跟新的style的name,中间用“.”隔开。注意:这种方式只适用与自定义的style继承 。
  1. <style name="CodeFont.Red">
  2. <item name="android:textColor">#FF0000</item>
  3. </style>

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

新的style继承了CodeFont,则在修改上边例子中的main.xml为:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <TextView
  7. style="@style/CodeFont.Red"
  8. android:text="测试style">
  9. </TextView>
  10. </LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
style="@style/CodeFont.Red"
android:text="测试style">
</TextView>
</LinearLayout>

效果如下,字体颜色变为了红色:

style也可以多级继承:

  1. <style name="CodeFont.Red.Big">
  2. <item name="android:textSize">30sp</item>
  3. </style>

<style name="CodeFont.Red.Big">
<item name="android:textSize">30sp</item>
</style>

字号变大,效果如下:

sytle的更多属性见android包下的R.attr。需要注意,并不是所有的View都支持定义的style的属性,如果自定义的sytle中包含View不支持的属性,程序会自动忽略它。

Theme:

如果声明一个style作为Theme,需要配置mainfest文件中<activity> 或 <application>的android:theme 属性。

将自定义的style作为application的theme:

修改mystyle.xml为:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <style name="CodeFont">
  4. <item name="android:textSize">20sp</item>
  5. <item name="android:typeface">monospace</item>
  6. </style>
  7. </resources>

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CodeFont">
<item name="android:textSize">20sp</item>
<item name="android:typeface">monospace</item>
</style>
</resources>

在mainfest 的application中添加 android:theme属性:

  1. <application android:icon="@drawable/icon"
  2. android:label="@string/app_name"
  3. android:theme="@style/CodeFont">

<application android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/CodeFont">

则application中的所有text字体都会改变,效果如下:

在每个<activity>标签中使用android:theme属性:

  1. <activity android:name=".MainActivity"
  2. android:label="@string/app_name"
  3. android:theme="@style/CodeFont">

<activity android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/CodeFont">

android:theme还可以配置android中已经存在的theme:

  1. <activity android:theme="@android:style/Theme.Translucent">

<activity android:theme="@android:style/Theme.Translucent">

如果想调整android已经定义好的theme,则可以通过自定义style来实现,例如:

  1. <color name="custom_theme_color">#b0b0ff</color>
  2. <style name="CustomTheme" parent="android:Theme.Light">
  3. <item name="android:windowBackground">@color/custom_theme_color</item>
  4. <item name="android:colorBackground">@color/custom_theme_color</item>
  5. </style>

<color name="custom_theme_color">#b0b0ff</color>
<style name="CustomTheme" parent="android:Theme.Light">
<item name="android:windowBackground">@color/custom_theme_color</item>
<item name="android:colorBackground">@color/custom_theme_color</item>
</style>

效果如下:

关于在<activity>中android的Theme的详细使用见:android Theme使用总结

根据android版本选择主题:

在android新的版本中增加了新的theme,如果想在新版中利用新theme同时又兼容旧版本,可以通过配置两个theme文件实现,例如在res/values目录下配置sytle.xml文件:

  1. <style name="LightThemeSelector" parent="android:Theme.Light">
  2. </style>

<style name="LightThemeSelector" parent="android:Theme.Light">
</style>

在res/values-11目录下配置文件style.xml:

  1. <style name="LightThemeSelector" parent="android:Theme.Holo.Light" mce_bogus="1">
  2. </style>

<style name="LightThemeSelector" parent="android:Theme.Holo.Light" mce_bogus="1">
</style>

按照文档中说的在res下创建values-11目录,程序报错,需要找时间研究一下,说不定是android的一个bug。

使用Android提供的Style和Theme:

Android平台提供了大量的styles和themes,可以在android包中的R.style下找到,但是Android现在并未提供关于styles和themes的相关文档说明,具体可以参考styles.xml源码themes.xml源码 ,扫了一下,描述的很清楚。

Android中Style和Theme的使用的更多相关文章

  1. Android中style和theme的区别

    在学习Xamarin android的过程中,最先开始学习的还是熟练掌握android的六大布局-LinearLayout .RelativeLayout.TableLayout.FrameLayou ...

  2. Android:Style和Theme

    在Web开发中,Html负责内容,CSS负责表现.同样,在Android开发中,可以使用Theme.Style+UI组件的方式实现内容和形式的分离. Style是针对窗体元素级别的,改变指定控件或者L ...

  3. android的style控制Theme

    value-v14就是在API14(4.0)的手机上所使用的Theme(其他版本以此类推) theme的名字叫做AppTheme,后面写有继承自哪个Theme,如下所示 <style name= ...

  4. Android中的主题Theme

    系统自带的Theme: android以及为我们定义好了一些theme,需要是我们直接可以拿来使用. 常用的Theme通常如下:  android:theme="@android:style ...

  5. Android中style的使用

    摘自搜搜问问. <item name="#1">#2</item> 1.item 的name属性#1可以为所有系统所带组件的属性,#2为此属性的值如andr ...

  6. 【转】说说Android中的style和theme

    最近在做软件从2.3到4.0的改变的一些工作,其中涉及了一些style和theme相关的东西.上网上查了一些东西,这个一并说说.关于android中style和theme的基本使用,这里就不再赘述了, ...

  7. Android入门第十六篇之Style与Theme [转]

    本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处! 越来越多互联网企业都在Android平台上部署其客户端,为了提升用户体验,这些客户端都做得布局合理而且美观.. ...

  8. Android中如何利用attrs和styles定义控件

    一直有个问题就是,Android中是如何通过布局文件,就能实现控件效果的不同呢?比如在布局文件中,我设置了一个TextView,给它设置了 textColor,它就能够改变这个TextView的文本的 ...

  9. android 中theme.xml与style.xml的区别

    from://http://liangoogle.iteye.com/blog/1848448 android 中theme.xml与style.xml的区别: 相同点: 两者的定义相同. <r ...

随机推荐

  1. 【Python开发实战】Windows7+VirtualBox+Ubuntu环境配置

    1. VirtualBox的安装 参考常规安装方式即可. VirtualBox 4.3.14 for Windows hosts:http://download.virtualbox.org/virt ...

  2. failure injection

    (1)malloc穷尽的情况: 假设下面的代码是测试代码,里面含有被测函数fmalloc,其中含有一个malloc语句,在一般情况下,是很难走到malloc失败的分支的,因为很难模拟系统内存耗尽的情况 ...

  3. 4537: [Hnoi2016]最小公倍数

    Description 给定一张N个顶点M条边的无向图(顶点编号为1,2,…,n),每条边上带有权值.所有权值都可以分解成2^a*3^b的形式.现在有q个询问,每次询问给定四个参数u.v.a和b,请你 ...

  4. npoi批量

    npoi批量导入实现及相关技巧 批量导入功能对于大部分后台系统来说都是不可或缺的一部分,常见的场景-基础数据的录入(部门,用户),用批量导入方便快捷.最近项目需要用到批量导入,决定花点时间写套比较通用 ...

  5. Arduino语言学习记录(持续更新)

    几天前某宝买了一套,这几天没工夫.今天开始学学这个“玩具”. 1.Arduino的变量数据类型: 数据类型  数据类型 RAM 范围 void keyword N/A N/A boolean 1 by ...

  6. iOS内存管理系列之一:对象所有权与引用计数

    当一个所有者(owner,其本身可以是任何一个Objective-C对象)做了以下某个动作时,它拥有对一个对象的所有权(ownership): 1. 创建一个对象.包括使用任何名称中包含“alloc” ...

  7. js compress and combine

    http://www.cnblogs.com/snandy/archive/2012/06/08/2541827.html http://developer.51cto.com/art/201301/ ...

  8. Fibonacci Tree

    hdu4786:http://acm.hdu.edu.cn/showproblem.php?pid=4786 题意:给你一个无向图,然后其中有的边是白色的有的边是黑色的.然后问你是否存在一棵生成树,在 ...

  9. Android SectionIndexer 的使用(联系人分类索引)

    // 获取标题栏索引 int position = sectionIndexter.getPositionForSection(l[idx]); ) { return true; } // 设置调整到 ...

  10. Git创建一个自己的本地仓库

    如果我们要把一个项目加入到Git的版本管理中,可以在项目所在的目录用git init命令建立一个空的本地仓库,然后再用git add命令把它们都加入到Git本地仓库的暂存区(stage or inde ...