android笔记27- 风格style 使用
来源:互联网 发布:人工智能的未来趋势 编辑:程序博客网 时间:2024/05/23 01:18
1、初初了解
style和theme 都是以独立的XML格式文件存在于 工程res\values这个文件夹下面的。他们都是资源,android本身有默认的style 和 theme资源,但是你也可以自定义自己的style 和 theme 。
2、 自定义style
自定义步骤:
(1)如果res/values文件夹下面没有style.xml,那么新建一个xml。命名为style;
(2)增加一个<resources> 根节点;
(3)在<style>元素内部,申明一个或者多个<item>,每一个<item>定义了一个名字属性,并且在元素内部定义了这个风格的值。.
举例如下:新建一个style.xml 编辑输入以下内容:
<?xml version="1.0" encoding="utf-8"?><resources> <style name="SpecialText" parent="@style/Text"> <item name="android:textSize">18sp</item> <item name="android:textColor">#008</item> </style></resources>
下面再演示如何引用该自定义的风格:<EditText id="@+id/text1" style="@style/SpecialText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hello, World!" />当我使用了很多个Text控件,而他们对文字大小和颜色是一样的要求的时候,那么我们就可以用 style="@style/SpecialText" 这么一句话来代替这些设置了。
注意:在<style>元素中的父类属性(即上面红色文字)。这个属性让你可以能够定义一个资源,当前风格可以从这个资源当中继承到值。
比如说,看以下我们设置的两种风格:
<<?xml version="1.0" encoding="utf-8"?> <resources> <style name="homebtn" > <item name="android:layout_width">wrap_content</item> <item name="android:layout_height">wrap_content</item> <item name="android:textSize">20sp</item> <item name="android:textColor">#FFFFffff</item> <item name="android:gravity">center</item> <item name="android:layout_marginTop">15dip</item> <item name="android:layout_centerHorizontal">true</item> <item name="android:background">@drawable/selector</item> </style> <style name="homeiv"> <item name="android:layout_width">wrap_content</item> <item name="android:layout_height">wrap_content</item> <item name="android:layout_marginTop">15dip</item> <item name="android:layout_centerHorizontal">true</item> </style> </resources>
我们明显可以看到,这两种风格是存在重复的属性(红色代码)的。为了减小工作量,这时候我们的parent 就派上用场了。
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="homeiv"> <item name="android:layout_width">wrap_content</item> <item name="android:layout_height">wrap_content</item> <item name="android:layout_marginTop">15dip</item> <item name="android:layout_centerHorizontal">true</item> <item name="android:background">@drawable/home</item> </style> <style name="homebtn" parent="@style/homeiv"> <item name="android:textSize">20sp</item> <item name="android:textColor">#FFFFffff</item> <item name="android:gravity">center</item> <item name="android:background">@drawable/selector</item> </style> </resources>
看,没有重复了吧。可能现在如此少的代码你觉得没有多少,但是当重复属性很多的时候,你就觉得这个parent 是多么的可爱的了。哈哈,如此即可快速并简单地实现style风格的设置。
声明:本博文部分内容出自于
Android 用style简化layout布局文件
仅当学习笔记记录所用。 0 0
- android笔记27- 风格style 使用
- 笔记 style.xml风格记录
- Android风格设计(style)
- android 主题theme风格style
- android主题theme和风格style总结
- Android风格与主题( style and theme )
- Android的风格和主题(style,themes)
- Android主题theme和风格style总结
- Android风格style与主题theme
- Android主题theme和风格style总结
- android风格和主题:Style and Theme
- Android 主题风格(Theme&Style)介绍
- 使用风格(Style)和主题(Theme)
- android中自定义主题style的使用,app风格Theme自定义
- Android笔记 style样式
- android 组件使用style
- android样式style使用
- android style 使用
- topcoder srm609 div1
- 一口一个设计模式--职责链模式
- setuptools
- wifi共享精灵 设置热点后 电脑上不了网
- 链表倒序算法
- android笔记27- 风格style 使用
- C++对象
- C/C++内存分配知识
- 木棒三角形-枚举
- linux下xargs基本用法
- 黑马程序员—16—java基础:有关map结合的学习笔记和心得体会
- shell学习总结之变量引用
- vim tab设置为4个空格
- 是不是已经否为数字或数字字