Android_08_样式和主题

来源:互联网 发布:js手动触发回车事件 编辑:程序博客网 时间:2024/06/07 10:06

工程目录 ----->  res  ----->   values ----->  styles.xml

在styles.xml文件中,定义了样式和主题,如下:

<resources xmlns:android="http://schemas.android.com/apk/res/android">    <!--        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="jiangnanstyle">        <item name="android:layout_width">wrap_content</item>        <item name="android:layout_height">wrap_content</item>        <item name="android:textSize">22sp</item>        <item name="android:textColor">#00ff00</item>    </style>        <style name="pangzhi" parent="jiangnanstyle">        <item name="android:textSize">30sp</item>    </style>        <style name="pangzhi.lizhi" >        <item name="android:textColor">#0000ff</item>    </style>        <style name="myTheme">        <item name="android:background">#ff0000</item>    </style></resources>


样式的使用:

工程目录 ----->  res  ----->   layout ----->  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:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity"     android:orientation="vertical"    >    <TextView        android:text="@string/hello_world"        style="@style/jiangnanstyle"        />    <TextView        android:text="@string/hello_world"        style="@style/pangzhi"        />    <TextView        android:text="@string/hello_world"        style="@style/pangzhi.lizhi"        />    <TextView        android:text="@string/hello_world"        style="@style/jiangnanstyle"        />    <TextView        android:text="@string/hello_world"        style="@style/jiangnanstyle"        /></LinearLayout>

注:

 对于在style.xml中的定义我们可以看到:

1>

<style name="jiangnanstyle">        <item name="android:layout_width">wrap_content</item>        <item name="android:layout_height">wrap_content</item>        <item name="android:textSize">22sp</item>        <item name="android:textColor">#00ff00</item>    </style>
这就是定义了一个样式,其没有继承任何的样式,在布局文件中,我们可以看到其使用方法是:

<TextView        android:text="@string/hello_world"        style="@style/jiangnanstyle"        />
2>
<style name="pangzhi" parent="jiangnanstyle">        <item name="android:textSize">30sp</item>    </style>
这种方式是继承了jiangnanstyle的样式,与此同时,将textSize的属性予以修改,其使用方法如下:

<TextView        android:text="@string/hello_world"        style="@style/pangzhi"        />

3>

<style name="pangzhi.lizhi" >        <item name="android:textColor">#0000ff</item>    </style>
这是通过 . 的方式实现样式的继承,这种继承方式使用比较普遍,其也是继承了pangzhi的样式,那么在使用时,其使用方法如下:

<TextView        android:text="@string/hello_world"        style="@style/pangzhi.lizhi"        />



主题的使用:

工程目录  ------>  AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.itheima.styletheme"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="17" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/myTheme" >        <activity            android:name="com.itheima.styletheme.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>

注:关于主题的定义,也是在style.xml中进行定义的,其使用场所和样式有所区别

在定义的时候,我们可以看到这样一个style:

<style name="myTheme">        <item name="android:background">#ff0000</item>    </style>
其和样式的定义方法一样

其在清单文件中的的使用可以看到:

<application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/myTheme" >        <activity            android:name="com.itheima.styletheme.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>


总结:

与样式资源非常相似,主题资源的XML文件通常也放在/res/values目录下,主题同样使用<style.../>元素来定义主题;

一言以蔽之:主题定义的地方和样式相同,但其在使用的场所上有所区别,主题是在清单文件中使用,样式是在布局文件中使用,如下:

1>主题不能作用于单个的View组件,主题应该对整个应用中的所有Activity起作用,或对指定的Activity起作用。  

2>主题定义的格式应该是改变窗口的外观的格式,例如窗口标题,窗口边框等

0 0
原创粉丝点击