標籤:
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屬性指定的父樣式為準.
第七個是不帶任何父樣式的自訂樣式。
Android學習隨筆之UI美化(一)--------- 樣式和主題