首先,我們來確定下style和Theme都是做什麼用的?
我們都知道,我們可以通過在main.xml或者其他的布局檔案中通過屬性設定來設定控制項的樣式,這就導致了我們基本上要為每一個控制項都一一的設定屬性,更讓人惱火的是,這些屬性裡面的很多都是相同的,欲哭無淚啊!
在這個程式員天天期待救世主出現的年代,天空一聲驚雷,style和Theme憑空降臨了,幫我們解決了這個天大的難題。style允許我們可以將一些公用的屬性設定放到一起,然後定義一個style名稱,我們在控制項中想使用這些設定的時候,只需要調用下這個style就可以了。至於Theme,你可以認為它是一種特殊的style。
1)、style一般用來控制控制項的屬性控制
2)、Theme一般是用來控制視窗的屬性設定
--------------------------------------------------------------------
下面我們就一起來介紹下,具體的應用辦法:
問題一:我們的style設定都放在那裡呢?
我們的style(包括Theme)都存放在res/values/*.xml中,這裡的xml檔案名稱我們可以隨意,但是為了規範,我們一般都使用style.xml或者styles.xml
style程式碼範例:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<style name="TextView">
<item name="android:textSize">18sp</item>
<item name="android:textColor">#008</item>
<item name="android:shadowColor">@android:color/black</item>
<item name="android:shadowRadius">2.0</item>
</style>
<style name="EditText">
<item name="android:shadowColor">@android:color/black</item>
<item name="android:shadowRadius">1.0</item>
<item name="android:background">@android:drawable/btn_default</item>
<item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
</style>
<style name="Button">
<item name="android:background">@android:drawable/edit_text</item>
<item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
</style>
</resources>
xml中我們定義了三個style,當我們在程式中調用的時候,便可以通過style的name屬性值來調用。(注意:我們說過了style.xml的名稱可以隨意定義,但是裡面標籤的結構卻不是隨意的,想定義style,則必須在resources標籤下定義style標籤,內容的子屬性通過item來進行設定)
我們再來看看一個Theme的樣本:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme" parent="android:Theme.Light">
<item name="android:windowFullscreen">true</item>
<item name="android:windowTitleSize">160dip</item>
<item name="android:windowTitleStyle">@style/WindowTitle</item>
</style>
<style name="WindowTitle" parent="android:WindowTitle">
<item name="android:singleLine">true</item>
<item name="android:shadowColor">#BB000000</item>
<item name="android:shadowRadius">5.75</item>
</style>
</resources>
大家發現了沒有,一模一樣的結構!這也是我剛才為什麼說,其實Theme就是一種特殊style的原因。
-----------------------------------------------------------------
我們再來看看如何調用我們定義好的style。這裡假設,我們有一個和EditText相關的style,該style的name屬性為style_editText,有一個和視窗設定相關的Theme,該Theme的名稱為theme_ok。那麼我們如何使用它們呢?
第一種:為某個特定的EditText使用style_editText。
<EditText
android:id="@+id/myEditText"
android:layout_width="fill_parent"
android:layout_height="wrap+parent"
style="@style/style_editText"
android:hit="歡迎來到chenzheng_java部落格"
/>
第二種:在程式中使用主題theme_ok
public class ThemeStyleActivity extends Activity {</p><p>@Override<br />protected void onCreate(Bundle savedInstanceState) {<br />super.onCreate(savedInstanceState);<br />setTheme(R.style.theme_ok);<br />setContentView(R.layout.main);<br />}<br />}<br />
我們可以看到,我們通過setTheme方法來實現的。
第三種:為整個應用或者某個activity添加主題
為整個應用添加主題,我們可以在AndroidManifest.xml中通過為<application>標籤添加實行android:theme屬性來實現。例如android:theme="@style/Theme_ok"
為activity添加主題,我們可以在AndroidManifest.xml中通過為<activity>標籤添加實行android:theme屬性來實現。例如android:theme="@style/Theme_ok"
---------------------------------------------------------------------------