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

1、样式定义

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

如下是一组样式的定义

[xml] view plain copy print ?
  1. <spanstyle="background-color:rgb(255,255,255);"><!--全局字体样式-->
  2. <stylename="DefaultFontStyle">
  3. <itemname="android:textSize">18px</item>
  4. <itemname="android:textColor">#0000CC</item>
  5. </style>
  6. <!--全局背景色-->
  7. <stylename="DefaultBgColor"parent="@style/DefaultFontStyle">
  8. <itemname="android:background">#F2F2F2</item>
  9. </style>
  10. <!--全局样式-->
  11. <stylename="DefaultStyle"parent="@style/DefaultBgColor">
  12. </style></span>

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

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

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

2、单个view如何设置样式

比如TextView,设置样式如下

[xml] view plain copy print ?
  1. <spanstyle="background-color:rgb(255,255,255);"><TextView
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content"
  4. android:text="我在做什么:"
  5. android:textSize="18px"
  6. android:textColor="#0000CC"
  7. /></span>

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

[xml] view plain copy print ?
  1. <spanstyle="background-color:rgb(255,255,255);"><TextView
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content"
  4. android:text="我在做什么:"
  5. style="@style/DefaultStyle"
  6. /></span>

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

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

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

3、全局样式设置

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

[html] view plain copy print ?
  1. <spanstyle="background-color:rgb(255,255,255);">body{
  2. background:#cce8cf;
  3. color:#000;
  4. font-family:宋体verdana,tahoma;
  5. font-size:18px;
  6. padding:1px2px02px;
  7. counter-reset:section;
  8. }</span>

设置全局的样式

[html] view plain copy print ?
  1. <spanstyle="background-color:rgb(255,255,255);">div{
  2. margin-top:10px;
  3. margin-bottom:10px;
  4. }</span>

设置单个标签的样式

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

[xml] view plain copy print ?
  1. <spanstyle="background-color:rgb(255,255,255);"><applicationandroid:theme="@style/DefaultStyle"></span>

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

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

[xml] view plain copy print ?
  1. <spanstyle="background-color:rgb(255,255,255);"><activityandroid:name=".AccountManageActivity"
  2. android:theme="@style/DefaultStyle"></span>

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

[xml] view plain copy print ?
  1. <spanstyle="background-color:rgb(255,255,255);">android:theme="@android:style/Theme.Dialog"</span>

为对话框样式设置

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

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

3、样式继承关系

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

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

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

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

例子如下

[xml] view plain copy print ?
  1. <spanstyle="background-color:rgb(255,255,255);"><!--全局字体样式-->
  2. <stylename="DefaultFontStyle">
  3. <itemname="android:textSize">18px</item>
  4. <itemname="android:textColor">#0000CC</item>
  5. </style>
  6. <!--全局背景色-->
  7. <stylename="DefaultBgColor"parent="@style/DefaultFontStyle">
  8. <itemname="android:background">#F2F2F2</item>
  9. </style>
  10. <!--全局样式-->
  11. <stylename="DefaultStyle"parent="@style/DefaultBgColor">
  12. </style>
  13. <!--textView字体样式-->
  14. <stylename="TextViewFontStyle">
  15. <itemname="android:textSize">20px</item>
  16. </style></span>

application主题为

[xml] view plain copy print ?
  1. <spanstyle="background-color:rgb(255,255,255);"><applicationandroid:theme="@style/DefaultStyle"></span>

activity主题为

[xml] view plain copy print ?
  1. <spanstyle="background-color:rgb(255,255,255);"><activityandroid:name=".AccountManageActivity"
  2. android:theme="@style/DefaultStyle"></span>

textView样式设置如下

[java] view plain copy print ?
  1. <spanstyle="background-color:rgb(255,255,255);"><TextView
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content"
  4. android:text="我在做什么:"
  5. style="@style/TextViewFontStyle"
  6. /></span>

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

更多相关文章

  1. 填坑总结:通过selector的android:state_checkable和android:state
  2. android与html5的交互——数据库操作,UI操作,以及html5的localStor
  3. android notification通知栏及8.0适配
  4. Android(安卓)各版本 设置 USB 默认连接 MTP 模式 ( Android(安
  5. android优化 清除无效代码 UCDetector
  6. Android_TextView属性XML详解
  7. 基于android的远程视频监控系统(已开放源码)
  8. Android的Window类
  9. Android中主题与样式

随机推荐

  1. Android 透明Dialog
  2. LinearLayout和RelativeLayout的属性区别
  3. Android 图片拖动和缩放
  4. APIDEMO ACTIVITY 屏幕在出现软键盘情况
  5. Android工具开发一(清除手机所有app缓存)
  6. Android(安卓)O中Notification进度条一直
  7. 如何手工创建android项目
  8. androidx.core.widget.NestedScrollView
  9. 使用googleMap获取api方法
  10. PHP,Android,IOS通信之 AES128加解密案例