1、样式和主题的使用

样式和主题是一样的,都是一系列属性的集合,只是应用的对象不同,样式应用在单个控件上,如果TextView,Button等.主题应用在功能清单文件中的<application>或者<activity>标签上以属性的形式出现,如:

<application         theme=”XXX”/>

<activity     theme=”XXX”/>

 

来看一个清单文件:

在这里要明确两点:

  1、主题是应用在application或者activity标签之上的,用于设置整个应用程序或者某个Activity的整体外观属性.

  2、如果同时指定了主题,以activity标签上的主题为准

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.day27_uitest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="17"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
             android:theme="@style/AppTheme"  >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 2、自定义style (在这里我的理解:这里的style就像css<层叠样式表>,用来规范一些外观)

      MyStyle。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="Theme.AppCompat.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>

    <!-- 对应用程序定制一个样式
            使用parent属性指定当前样式的父样式,这种方式即可以引用系统样式,
         也可以引用用户自定义样式  
     -->
    <style name="MyAppTheme" parent="AppTheme">

        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
        <item name="android:textColor">@android:color/white</item>
    </style>
    <!-- 对当前的界面Activity定制一个样式,在acvitity中定义的样式,对界面中所有的控件都有效 -->
    <style name="MyMainActivityTheme" parent="MyAppTheme">
        <item name="android:textSize">10sp</item>
        <item name="android:textColor">#00FF00</item>
    </style>
    <!-- 单独对布局文件中的控件的样式进行定制
    
            使用父样式做前缀生成子样式,但这个用法只能引用用户自定义样式,
           不能引用系统样式.换句话来讲,引用系统样式只能使用parent属性.            
           如果同时使用自定义样式做前缀和parent属性,则以parent属性指定的父样式为准.
        
     -->
    <style name="MyMainActivityTheme.MyTextViewStyle" >
         <item name="android:textSize">20sp</item>
         <item name="android:textColor">#FF0000</item>
    </style>
    <style name="MyMainActivityTheme.MyTextViewStyle2">
         <item name="android:textSize">40sp</item>
        <item name="android:textColor">#0000FF</item>
    </style>
    
    <style name="style_mine">
        <!-- ctrl+alt+向下的方向键完成整行复制 -->
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
    </style>
    

</resources>

上面这个文件中:第一和第二个style是系统自带的样式

第三个第四个是带有parent(父样式)的自定义样式

第五个第六个是带有父样式前缀的自定义样式 ,此时应注意:

  如果同时使用自定义样式做前缀和parent属性,则以parent属性指定的父样式为准.


第七个是不带任何父样式的自定义样式。

posted on 2015-10-16 11:06  Fann0221  阅读(412)  评论(0编辑  收藏  举报