现在的位置: 首页 > 综合 > 正文

android的样式(style)与主题(theme)

2013年01月01日 ⁄ 综合 ⁄ 共 9897字 ⁄ 字号 评论关闭
文章目录

Android上的Style分为了两个方面: 

1,Theme是针对窗体级别的,改变窗体样式; 
2,Style是针对窗体元素级别的,改变指定控件或者Layout的样式。 

Android系统的themes.xml和style.xml(位于系统源代码frameworks\base\core\res\res\values\)包含了很多系统定义好的style,建议在里面挑个合适的,然后再继承修改。

风格是一个包含一种或者多种格式化属性的集合,你可以将其用为一个单位用在布局XML单个元素当中。比如,你可以定义一种风格来定义文本的字号大小和颜色,然后将其用在View元素的一个特定的实例。

主题是一个包含一种或者多种格式化属性的集合,你可以将其为一个单位用在应用中所有的Activity当中或者应用中的某个Activity当 中。比如,你可以定义一个主题,它为window frame和panel 的前景和背景定义了一组颜色,并为菜单定义可文字的大小和颜色属性,你可以将这个主题应用在你程序当中所有的Activity里。

风格和主题都是资源。你可以用android提供的一些默认的风格和主题资源,你也可以自定义你自己的主题和风格资源。
如何新建自定义的风格和主题:
1.在res/values 目录下新建一个名叫style.xml的文件。增加一个<resources>根节点。
2.对每一个风格和主题,给<style>element增加一个全局唯一的名字,也可以选择增加一个父类属性。在后边我们可以用这个名字来应用风格,而父类属性标识了当前风格是继承于哪个风格。
3.在<style>元素内部,申明一个或者多个<item>,每一个<item>定义了一个名字属性,并且在元素内部定义了这个风格的值。
4.你可以应用在其他XML定义的资源。
——————————————————————————————–
风格
下边是一个申明风格的实例:
 
<?xml version="1.0" encoding="utf-8"?>
<resources>
 <style name="SpecialText" parent="@style/Text">
 <item name="android:textSize">18sp</item>
 <item name="android:textColor">#008</item>
 </style>
</resources>
 
如上所示,你可以用<item>元素来为你的风格定义一组格式化的值。在Item当中的名字的属性可以是一个字符串,一个16进制数所表示的颜色或者是其他资源的引用。
注意在<style>元素中的父类属性。这个属性让你可以能够定义一个资源,当前风格可以从这个资源当中继承到值。你可以从任何包 含这个风格的资源当中继承此风格。通常上,你的资源应该一直直接或者间接地继承Android的标准风格资源。 这样的话,你就只需要定义你想改变的值。
在这个例子当中的EditText元素,演示了如何引用一个XML布局文件当中定义的风格:
<EditText id="@+id/text1"
 style="@style/SpecialText"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="Hello, World!" />
现在这个EditText组件的所表现出来的风格就为我们在上边的XML文件中所定义的那样。

主题

就像风格一样,主题依然在<style>元素里边申明,也是以同样的方式引用。不同的是你通过在Android Manifest中定义的<application>和<activity>元素将主题添加到整个程序或者某个 Activity,但是主题是不能应用在某一个单独的View里。
下边是申明主题的一个例子:
<?xml version="1.0" encoding="utf-8"?>
<resources>
 <style name="CustomTheme">
 <item name="android:windowNoTitle">true</item>
 <item name="windowFrame">@drawable/screen_frame</item>
 <item name="windowBackground">@drawable/screen_background_white</item>
 <item name="panelForegroundColor">#FF000000</item>
 <item name="panelBackgroundColor">#FFFFFFFF</item>
 <item name="panelTextColor">?panelForegroundColor</item>
 <item name="panelTextSize">14</item>
 <item name="menuItemTextColor">?panelTextColor</item>
 <item name="menuItemTextSize">?panelTextSize</item>
 </style>
</resources>
注意我们用了@符号和?符号来应用资源。@符号表明了我们应用的资源是前边定义过的(或者在前一个项目中或者在Android 框架中)。问号?表明了我们引用的资源的值在当前的主题当中定义过。通过引用在<item>里边定义的名字可以做到(panelTextColor 用的颜色和panelForegroundColor中定义的一样)。这中技巧只能用在XML资源当中。
在manifest当中设置主题
为了在成用当中所有的Activity当中使用主题,你可以打开AndroidManifest.xml 文件,编辑<application>标签,让其包含android:theme属性,值是一个主题的名字,如下:
<application android:theme="@style/CustomTheme">
如果你只是想让你程序当中的某个Activity拥有这个主题,那么你可以修改<activity>标签。
Android中提供了几种内置的资源,有好几种主题你可以切换而不用自己写。比如你可以用对话框主题来让你的Activity看起来像一个对话框。在manifest中定义如下:
<activity android:theme="@android:style/Theme.Dialog">
如果你喜欢一个主题,但是想做一些轻微的改变,你只需要将这个主题添加为父主题。比如我们修改Theme.Dialog主题。我们来继承Theme.Dialog来生成一个新的主题。
<style name="CustomDialogTheme" parent="@android:style/Theme.Dialog">
继承了Theme.Dialog后,我们可以按照我们的要求来调整主题。我们可以修改在Theme.Dialog中定义的每个item元素的值,然后我们在Android Manifest 文件中使用CustomDialogTheme 而不是 Theme.Dialog 。

