1.主题和样式的区别主要区别在
-
主题不能作用于单个View组建,主题应该对整个应用中的所有Activity起作用或者对指定的Activity起作用。
-
主题定义的格式应该是改变窗口的外观格式,例如窗口变体,窗口边框等。
2.自定义主题
-
在/res/values/my_style.xml文件增加一个主题,定义主题<style.../>片段如下:
<style name="CrazyTheme" parent="@android:style/Theme.Dialog"> <item name="android:windowNoTitle">true</item> <item name="android:windowFullScreen">true</item> <item name="android:windowFrame">@drawable/window_border</item> <item name="android:windowBackground>@drawable/star</item> </style>
可以通过parent属性,继承原有的主题。
-
在定义上面的主题后,接下来在Java代码中使用该主图:
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTheme(android.R.style.CrazyitTheme); setContentView(R.layout.linear_layout_3); }
还可以在AndroidManifest.xml中对指定应用、指定Activity应用主题,这样更简单:
<application android:theme="@style/CrazyitTheme"> ... ... </application>
<activity android:theme="@android:style/Theme.Dialog"> </activity>
3.Android系统包含了很多系统定义好的theme。总结如下:
主题
说明
图例
Theme.NoTitleBar
不显示应用程序标题栏
![]()
Theme.NoTitleBar.Fullscreen
不显示应用程序标题栏,并全屏
![]()
Theme.Light
背景为白色
![]()
Theme.Light.NoTitleBar
白色背景并无标题栏
![]()
Theme.Light.NoTitleBar.Fullscreen
白色背景,无标题栏,全屏
![]()
Theme.Black
背景黑色
![]()
Theme.Black.NoTitleBar
黑色背景并无标题栏
![]()
Theme.Black.NoTitleBar.Fullscreen
黑色背景,无标题栏,全屏
![]()
Theme.Wallpaper
用系统桌面为应用程序背景
![]()
Theme.Wallpaper.NoTitleBar
用系统桌面为应用程序背景,且无标题栏
![]()
Theme.Wallpaper.NoTitleBar.Fullscreen
用系统桌面为应用程序背景,无标题栏,全屏
![]()
Theme.Translucent
透明背景
![]()
Theme.Translucent.NoTitleBar
透明背景并无标题
![]()
Theme.Translucent.NoTitleBar.Fullscreen
透明背景并无标题,全屏
![]()
Theme.Dialog
对话框形式显示
![]()
Theme.Panel
面板风格显示
![]()
Theme.Light.Panel
平板风格显示
![]()