android的Style(样式)与Theme(主题)的使用

来源:互联网 发布:怎么维护代理商 知乎 编辑:程序博客网 时间:2024/06/05 15:12

Style(样式)


什么是样式?

为每个View重复地指定字体,颜色等属性,无疑会增加大量的代码,而且不利于我们后期项目的维护,所以就引入样式(Style)
学过web的都知道,我们可以通过css的选择器对html中的元素进行设置;而在UI组件中,我们可以通过style属性来指定
样式。



Style的使用步骤:


样式资源文件都放在res/values目录下,根元素,<resources/>;可包含多个<style/>子元素,每个style可定义一个样式;
有如下两个属性,name:样式名   parent:继承父样式,当然也可以覆盖


使用示例:

代码示例:

①定义一个样式文件 my_style.xml
[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="font-family:Comic Sans MS;"><?xml version="1.0" encoding="utf-8"?>  
  2. <resources xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <!-- 定义一个样式指定文本的背景,字体大小 -->  
  4.     <style name="sty1">  
  5.         <item name="android:background">#574867</item>  
  6.         <item name="android:textSize">50sp</item>  
  7.     </style>  
  8.     <!-- 定义一个样式,继承第一个样式的所有属性,并且设置字体颜色,覆盖背景颜色 -->  
  9.     <style name="sty2" parent="@style/sty1">  
  10.         <item name="android:textColor">#471862</item>  
  11.         <!-- 覆盖父样式的背景颜色属性 -->  
  12.         <item name="android:background">#ff</item>  
  13.     </style>  
  14. </resources></span>  

代码分析:定义了两个样式,第二个样式继承了第一个样式的所有属性,并且覆盖了父类的背景属性

②在另一个xml文件中使用该样式:

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="font-family:Comic Sans MS;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:id="@+id/LinearLayout1"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical"  
  7.     tools:context=".MainActivity" >  
  8.   
  9.     <TextView  
  10.         style="@style/sty1"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:text="@string/hello_world" />  
  14.     <TextView  
  15.         style="@style/sty2"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:text="@string/hello_world" />  
  19. </LinearLayout></span>  

③运行截图:




Theme(主题)


什么是主题?

与前面的样式类似,区别在于,主题是对整个应用中所有的Activity都起作用,或者对指定的Activity起作用
定义格式通常是改变窗口的外观格式,标题,边框


Theme的使用步骤:


根元素依旧为<resource/>,使用<style/>来定义主题


使用实例:



效果图:


这里的话我们在主题中设置了没有标题,覆盖整个屏幕,以及设置了绿色的边框设置了内边距,以及填充了颜色(我们用了透明的颜色):
在#xxxxxx的前面加上两位数表示透明度

代码实例:
①建立一个shapeDrawable.xml资源文件,用于绘制边框
border.xml:

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="font-family:Comic Sans MS;"><?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:shape="rectangle" >  
  4.     <!-- 设置边框 -->  
  5.     <stroke android:width="5dp" android:color="#86D04E"/>  
  6.     <!-- 设置内边距 -->  
  7.     <padding   
  8.         android:left="5dp"  
  9.         android:right="5dp"  
  10.         android:top="5dp"  
  11.         android:bottom="5dp"  
  12.     />  
  13.     <!-- 设置填充颜色 -->  
  14.     <solid android:color="#00000000"/>  
  15. </shape></span>  


自定义主题文件,MyTheme

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="font-family:Comic Sans MS;"><?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <style name="MyTheme">  
  4.         <item name="android:windowNoTitle">true</item>  
  5.         <item name="android:windowFullscreen">true</item>  
  6.         <item name="android:windowFrame">@drawable/border</item>  
  7.         <item name="android:windowBackground">@drawable/back</item>  
  8.     </style>  
  9. </resources></span>  

调用该主题:
调用方法有以下几种:
1)这只在AndroidManifest.xml文件中,为Activity指定主题,或者为整个应用设置主题,在<applicaiton/>元素添加android:theme = "MyTheme"即可
2)在Java代码中设置,如下:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="font-family:Comic Sans MS;">package com.jay.example.themedemo;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5.   
  6. public class MainActivity extends Activity {  
  7.   
  8.     @Override  
  9.     protected void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setTheme(R.style.MyTheme);  
  12.         setContentView(R.layout.activity_main);  
  13.           
  14.     }  
  15. }  
  16. </span>  

当然theme和style一样可以继承哦,这里就不多解释了...

源码下载:
style实例源码:http://pan.baidu.com/s/1o64nEeI
theme实例源码:http://pan.baidu.com/s/1sjtYkWX
0 0
原创粉丝点击