https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/styles.xml(android默认样式)

Android样式——Styles

说明

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

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

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

例如:

复制代码
1 <TextView2     android:layout_width="fill_parent"3     android:layout_height="wrap_content"4     android:textColor="#00FF00"5     android:typeface="monospace"6     android:text="@string/hello" />
复制代码

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

复制代码
1 <TextView2     style="@style/CodeFont"3     android:text="@string/hello" />
复制代码

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

定义

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

CodeFont.xml

复制代码
1 <?xml version="1.0" encoding="utf-8"?>2 <resources>3     <style name="CodeFont" parent="@style/BaseFont">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>
复制代码

<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

更多相关文章

  1. 《android开发应用实战详解》光盘源代码
  2. Android 源码编译 文件系统制作
  3. manifest文件
  4. android不是内部或外部命令,也不是可运行的程序或批处理文件
  5. android sdk 文件目录含义介绍
  6. 如何查看android数据文件?
  7. android 添加文件打开方式,找了很久终于找到了,收藏起来吧
  8. Android读取文件

随机推荐

  1. Appium学习第一个测试脚本
  2. android读取扫码模组数据的方法
  3. Android图形系统草稿
  4. Android版本变化而产生的权限变更
  5. Android(安卓)一个自定义View需要实现哪
  6. Android嵌套滑动机制源码分析
  7. Android(安卓)多渠道打包进阶版
  8. [转]Android(安卓)开源项目分类汇总
  9. android 用代码编写linearlayout布局
  10. Android开发小技巧(二)-------如何获取全局