欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于android樣式與主題(style&theme)的詳解

 更新時間:2013年06月15日 10:43:13   作者:  
本篇文章是對android中的樣式與主題(style&theme)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
android 中的樣式和 CSS 樣式作用相似,都是用于為界面元素定義顯示風(fēng)格,它是一個包含一個或者多個view 控件屬性的集合。如:需要定義字體的顏色和大小。
在 CSS 中是這樣定義的:
<style>
    .itcast{COLOR:#0000CC;font-size:18px;}
</style>
可以像這樣使用上面的 css 樣式: <div class="itcast"> 傳智播客 </div>
在 Android 中可以這樣定義樣式:
在 res/values/styles.xml 文件中添加以下內(nèi)容
復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name=“itcast”> <!-- 為樣式定義一個全局唯一的名字 -->
        <item name="android:textSize">18px</item> <!-- name 屬性為樣式要用在的 View 控件持有的屬性 -->
        <item name="android:textColor">#0000CC</item>
    </style>
</resources>

在 layout 文件中可以像下面這樣使用上面的 android 樣式:
復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ....>
    <TextView style="@style/itcast"
        .....  />
</LinearLayout>

<style> 元素中有一個 parent 屬性。這個屬性可以讓當(dāng)前樣式繼承一個父樣式,當(dāng)前樣式可以繼承到父樣式的值。當(dāng)然,如果父樣式的值不符合你的需求,你也可以對它進(jìn)行修改,如下:
復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="itcast">
        <item name="android:textSize">18px</item> <!-- name 屬性為樣式要用在的 View 控件持有的屬性 -->
        <item name="android:textColor">#0000CC</item>
    </style>
    <style name="subitcast" parent="@style/itcast">
        <item name="android:textColor">#FF0000</item>
    </style>
</resources>

android 中主題也是用于為應(yīng)用定義顯示風(fēng)格,它的定義和樣式的定義相同,如下:
復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name=“itcastTheme">
               <item name=“android:windowNoTitle”>true</item> <!– 沒標(biāo)題 à
               <item name=“android:windowFullscreen”>?android:windowNoTitle</item> <!– 全屏顯示 à
</style>
</resources>

上面“ ?android:windowNoTitle” 中的問號用于引用在當(dāng)前主題中定義過的資源的值。下面代碼顯示在AndroidManifest.xml 中如何為應(yīng)用設(shè)置上面定義的主題:
復(fù)制代碼 代碼如下:

<application android:icon="@drawable/icon" android:label="@string/app_name"
     android:theme="@style/itcastTheme">
   ......
</application>

除了可以在 AndroidManifest.xml 中設(shè)置主題,同樣也可以在代碼中設(shè)置主題,如下:
setTheme(R.style.itcastTheme);
盡管在定義上,樣式和主題基本相同,但是它們使用的地方不同。樣式用在單獨(dú)的 View ,如: EditText 、TextView 等;主題通過 AndroidManifest.xml 中的 <application> 和 <activity> 用在整個應(yīng)用或者某個 Activity,主題對整個應(yīng)用或某個 Activity 進(jìn)行全局性影響。如果一個應(yīng)用使用了主題,同時應(yīng)用下的 view 也使用了樣式,那么當(dāng)主題與樣式屬性發(fā)生沖突時,樣式的優(yōu)先級高于主題。

另外 android 系統(tǒng)也定義了一些主題,例如: <activity android:theme=“@android:style/Theme.Dialog”> ,該主題可以讓 Activity 看起來像一個對話框,如果需要查閱這些主題,可以在文檔的 reference à android-->R.style 中查看。

相關(guān)文章

最新評論