步步為營_Android開發課[37]_使用者介面之theme和style

來源:互聯網
上載者:User

標籤:android   使用者介面   life   開發   application   

Focus on technology, enjoy life!—— QQ:804212028
瀏覽連結:http://blog.csdn.net/y18334702058/article/details/44624305

  • 主題:使用者介面之theme和style
    -theme和style很容易混淆,theme樣式是用於添加到application或者activity中去的。而style樣式是用於添加到view控制項中去的。

樣式的定義:

找到專案檔夾:res/valus/styles,styles.xml這個檔案就是專門用來定義樣式的一個檔案夾。沒有的話,自己建立一個。
代碼如下:

<?xml version="1.0" encoding="utf-8"?><resources>    <style name="AppTheme" parent="AppBaseTheme">        <item name="android:layout_width">fill_parent        <item name="android:layout_height">wrap_content        <item name="android:textColor">00FF00    </style></resources>

樣式的繼承:

開發人員都應該知道的一個樣式可以繼承平台原有的樣式,也可以繼承自訂的樣式。繼承平台原有的樣式如下

<style name="GreenText" parent="@android:style/TextAppearance">    <item name="android:textColor">00FF00</style>

繼承自訂樣式只要在style的name屬性上加一個首碼名就可以了。首碼名是自訂的那個style的名稱,例如Red樣式繼承CodeFont的樣式可以這樣寫:

<style name="CodeFont.Red">    <item name="android:textColor">FF0000</style>

這種繼承方式還可以不斷繼承下去,Big再繼承他們的樣式:

<style name="CodeFont.Red.Big">    <item name="android:textSize">30sp</style>

Style 和 Theme的使用:

//在控制項裡使用styles樣式<TextView    style="@style/CodeFont    android:text="@string/hello" />//在application中使用themes樣式<application android:theme="@style/MyTheme">//在activity中使用themes樣式<activity android:theme="@style/MyTheme">

什麼是Style,什麼是Theme?

Style:是一個包含一種或者多種格式化屬性的集合,我們可以將其用為一個單位用在布局XML單個元素當中。比如,我們可以定義一種風格來定義文本的字型大小大小和顏色,然後將其用在View元素的一個特定的執行個體。

Theme:是一個包含一種或者多種格式化屬性的集合,我們可以將其為一個單位用在應用中所有的Activity當中(也就是application中)或者應用在某個Activity當 中。

來看看他們的定義:

<?xml version="1.0" encoding="utf-8"?>   <resources>       <!-- 定義style --><style name="myTextStyle" mce_bogus="1">    <item name="android:textSize">20px</item>    <item name="android:textColor">EC9237</item></style>      <style name="myTextStyle2" mce_bogus="1">      <item name="android:textSize">14px</item>    <item name="android:textColor">FF7F7C</item></style>       <!-- 定義theme -->     <style name="myTheme" mce_bogus="1">    <item name="android:windowNoTitle">true</item>    <item name="android:textSize">14px</item>    <item name="android:textColor">FFFF7F7C</item></style>  </resources> 

從上面的例子可以看出,定義上style與定義theme基本相同,只是使用的地方不同,還有就是在一些標籤的使用上: style 作用於view,theme作用於application或者Activity。

Focus on technology, enjoy life!—— QQ:804212028
瀏覽連結:http://blog.csdn.net/y18334702058/article/details/44624305

步步為營_Android開發課[37]_使用者介面之theme和style

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.