android 主题和样式-style和Theme的区别和使用

来源:互联网 发布:明礼仪知荣辱演讲稿 编辑:程序博客网 时间:2024/04/24 00:07

项目中经常使用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" />


0 0
原创粉丝点击