android 中theme和style的文法相關

來源:互聯網
上載者:User

標籤:

1.theme和style都是一組屬性的集合,用於定義文本、顏色、大小等顯示風格。他們都是資源,可以用android系統層級的一些預設的風格和佈景主題資源,你也可以自訂你自己的主題和風格資源。

2.自訂style

 1 <resources> 2  <style name="CustomTheme"> 3  <item name="android:windowNoTitle">true</item> 4  <item name="windowFrame">@drawable/screen_frame</item> 5  <item name="windowBackground">@drawable/screen_background_white</item> 6  <item name="panelForegroundColor">#FF000000</item> 7  <item name="panelBackgroundColor">#FFFFFFFF</item> 8  <item name="panelTextColor">?panelForegroundColor</item> 9  <item name="panelTextSize">14</item>10  <item name="menuItemTextColor">?panelTextColor</item>11  <item name="menuItemTextSize">?panelTextSize</item>12  </style>13 </resources>

注意:@符號表明了我們應用的資源是前邊定義過的(或者在前一個項目中或者在Android 架構中)。?符號表明了我們引用的資源的值在當前的主題當中定義過。通過引用在<item>裡邊定義的名字可以找到(panelTextColor 用的顏色和panelForegroundColor中定義的一樣)。這中技巧只能用在XML資源當中。

3.自訂Style(2)

    <style name="AppBaseTheme" parent="android:Theme"></style>    <style name="AppTheme" parent="AppBaseTheme">        <item name="android:textSize">50sp</item>    </style>    <style name="AppTheme.Test" parent="android:Theme.Light">        <item name="android:textColor">#ff0000</item>    </style>    <style name="AppTheme.Test.Blue">        <item name="android:textColor">#0000ff</item>    </style>    <style name="AppTheme.Test.Blue.New">        <item name="android:textColor">#0000ff</item>        <item name="android:textSize">30sp</item>    </style>

   parent欄位標明改style從哪種style繼承過來,然後我們就可以在該style 內複寫父style中的一些風格,比如字型、顏色etc。

  以下有幾點要注意一下:

    1.第三種style是通過A.B這樣的形式定義的,這是繼承的另外一種簡易的寫法。但只要有parent屬性,那麼parent會完全覆蓋A的所有屬性,即A寫不寫都是一樣的,該自

     定義style 不會從A繼承到任何風格,這裡用上面的例子運行一下就可以看出來;

    2.如果自訂的style沒有parent屬性,那麼style的name中的A部分可以不寫,這表明這是我們自訂的style,但是如果用簡易的寫法來寫,而且又不想寫parent的話

             那麼A必須是聲明過的,否則會報錯“A主題找不到”;

    3.簡易的寫法支援多繼承。

3.接下來我們就可以在manifest中,或者指定的空間直接使用我們自訂的style 了。

  

android 中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.