Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Android风格设计(style)

Android的风格设计(style)是一个很重要的功能,因为它可以让应用程序里的控件(widget)个性化。风格设计的使用如下:
  • 在Android的项目里以XML的资源来定义风格
  • 一个Android项目可以定义多个风格
  • 让widget套用其中的一个样式
Android的style功能,主要的对象是widget,风格是为了套用到widget上;另外Android提供布景(theme)功能,可以做更大范围的套用。相关阅读:Android的布景设计(theme) http://www.linuxidc.com/Linux/2012-05/61361.htm
Android事件监听器(Event Listener) http://www.linuxidc.com/Linux/2012-05/61186.htm下面是一个风格定义的具体例子:在/res/values/目录下建立一个新文件style.xml,编辑内容如下:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <style name="myText">  
  4.         <item name="android:textSize">18sp</item>  
  5.         <item name="android:textColor">#00FF00</item>  
  6.     </style>  
  7.     <style name="myButton">  
  8.         <item name="android:background">#00BFFF</item>  
  9.     </style>  
  10. </resources>  
style.xml语法说明:
  1. 在<resource>标签定义资源项目,<style>标签用来定义风格资源;
  2. <style>的name属性定义风格名称,widget使用此名称套用;
  3. <item>标签定义此风格的内容;
  4. textSize  ——  字体大小
  5. textColor —— 字体颜色
  6. background —— 背景
  7. 更多,参考Android Reference
定义好style后,就可以让widget套用。让widget套用定义好的style方法很简单,只需在main.XML中的widget项目属性添加定义好的style name就可以了,编辑main.XML:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         style="@style/myText"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="@string/hello" />  
  12.   
  13.     <Button  
  14.         android:id="@+id/btn"  
  15.         style="@style/myButton"  
  16.         android:layout_width="fill_parent"  
  17.         android:layout_height="wrap_content" />  
  18.   
  19. </LinearLayout>