Android主题Theme

简述:

主题Theme就是用来设置界面UI风格,可以设置整个应用或者某个活动Activity的界面风格。在Android SDK中内置了下面的Theme,可以按标题栏Title Bar和状态栏Status Bar是否可见来分类:

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" 用系统桌面为应用程序背景,无标题栏,全屏
android:theme="Translucent" 半透明
android:theme="Theme.Translucent.NoTitleBar" 半透明、无标题栏
android:theme="Theme.Translucent.NoTitleBar.Fullscreen" 半透明、无标题栏、全屏
android:theme="Theme.Panel"
android:theme="Theme.Light.Panel"
这些主题可以应用到整个应用Application范围或者某个活动Activity范围中。

应用Application范围
在AndroidManifest.xml中的application节点中设置theme属性,主题theme应用到整个应用程序中。
<application
Android:icon=”@drawable/icon”
Android:icon=”@string/app_name”
Android:icon=”@android:style/ Theme.Black.NoTitleBar”>

活动Activity范围
使用Java代码或者在AndroidManifest.xml中对活动Activity的主题进行设置,主题仅应用到当前活动中。
在AndroidMainifest.xml设置方法:
<activity
android:name=“.About”
android:label=“@string/app_name”
android:theme=“@android:style/ Theme.Black.NoTitleBar” >

使用java代码进行设置,在当前活动Activity的onCreate中进行设置:
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setTheme(android.R.style.Theme_Translucent_NoTitleBar);
setContentView(R.layout.main);
}

动态设置Theme

启动Activity但不显示其界面
    由于启动浏览器打开网站通过TestActivity这个activity启动的,因此当打开这个app时会首先进入TestActivity,尽管没有为其设置布局,但也会显示一个空白界面,所以会出现上述屏幕闪烁一下的现象。
     怎样避免这个现象,能使打开app时绕开testActivity的空白显示直接进入网站?
     在activity标签下可以配置名字叫做theme的属性,该属性可以配置各种各样的显示样式,其中一个属性值是Theme_NoDisplay,简单来说,配置为该属性值可以不显示任何东西,仅仅是运行该activity。因此在配置文件AndroidManifest.xml中的activity标签下添加如下一句:
    android:theme="@android:style/Theme.NoDisplay"
    就解决了屏幕闪烁的问题。
    PS:借鉴([Andriod开发] 启动程序时不显示主Activity的方法)http://www.189works.com/article-54326-1.html
     另作者benben还总结了theme各属性值的使用,(android Theme使用总结)http://www.189works.com/article-30428-1.html
    http://blog.sina.com.cn/s/blog_76721b1201017joi.html
setTheme(android.R.style.Theme_NoDisplay)
http://blog.csdn.net/hymking/article/details/53559761
Android动态切换主题
    要想主题生效,有一点非常重要:“setTheme一定要在setContentView之前被调用”
    http://blog.csdn.net/gulumi_mmga/article/details/46516353
    关于设置主题的注意事项:
    不少同学会发泄setTheme()竟然会无效。那么注意
    使用setTheme()只能在Oncreate()之前使用。在setContentView(),还是不行那么就在super.onCreate(savedInstanceState);之前
    如果要使用动态切换主题,那么就必须调用actvity.finish()。然后再重新加载setTheme()
http://blog.csdn.net/wsscy2004/article/details/7562909

猜你喜欢

转载自blog.csdn.net/u010144805/article/details/81365787