Style(样式)
什么是样式?
为每个View重复地指定字体,颜色等属性,无疑会增加大量的代码,而且不利于我们后期项目的维护,所以就引入样式(Style)
学过web的都知道,我们可以通过css的选择器对html中的元素进行设置;而在UI组件中,我们可以通过style属性来指定
样式。
Style的使用步骤:
样式资源文件都放在res/values目录下,根元素,<resources/>;可包含多个<style/>子元素,每个style可定义一个样式;
有如下两个属性,name:样式名 parent:继承父样式,当然也可以覆盖
使用示例:
代码示例:
①定义一个样式文件 my_style.xml
- <span style="font-family:Comic Sans MS;"><?xml version="1.0" encoding="utf-8"?>
- <resources xmlns:android="http://schemas.android.com/apk/res/android">
- <!-- 定义一个样式指定文本的背景,字体大小 -->
- <style name="sty1">
- <item name="android:background">#574867</item>
- <item name="android:textSize">50sp</item>
- </style>
- <!-- 定义一个样式,继承第一个样式的所有属性,并且设置字体颜色,覆盖背景颜色 -->
- <style name="sty2" parent="@style/sty1">
- <item name="android:textColor">#471862</item>
- <!-- 覆盖父样式的背景颜色属性 -->
- <item name="android:background">#ff</item>
- </style>
- </resources></span>
代码分析:定义了两个样式,第二个样式继承了第一个样式的所有属性,并且覆盖了父类的背景属性
②在另一个xml文件中使用该样式:
- <span style="font-family:Comic Sans MS;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/LinearLayout1"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context=".MainActivity" >
- <TextView
- style="@style/sty1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/hello_world" />
- <TextView
- style="@style/sty2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/hello_world" />
- </LinearLayout></span>
③运行截图:
Theme(主题)
什么是主题?
与前面的样式类似,区别在于,主题是对整个应用中所有的Activity都起作用,或者对指定的Activity起作用
定义格式通常是改变窗口的外观格式,标题,边框等
Theme的使用步骤:
根元素依旧为<resource/>,使用<style/>来定义主题
使用实例:
效果图:
这里的话我们在主题中设置了没有标题,覆盖整个屏幕,以及设置了绿色的边框设置了内边距,以及填充了颜色(我们用了透明的颜色):
在#xxxxxx的前面加上两位数表示透明度
代码实例:
①建立一个shapeDrawable.xml资源文件,用于绘制边框
border.xml:
- <span style="font-family:Comic Sans MS;"><?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android"
- android:shape="rectangle" >
- <!-- 设置边框 -->
- <stroke android:width="5dp" android:color="#86D04E"/>
- <!-- 设置内边距 -->
- <padding
- android:left="5dp"
- android:right="5dp"
- android:top="5dp"
- android:bottom="5dp"
- />
- <!-- 设置填充颜色 -->
- <solid android:color="#00000000"/>
- </shape></span>
②自定义主题文件,MyTheme
- <span style="font-family:Comic Sans MS;"><?xml version="1.0" encoding="utf-8"?>
- <resources>
- <style name="MyTheme">
- <item name="android:windowNoTitle">true</item>
- <item name="android:windowFullscreen">true</item>
- <item name="android:windowFrame">@drawable/border</item>
- <item name="android:windowBackground">@drawable/back</item>
- </style>
- </resources></span>
③调用该主题:
调用方法有以下几种:
1)这只在AndroidManifest.xml文件中,为Activity指定主题,或者为整个应用设置主题,在<applicaiton/>元素添加android:theme = "MyTheme"即可
2)在Java代码中设置,如下:
- <span style="font-family:Comic Sans MS;">package com.jay.example.themedemo;
- import android.os.Bundle;
- import android.app.Activity;
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setTheme(R.style.MyTheme);
- setContentView(R.layout.activity_main);
- }
- }
- </span>
当然theme和style一样可以继承哦,这里就不多解释了...
源码下载:
style实例源码:http://pan.baidu.com/s/1o64nEeI
theme实例源码:
http://pan.baidu.com/s/1sjtYkWX