在程序当中设置主题

如果需要的话,你可 以在Activity当中通过使用方法setTheme()来加载一个主题。注意,如果你这么做的话,你应该初始化任何View之前设置主题。比如,在调 用setContentView(View) 和inflate(int, ViewGroup)方法前。这保证系统将当前主题应用在所有的UI界面。例子如下:
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 ...
 setTheme(android.R.style.Theme_Light);
 setContentView(R.layout.linear_layout_3);
}
如果你打算在程序代码中来加载主界面的主题,那么需要注意主题当中不能包括任何系统启动这个Activity所使用的动画,这些动画将在程序启动前显示。在很多情况下,如果你想将主题应用到你的主界面,在XML中定义似乎是一个更好的办法。

下面的前三个之外直接复制就会出错。@是说明系统已经定义过的,@android:style/  是必须带上的。

?android:theme="@android:style/Theme.Dialog"   将一个Activity显示为对话框模式
?android:theme="@android:style/Theme.NoTitleBar"  不显示应用程序标题栏
?android:theme="@android:style/Theme.NoTitleBar.Fullscreen"  不显示应用程序标题栏,并全屏
?android:theme="Theme.Light"  背景为白色
?android:theme="Theme.Light.NoTitleBar"  白色背景并无标题栏 
?android:theme="Theme.Light.NoTitleBar.Fullscreen"  白色背景,无标题栏,全屏
?android:theme="Theme.Black"  背景黑色
?android:theme="Theme.Black.NoTitleBar"  黑色背景并无标题栏
?android:theme="Theme.Black.NoTitleBar.Fullscreen"    黑色背景,无标题栏,全屏
?android:theme="Theme.Wallpaper"  用系统桌面为应用程序背景
?android:theme="Theme.Wallpaper.NoTitleBar"  用系统桌面为应用程序背景,且无标题栏
?android:theme="Theme.Wallpaper.NoTitleBar.Fullscreen"  用系统桌面为应用程序背景,无标题栏,全屏
?android:theme="Translucent"
?android:theme="Theme.Translucent.NoTitleBar" 半透明,无标题
?android:theme="Theme.Translucent.NoTitleBar.Fullscreen" 半透明,无标题,全屏
?android:theme="Theme.Panel" 面板风格显示
?android:theme="Theme.Light.Panel" 平板风格显示

===================================================================================================

用到了Android的主题和风格,感觉很多地方需要总结和记录下来。其实主题和风格是有很大的作用的,特别是界面要求比较高的客户端。

Style:是一个包含一种或者多种格式化属性的集合,我们可以将其用为一个单位用在布局XML单个元素当中。比如,我们可以定义一种风格来定义文本的字号大小和颜色,然后将其用在View元素的一个特定的实例。 

如何定义style?
style也属于resource,所以要在resource下定义,就像定义string,color一样
定义style,需要指定name,style通常包含一个或多个item,每个item的name是android view的属性的名字,值则是对应相关属性的值
可以给style指定parent,从而可以继承和覆盖parent style的属性,parent取值是另外一个style,如果是继承自自己定义的style,只需要在命名style时增加前缀,这个前缀就是即将继承的style的名字

例如CodeFont是一个自己定义的style,那么下面的style,CodeFont.Red,则继承了CodeFont,只是文本的颜色修改成了红色

  1. <style name="CodeFont.Red"> 红色  
  2.         <item name="android:textColor">#FF0000</item>   
  3.  </style>  
  4.  <style name="CodeFont.Red.Big">  红色,并且大字体  
  5.         <item name="android:textSize">30sp</item>   
  6.  </style>  

也可以继承平台的style,可继承的样式请参照绍docs/guide/topics/ui/themes.html#PlatformStyles

  1. <style name="CodeFont" parent="@android:style/TextAppearance">   

如果父样式的值不符合你的需求,你也可以对它进行修改,和CSS中的覆盖效果一样,都是以最后的为准,  

在style中可以定义的属性
都有哪些属性在style的定义里是有效的呢?具体请参考docs/reference/android/R.attr.html
在view上使用style时,对view有效的属性起作用,无效的则会忽略
有一些属性对view无效,只对theme有效,在R.attr定义中以window开头的一些属性只对theme有效

style的使用
如果给view指定style,那么这个style只对该view有效
如果给viewgroup指定style,那么viewgroup下的元素也不会应用这个style,除非特别指定
给view指定style时,没有android:前缀,而只是style

下面是具体用法:

