样式和主题概述
共同点:(1)定义的方式是一样的
不同点:(1)style作用范围比较窄(控件 button textview),(2)theme作用在activity或者Application节点下范围比较宽
一、项目目录结构
二、activity_main.xml界面
三、activity_main.xml代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.zgs.StyleAndTheme.MainActivity" >
<!-- 以下设置了自己定义的样式 -->
<TextView
style="@style/my_style"
android:text="aaa" />
<TextView
style="@style/my_style"
android:text="bbb" />
<TextView
style="@style/my_style"
android:text="ccc" />
<TextView
style="@style/my_style"
android:text="ddd" />
</LinearLayout>
四、styles.xml代码<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<style name="my_style">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#ff0000</item>
<item name="android:textSize">45sp</item>
</style>
<style name="my_theme">
<item name="android:background">#ADFF2F</item>
</style>
</resources>
特殊说明:样式和主题不一定非得定义在styles.xml文件中,可任意新建个xml文件,然后在里面定义样式和主题。
五、AndroidManifest.xml代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zgs.StyleAndTheme"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="22" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/my_theme" > <!-- 此处设置了自己定义的主题 -->
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
六、效果演示