android中的样式和CSS样式作用相似,都是用于为界面元素定义显示风格,它是一个包含一个或者多个view控件属性的集合。如:需要定义字体的颜色和大小。
在CSS中是这样定义的:
<style>元素中有一个parent属性。这个属性可以让当前样式继承一个父样式,当前样式可以继承到父样式的值。当然,如果父样式的值不符合你的需求,你也可以对它进行修改,如下:
在CSS中是这样定义的:
1
<style>
2 .itcast { COLOR : #0000CC ; font-size : 18px ; }
3 </style>
2 .itcast { COLOR : #0000CC ; font-size : 18px ; }
3 </style>
可以像这样使用上面的css样式:
1
<
div
class
="itcast"
>
传智播客
</
div
>
在Android中可以这样定义样式:
在res/values/styles.xml文件中添加以下内容
1
<?
xml version="1.0" encoding="utf-8"
?>
2 < resources >
3 < style name =“itcast”> <!-- 为样式定义一个全局唯一的名字-- >
4 < item name ="android:textSize" > 18px </ item > <!-- name属性为样式要用在的View控件持有的属性 -->
5 < item name ="android:textColor" > #0000CC </ item >
6 </ style >
7 </ resources >
2 < resources >
3 < style name =“itcast”> <!-- 为样式定义一个全局唯一的名字-- >
4 < item name ="android:textSize" > 18px </ item > <!-- name属性为样式要用在的View控件持有的属性 -->
5 < item name ="android:textColor" > #0000CC </ item >
6 </ style >
7 </ resources >
在layout文件中可以像下面这样使用上面的android样式:
1
<?
xml version="1.0" encoding="utf-8"
?>
2 < LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android" . >
3 < TextView style ="@style/itcast"
4 .. />
5 </ LinearLayout >
2 < LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android" . >
3 < TextView style ="@style/itcast"
4 .. />
5 </ LinearLayout >
<style>元素中有一个parent属性。这个属性可以让当前样式继承一个父样式,当前样式可以继承到父样式的值。当然,如果父样式的值不符合你的需求,你也可以对它进行修改,如下:
1
<?
xml version="1.0" encoding="utf-8"
?>
2 < resources >
3 < style name ="itcast" >
4 < item name ="android:textSize" > 18px </ item > <!-- name属性为样式要用在的View控件持有的属性 -->
5 < item name ="android:textColor" > #0000CC </ item >
6 </ style >
7 < style name ="subitcast" parent ="@style/itcast" >
8 < item name ="android:textColor" > #FF0000 </ item >
9 </ style >
10 </ resources >
2 < resources >
3 < style name ="itcast" >
4 < item name ="android:textSize" > 18px </ item > <!-- name属性为样式要用在的View控件持有的属性 -->
5 < item name ="android:textColor" > #0000CC </ item >
6 </ style >
7 < style name ="subitcast" parent ="@style/itcast" >
8 < item name ="android:textColor" > #FF0000 </ item >
9 </ style >
10 </ resources >
android中主题也是用于为应用定义显示风格,它的定义和样式的定义相同,如下:
1
<?
xml version="1.0" encoding="utf-8"
?>
2 < resources >
3 < style name =“itcastTheme">
4 <item name =“android:windowNoTitle”>true</item> <!-- 没标题 -- >
5 < item name =“android:windowFullscreen”>?android:windowNoTitle</item> <!--全屏显示-- >
6 </ style >
7 </ resources >
2 < resources >
3 < style name =“itcastTheme">
4 <item name =“android:windowNoTitle”>true</item> <!-- 没标题 -- >
5 < item name =“android:windowFullscreen”>?android:windowNoTitle</item> <!--全屏显示-- >
6 </ style >
7 </ resources >
上面“?android:windowNoTitle”中的问号用于引用在当前主题中定义过的资源的值。
下面代码显示在AndroidManifest.xml中如何为应用设置上面定义的主题:
1
<
application
android:icon
="@drawable/icon"
android:label
="@string/app_name"
2 android:theme ="@style/itcastTheme" >
3
4 </ application >
2 android:theme ="@style/itcastTheme" >
3
4 </ application >
除了可以在AndroidManifest.xml中设置主题,同样也可以在代码中设置主题,如下:
setTheme(R.style.itcastTheme);
尽管在定义上,样式和主题基本相同,但是它们使用的地方不同。
样式用在单独的View,如:EditText、TextView等;
主题通过AndroidManifest.xml中的<application>和<activity>用在整个应用或者某个 Activity,主题对整个应用或某个Activity存在全局性影响。
如果一个应用使用了主题,同时应用下的view也使用了样式,那么当主题与样式属性发生冲突时,样式的优先级高于主题。
样式用在单独的View,如:EditText、TextView等;
主题通过AndroidManifest.xml中的<application>和<activity>用在整个应用或者某个 Activity,主题对整个应用或某个Activity存在全局性影响。
如果一个应用使用了主题,同时应用下的view也使用了样式,那么当主题与样式属性发生冲突时,样式的优先级高于主题。
另外android系统也定义了一些主题,例如:<activity android:theme=“@android:style/Theme.Dialog”>,该主题可以让Activity看起来像一个对话框,如果需要查阅这些主题,可以在文档的reference-->android-->R.style 中查看。