Android 样式

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

在CSS中是这样定义的:

<style>
.wu{COLOR:#0000CC;font-size:18px;}
</style>

可以像这样使用上面的css样式:<div class="wu">wuyudong‘blog</div>

在Android中可以这样定义样式:

在res/values/styles.xml文件中添加以下内容

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name=“wu”> <!-- 为样式定义一个全局唯一的名字-->
<item name=“android:textSize”>18px</item> <!-- name属性的值为使用了该样式的View控件的属性 -->
<item name="android:textColor">#0000CC</item>
</style>
</resources>

在layout文件中可以像下面这样使用上面的android样式:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ....>
<TextView style="@style/wu"
..... />
</LinearLayout>

下面来实践一下

在style.xml中添加下面的代码:

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices. -->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here. -->
</style> <!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style> <style name="text_content_style" parent="AppBaseTheme">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#66ff00</item>
<item name="android:textSize">20sp</item>
</style> </resources>

布局代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" > <TextView
style="@style/text_content_style"
android:text="我是一个文本" />
<TextView
style="@style/text_content_style"
android:text="我是一个文本" />
<TextView
style="@style/text_content_style"
android:text="我是一个文本" />
<TextView
style="@style/text_content_style"
android:text="我是一个文本" />
<TextView
style="@style/text_content_style"
android:text="我是一个文本" /> </LinearLayout>

运行项目后

Android 主题

android中主题也是用于为应用定义显示风格,它的定义和样式的定义相同,如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name=“wuTheme">
<item name=“android:windowNoTitle”>true</item> <!–- 没标题 -->
<item name=“android:windowFullscreen”>?android:windowNoTitle</item> <!–- 全屏显示 -->
</style>
</resources>

上面“?android:windowNoTitle”中的问号用于引用在当前主题中定义过的资源的值。下面代码显示在AndroidManifest.xml中如何为应用设置上面定义的主题:

<application android:icon="@drawable/icon" android:label="@string/app_name"
android:theme="@style/wuTheme">
......
</application>

除了可以在AndroidManifest.xml中设置主题,同样也可以在代码中设置主题,如下:

setTheme(R.style.itcastTheme);

尽管在定义上,样式和主题基本相同,但是它们使用的地方不同。样式用在单独的View,如:EditText、TextView等;主题通过AndroidManifest.xml中的<application>和<activity>用在整个应用或者某个 Activity,主题对整个应用或某个Activity进行全局性影响。如果一个应用使用了主题,同时应用下的view也使用了样式,那么当主题和样式属性发生冲突时,样式的优先级高于主题。

另外android系统也定义了一些主题,例如:<activity android:theme=“@android:style/Theme.Dialog”>,该主题可以让Activity看起来像一个对话框,还有透明主题:@android:style/Theme.Translucent 。如果需要查阅这些主题,可以在文档的referenceandroid-->R.style 中查看。

Android 样式和主题(style & theme)的更多相关文章

  1. Android样式的开发:Style篇

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

  2. Android样式和主题

    样式:style--> 主题:theme--> <style name="my_style"> <item name="android:te ...

  3. android样式跟主题

    简单说类似与自定义控件,只不过自定义控件针对的是view 而样式与主题针对的是属性.元素 在TexvView中引入样式 layout.xml <?xml version="1.0&qu ...

  4. Android学习笔记主题(Theme)资源文件

    安卓的主题资源文件,可以用于对Android应用的美化. styles文件是主题资源文件. 定义一个主题资源格式如下: <resources> <!-- Base applicati ...

  5. android 样式和主题

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

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

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

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

  8. 第三部分:Android 应用程序接口指南---第二节:UI---第十一章 样式和主题

    第11章 样式和主题 style是用于指定View或window的外观和格式的一系列属性的集合.style可以指定高(height).填补(padding).字体颜色.字体大小.背景颜色等等属性.st ...

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

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

随机推荐

  1. JS根据身份证号码算年龄

    如果把身份证号码传到页面上,在前端页面获取年龄就需要用到JS脚本了: function GetAge(identityCard) { var len = (identityCard + "& ...

  2. HTML5的五种客户端离线存储方案

    最近折腾HTML5游戏需要离线存储功能,便把目前可用的几种HTML5存储方式研究了下,基于HT for Web写了个综合的实例,分别利用了Cookie.WebStorage.IndexedDB以及Fi ...

  3. PHP内核研究(内存管理1)

    PHP内存管理 PHP在5.3之前采用的是引用计数法 PHP在5.3之后采用了新的垃圾回收机制 操作系统在申请内存空间的时候回引发系统调用 在操作系统申请内存空间的时候,会将CPU从用户态切换到内核态 ...

  4. ROS 多台电脑间进行通信

    版权声明:本文为博主原创文章,转载请标明出处: http://www.cnblogs.com/liu-fa/p/5773822.html 在我看来,ROS最牛逼的地方就是它的通信机制了,不仅仅是进程间 ...

  5. Swift 值类型和引用类型

    Swift中的类型分为两类:一,值类型(value types),每个值类型的实例都拥有各自唯一的数据,通常它们是结构体,枚举或元组:二,引用类型(reference types),引用类型的实例共享 ...

  6. Java 读取Properties文件时应注意的路径问题

    1. 使用Class的getResourceAsStream()方法读取Properties文件(资源文件)的路径问题:  InputStream in = this.getClass().getRe ...

  7. 怎样实现了捕获应用中的日志在android开发中

    怎样实现了捕获应用中的日志在android开发中,大家可研究一下. Process mLogcatProc = null; BufferedReader reader = null; try { mL ...

  8. 2016年湖南省第十二届大学生计算机程序设计竞赛Problem A 2016 找规律归类

    Problem A: 2016 Time Limit: 5 Sec  Memory Limit: 128 MB Description  给出正整数 n 和 m,统计满足以下条件的正整数对 (a,b) ...

  9. this指针

    this Note:不论是类中的构造函数,还是类中的普通成员函数,内部都隐含一个形参叫this指针,用于接收当前正在构造/调用对象的地址,因此不同的对象被构造/调用时,同一个函数的执行结果不同;对于一 ...

  10. 烦人的win10的输入法

    这段时间在使用win10,被win10的输入法折腾的要死要死的... 通过度娘把它设置得跟win7使用习惯差不多了, (见:http://jingyan.baidu.com/article/b2c18 ...