说明

样式(style)是属性的集合,用来指定View或者Window的外观和格式。

这些属性可以是height(高度)、padding(内边距)、font size(字体颜色)等。

样式定义在另一个xml文件中,从布局文件中分离出来。

例如:

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

如果使用样式,则可以转换为

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

CodeFont就是样式文件名,把一些属性从原布局文件中提取出来,存放到了CodeFont文件中。

定义

样式文件必须存放在res/valuse的文件夹中,命名任意,后续为.xml。

CodeFont.xml

 <?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CodeFont" parent="@style/BaseFont">
<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>:根元素。

<style>:属性集合。

<item>:属性。

使用parent,可以继承样式。除此之外,还可以在<style>的命名前添加"继承样式名.",效果一样。例如:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="BaseFont.CodeFont" >
<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>

注意:

引用此样式时的名字为BaseFont.CodeFont。

如果引用系统自带的样式,值的格式为“@android:style/样式名”。我们自定义的样式,值的格式为“@style/样式名”。

应用

两种方式

对单独的View,举例:

  布局文件中,<TextView style="@style/CodeFont" android:text="@string/hello" />

对整个Activity或整个应用程序,举例:

  AndroidManifest.xml文件中,<activity android:theme="@android:style/Theme.Dialog">或者<application android:theme="@style/CustomTheme">

参考:http://developer.android.com/guide/topics/ui/themes.html

Android样式——Styles的更多相关文章

  1. Android 样式 (style) 和主题(theme)

    转载:https://gold.xitu.io/post/58441c48c59e0d0056a30bc2 样式和主题 样式是指为 View 或窗口指定外观和格式的属性集合.样式可以指定高度.填充.字 ...

  2. Android样式(style)和主题(theme)

    样式和主题 样式是指为 View 或窗口指定外观和格式的属性集合.样式可以指定高度.填充.字体颜色.字号.背景色等许多属性. 样式是在与指定布局的 XML 不同的 XML 资源中进行定义. Andro ...

  3. Android 样式和主题(style & theme)

    Android 样式 android中的样式和CSS样式作用相似,都是用于为界面元素定义显示风格,它是一个包含一个或者多个view控件属性的集合.如:需要定义字体的颜色和大小. 在CSS中是这样定义的 ...

  4. [转]Android样式的开发:shape篇

    转载自Keegan小钢原文链接:http://keeganlee.me/post/android/20150830 Android样式的开发:shape篇Android样式的开发:selector篇A ...

  5. 125、Android样式的开发(转载)

    Android样式的开发:drawable汇总篇 http://android.jobbole.com/82117/Android样式的开发:layer-list篇 http://android.jo ...

  6. Android样式的开发:shape篇

    转载请注明:转载自Keegan小钢并标明原文链接:http://keeganlee.me/post/android/20150830微信订阅号:keeganlee_me写于2015-08-30 And ...

  7. Android样式的开发:Style篇

    前面铺垫了那么多,终于要讲到本系列的终篇,整合所有资源,定义成统一的样式.哪些该定义成统一的样式呢?举几个例子吧: 每个页面标题栏的标题基本会有一样的字体大小.颜色.对齐方式.内间距.外间距等,这就可 ...

  8. android 样式开发

    1. Android的样式一般定义在res/values/styles.xml文件中,其中有一个根元素<resource>,而具体的每种样式定义则是通过<resource>下的 ...

  9. Android 样式的开发(转)

    Android(2)    目录(?)[-] rectangle oval line ring layer-list篇 普通图片 bitmap标签 点九图片 nine-patch标签 color标签 ...

随机推荐

  1. Mac技巧之让U盘、移动硬盘在苹果电脑和Windows PC都能识别/读写,且支持4GB大文件:exFAT格式

    如果您的 U 盘.移动硬盘既要用于 PC 又要用于苹果电脑,Mac OS X 系统的 HFS+ 和 Windows 的 NTFS 格式显然都不行……HFS+ 在 Windows 下不识别,NTFS 格 ...

  2. HelloWorld和数据绑定

    HelloWorld和数据绑定 目录导读: AngularJS 系列 学习笔记 目录篇 前言: 好记性不如烂键盘,随笔就是随手笔记,希望以后有用. 本篇目录: 1. Hello World 2. An ...

  3. iOS 本地存储四种方法

    在iOS开发过程中,不管是做什么应用,都会碰到数据保存的问题.将数据保存到本地,能够让程序的运行更加流畅,不会出现让人厌恶的菊花形状,使得用户体验更好.下面介绍⼀一下数据保存的方式: 1.NSKeye ...

  4. Android Studio 单刷《第一行代码》系列 03 —— Activity 基础

    前情提要(Previously) 本系列将使用 Android Studio 将<第一行代码>(书中讲解案例使用Eclipse)刷一遍,旨在为想入坑 Android 开发,并选择 Andr ...

  5. Any Way You Slice It (向量旋转 以及 判断线段是否相交)(模板)

    http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11353 #include<iostream> # ...

  6. Codeforces Round #346 (Div. 2) F. Polycarp and Hay 并查集

    题目链接: 题目 F. Polycarp and Hay time limit per test: 4 seconds memory limit per test: 512 megabytes inp ...

  7. uva 108

    降维  枚举行累加   然后求单行上最大连续和 #include <iostream> #include <cstring> #include <cstdio> # ...

  8. Linux命令ln的使用

    ln是linux中一个非常重要命令,它的功能是为某一个文件在另外一个位置建立一个链接,这个命令最常用的参数是-s,具体用法是:ln –s 源文件 目标文件. 当我们需要在不同的目录,用到相同的文件时, ...

  9. GameAdmin

    username:root e-mail :123@qq.com password:123

  10. swift苹果的下一代语言

    http://numbbbbb.github.io/the-swift-programming-language-in-chinese/chapter1/01_swift.html 有时间再看,bas ...