说明

样式(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. NGUI系列教程九(自制ListView)

    在NGUI中可以很方便的实现ListView的控件,ListView就好比IOS或Android平台中使用手势上下拖动的控件.在Unity3D中实现ListView的原理无非就两种,第一种是摄像机不动 ...

  2. OGRE1.8.1源码编译(VS2008)

    最近在学习图形学,想看看OGRE源码,于是就去OGRE官网下载源码,配置环境,这里记录一下.鉴于电脑太破,VS2010比较慢,所以使用VS2008. 准备工作: CMake2.8       下载地址 ...

  3. ExtJS4.2学习(五)表格渲染与复选框

    鸣谢:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-11-10/174.html --------------- ...

  4. SQL SERVER数据导入

    我的博客已好久没有文字方面的记载了,好歹昨天已经结束软件设计师的考试了,今天怎么说也需要锻炼自己的写作能力.不然真怕自己又像上一年一样,一停就一年多了. 想好好学习数据库(SQL SERVER)方面的 ...

  5. WCF获取客户端IP和端口

    //提供方法执行的上下文环境 OperationContext context = OperationContext.Current; //获取传进的消息属性 MessageProperties pr ...

  6. WPF 位置转化和动画

    位置转化 private void DrawScale() { double majorTickUnitValue = this.ScaleSweepLenth / this.MajorDivisio ...

  7. Java Code Examples for org.springframework.http.HttpStatus

    http://www.programcreek.com/java-api-examples/index.php?api=org.springframework.http.HttpStatus

  8. ASP.NET 4.5.256 has not been registered on the Web server

    请见:http://answers.microsoft.com/en-us/insider/forum/insider_apps-insider_other/aspnet-45256-has-not- ...

  9. Time.deltaTime 含义和应用

    第一種:使用Time.deltaTime 一秒內從第1個Frame到最後一個Frame所花的時間,所以不管電腦是一秒跑60格或者一秒30格.24格,值都會趨近於一. 就結果而言,deltaTime是為 ...

  10. google maps v3 添加自定义图标(marker,overlay)

    google搜了很久都没找到符合v3版本的google maps自定义图标,可以让图标使用自己的html,都是V2版本的,依靠重写google api属性来完成. 然后就找了个jquery下的goog ...