首先在res/values下新建一style.xml文件:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <style name="TitleStyle">  
  4.         <item name="android:textSize">18sp</item>  
  5.         <item name="android:textColor">#ec9237</item>  
  6.     </style>  
  7.     <style name="Title" parent="@style/TitleStyle">  
  8.         <item name="android:textSize">5sp</item>  
  9.     </style>  
  10. </resources>  

在layout.xml中的应用:

  1. <EditText android:layout_height="wrap_content"   
  2.     android:text="EditText"   
  3.     style="@style/Title"  
  4.     android:layout_width="fill_parent"   
  5.     android:id="@+id/editText1"></EditText>  

其实style就像是一组属性的组合, 可以看做当在view中引用style时,是顺序执行style中的item里面的每个属性,对view进行设定而已。因为可能有多个view都是需要设置相同的属性,。所以把这些view的属性单独写出,提高重用性。 

theme:就像风格一样,主题依然在<style>元素里边申明,也是以同样的方式引用。不同的是你通过在Android
Manifest中定义的<application>和<activity>元素将主题添加到整个程序或者某个Activity,但是主题是
不能应用在某一个单独的View里,所以配置文件的属性也就是窗口等的主题样式。
 

定义一个主题:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <style name="theme1">  
  4.         <item name="android:windowNoTitle">true</item>  
  5.         <item name="android:windowFullscreen">?android:windowNoTitle</item>  
  6.     </style>  
  7. </resources>  

下面代码显示在AndroidManifest.xml中如何为应用设置上面定义的主题:

  1. <application android:icon="@drawable/icon" android:label="@string/app_name"    
  2.     android:theme="@style/theme1">    
  3.     <activity android:name=".MessageShowActivity" android:label="@string/app_name"    
  4.         android:windowSoftInputMode="adjustPan" android:screenOrientation="portrait"    
  5.         android:theme="@style/theme2">    
  6.     </activity>    
  7. </application>  

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

setTheme(R.style.theme1);

注意:我们用了@符号和?符号来应用资源。@符号表明了我们应用的资源是前边定义过的(或者在前一个项目
中或者在Android 框架中)。问号?表明了我们引用的资源的值在当前的主题当中定义过。

style和theme的区别:

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

另外android系统也定义了一些主题,例如:

<activity android:theme="@android:style/Theme.Dialog">,该主题可以让Activity看起来像一个对话框,

<activity android:theme="@android:style/Theme.Black.NoTitleBar">Variant of the light theme with no title bar,系统自带的黑色主题。如果需要查阅这些主题,可以在文档的reference-->android-->R.style
中查看。

==================================================================================================

Android学习笔记之Styles andThemes

一、前沿    

通过使用style和theme可以将

<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" />

style应用于某一固定的View,Theme应用于整个的Activity或者Application

二、定义Style

         Style需定义在/res/values下的xml文件中。例如:

<?xml version="1.0"encoding="utf-8"?>

<resources>

    <style name="CodeFont"parent="@android:style/TextAppearance.Medium">

       <itemname="android:layout_width">fill_parent</item>

       <itemname="android:layout_height">wrap_content</item>

       <item name="android:textColor">#00FF00</item>

        <itemname="android:typeface">monospace</item>

    </style>

</resources>

2.1 style的继承

         如果要继承android内置的style需要定义parent属性,如上面所示。如果需要继承自定义的style则不需要parent属性。name中表明即可。如下,该style继承了自定义的name为CodeFont的style,并重写了一些属性:

<stylename="CodeFont.Red.Big">

       <item name="android:textSize">30sp</item>

       <item name="android:textColor">#FF0000</item>

</style>

 

        

三、在UI上应用style和theme

3.1 在view上应用style

<TextView

    style="@style/CodeFont"

   android:text="@string/hello" />

3.2 在activity或application上应用theme

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

如果想使activity具有Dialog的风格的话,可以这样:

<activity android:theme="@android:style/Theme.Dialog">

想让背景透明的话,可以:

<activity android:theme="@android:style/Theme.Translucent">

如果想定制系统自带的theme的话,可以:

<colorname="custom_theme_color">#b0b0ff</color>

<style name="CustomTheme" parent="android:Theme.Light">

   <itemname="android:windowBackground">@color/custom_theme_color</item>

   <itemname="android:colorBackground">@color/custom_theme_color</item>

</style>

上面的例子中引用了另一个color资源,因为属性android:colorBackground只支持引用。

3.3 根据平台版本自动选择theme

         根据平台不同自动切换theme,比如定义了theme:

<stylename="LightThemeSelector" parent="android:Theme.Light">

</style>

如果想让该theme在version11中变换style,可以在res/velues-11下定义xml文件,包含如下内容:

<stylename="LightThemeSelector"parent="android:Theme.Holo.Light">

</style>

四、使用自带的style和theme

Android自带了大量的style和theme供选择使用,也可以个性化定制

 

 

抱歉!评论已关闭.