android应用程序如何设置样式,包括样式定义、单个view设置样式全局样式设置样式继承关系

1、样式定义

android的样式定义在res/values/style.xml文件中,类似web前端中将样式定义在某个css文件中,但android的style.xml是自动加载的,不需要手动import或link。目前还不了解android是否可以或怎么定义多个style文件。

如下是一组样式的定义

    <!-- 全局字体样式-->    <style name="DefaultFontStyle">         <item name="android:textSize">18px</item>        <item name="android:textColor">#0000CC</item>    </style>        <!-- 全局背景色-->    <style name="DefaultBgColor" parent="@style/DefaultFontStyle">         <item name="android:background">#F2F2F2</item>    </style>        <!-- 全局样式-->    <style name="DefaultStyle" parent="@style/DefaultBgColor">     </style>

a. android的样式定义是通过style标签完成的,通过添加item元素设置不同的属性值

b.样式可以通过设置parent进行继承。上面的DefaultBgColor继承自DefaultFontStyle,而DefaultStyle又继承自DefaultBgColor,这样DefaultStyle就有了字体大小颜色、背景色的属性了。

c. android的主题样式和一般样式的定义是一样的,只是引用时不同,下面将会介绍

2、单个view如何设置样式

比如TextView,设置样式如下

<TextView     android:layout_width="match_parent"     android:layout_height="wrap_content"    android:text="我在做什么:"    android:textSize="18px"    android:textColor="#0000CC"    />

也可以引用第一部分定义的样式,如下

<TextView     android:layout_width="match_parent"     android:layout_height="wrap_content"    android:text="我在做什么:"    style="@style/DefaultStyle"    />

设置view的style属性进行样式调用,推荐使用此种方式将样式和布局分离。其他view及viewGroup设置相同。

对于单个view的更多属性可以参考http://developer.android.com/reference/android/R.styleable.html#View

或具体的某个view的sdk文档xml attribute.

3、全局样式设置

在web前端编程中,可以使用

body {background: #cce8cf;color: #000;font-family: 宋体 verdana, tahoma;font-size: 18px;padding: 1px 2px 0 2px;counter-reset: section;}

设置全局的样式

div {    margin-top: 10px;margin-bottom: 10px;}

设置单个标签的样式

android中我们同样可以办到,只是这种全局样式被称作主题theme,比如对于整个应用默认字体都要18px,颜色为#0000CC,背景色为#F2F2F2,我们可以通过在AndroidManifest.xml设置application的android:theme属性完成,如下:

<application android:theme="@style/DefaultStyle">

DefaultStyle即为第一部分中定义的主题,在第一部分中我们提到的主题和样式定义一样也是这个意思,只是引用的时候使用android:theme罢了。

下面为单个activity设置主题的代码

<activity android:name=".AccountManageActivity"      android:theme="@style/DefaultStyle">

activity的主题还有一些特殊设置,如

android:theme="@android:style/Theme.Dialog"

为对话框样式设置

主题的设置也可以在代码中通过setTheme(R.id.xx)完成。

接下来问题就出现了,如果一个应用设置了application的主题,设置了activity,设置了view的样式,那么view的各个样式属性值究竟是多少呢??

3、样式继承关系

android的样式采取和css中一样的覆盖、继承原则,和面向对象的子类覆盖父类属性、继承没有定义的父类属性值的原则是一样的。

如果一个TextView自己设置了样式,它的ViewGroup设置了样式,activity设置了主题,application设置了主题。

它会先读取自己样式的值,对于自己没有的样式向上查找第一个找到的值即为要采取的值。

依次读取的顺序为View自己的样式->上一层ViewGroup的属性值->上上层ViewGroup的属性值->…->activity主题->activity主题

例子如下

    <!-- 全局字体样式-->    <style name="DefaultFontStyle">         <item name="android:textSize">18px</item>        <item name="android:textColor">#0000CC</item>    </style>        <!-- 全局背景色-->    <style name="DefaultBgColor" parent="@style/DefaultFontStyle">         <item name="android:background">#F2F2F2</item>    </style>        <!-- 全局样式-->    <style name="DefaultStyle" parent="@style/DefaultBgColor">     </style>    <!-- textView字体样式-->    <style name="TextViewFontStyle">         <item name="android:textSize">20px</item>    </style>

application主题为

<application android:theme="@style/DefaultStyle">

activity主题为

<activity android:name=".AccountManageActivity"      android:theme="@style/DefaultStyle">

textView样式设置如下

<TextView     android:layout_width="match_parent"     android:layout_height="wrap_content"    android:text="我在做什么:"    style="@style/TextViewFontStyle"    />

则textView中最终字体大小为20px,颜色采用activity中设置的0000CC

更多相关文章

  1. Android学习系列(39)--Android主题和样式之系统篇(上)
  2. .Android的Window类默认分类
  3. Android(安卓)实现自适应正方形GridView
  4. 利用HTML5开发Android笔记
  5. android map (地图)
  6. android 桌面程序 滑动抽屉 SlidingDraw,一个小小的demo
  7. Xmlns:android
  8. Android中给View设置阴影的三种方式
  9. Android之主题与样式

随机推荐

  1. This Android(安卓)SDK requires Android
  2. mapView 和textView布局
  3. 大话企业级android读书笔记(三)
  4. [Android(安卓)GMS 认证] CTS 问题列表之
  5. android原生音乐播放器界面字体显示不全
  6. Cocos2d-x编译Android环境
  7. AES加密解密Android版
  8. Android中OnScrollListener的详解(Listvie
  9. android gridview按钮边框和定制点击颜色
  10. android 自定义输入框