android 主題和樣式-style和Theme的區別和使用

來源:互聯網
上載者:User

標籤:android 主題 樣式 theme

項目中經常使用style和Theme,但卻從來沒有考慮過它們的區別,只會copy來copy去的,有時候還有些迷茫,為了徹底告別迷茫,現把這兩者的區別和使用總結出來,供自己和大夥參考

一.範圍

Theme是針對表單層級的,改變表單樣式。

Style是針對表單元素層級的,改變指定控制項或者Layout的樣式

二.使用方式

Theme

1. 在res\values\ 下建立themes.xml或者styles.xml檔案

2. 添加<resouces>節點(根節點)

3. 添加自訂的style

4.(1)在AndroidManifest.xml檔案中,為Activity指定theme屬性(推薦)

    (2)Activity建立時調用setTheme函數 (必須在setContentView前調用 )

<style name="MyBubbleTheme" parent="@android:style/Theme.Dialog" ><item name="android:windowBackground">@drawable/bubble</item></style>

 <activity    android:name=".BlurThemeActivity"    android:theme="@style/MyBubbleTheme" />

5.系統內建的Theme

android:theme="@android:style/Theme.Dialog"   //將一個Activity顯示為能話框模式android:theme="@android:style/Theme.NoTitleBar"  //不顯示應用程式標題欄android:theme="@android:style/Theme.NoTitleBar.Fullscreen"  //不顯示應用程式標題欄,並全屏android:theme="@Theme.Light"  //背景為白色android:theme="Theme.Light.NoTitleBar"  //白色背景並無標題列 android:theme="Theme.Light.NoTitleBar.Fullscreen"  //白色背景,無標題列,全屏android:theme="Theme.Black"  //背景黑色android:theme="Theme.Black.NoTitleBar"  //黑色背景並無標題列android:theme="Theme.Black.NoTitleBar.Fullscreen"    //黑色背景,無標題列,全屏android:theme="Theme.Wallpaper"  //用系統案頭為應用程式背景android:theme="Theme.Wallpaper.NoTitleBar"  //用系統案頭為應用程式背景,且無標題列android:theme="Theme.Wallpaper.NoTitleBar.Fullscreen"  //用系統案頭為應用程式背景,無標題列,全屏

6.常用屬性

<item name="windowBackground">@android:drawable/screen_background_dark</item>  <item name="windowFrame">@null</item>  <item name="windowNoTitle">false</item>  <item name="windowFullscreen">false</item>  <item name="windowIsFloating">false</item>  <item name="windowContentOverlay">@android:drawable/title_bar_shadow</item>  <item name="windowTitleStyle">@android:style/WindowTitle</item>  <item name="windowTitleSize">25dip</item>  <item name="windowTitleBackgroundStyle">@android:style/WindowTitleBackground</item>  <item name="android:windowAnimationStyle">@android:style/Animation.Activity</item>  

style

1. 在res\values\ 下建立styles.xml檔案

2. 添加<resouces>節點(根節點)

3. 添加自訂的style

4.在特定控制項或layout中添加style屬性

<style name="MyTextStyle">        <item name="android:textColor">#FF0000C0</item>        <item name="android:layout_width">wrap_content</item>        <item name="android:layout_height">wrap_content</item>    </style>
     <TextView    style="@style/MyTextStyle"  />


樣本:

很多程式剛啟動的時候的Tips介面,氣泡視窗和毛半透明效果


代碼如下:

BlurThemeActivity.java

import android.app.Activity;import android.os.Bundle;import android.view.WindowManager;public class BlurThemeActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        // Have the system blur any windows behind this one.        getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,                WindowManager.LayoutParams.FLAG_BLUR_BEHIND);    }}

themes.xml

 <style name="MyBubbleTheme" parent="@android:style/Theme.Dialog" >        <item name="android:windowBackground">@drawable/bubble</item>        <item name="android:windowNoTitle">true</item>    </style>

AndroidManifest.xml

 <activity    android:name=".BlurThemeActivity"    android:theme="@style/MyBubbleTheme" />


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.