android 样式(Style)和主题(Theme)

样式(Style)和主题(Theme)

开头语:在android很多地方要对控件的样式(如:ActionBar的样式)和Activity的样式进行设置,对应控件的样式设置是(Style)对Activity的样式设置是(Theme)

API是这样来解释Style的:

  A style resource defines the format and look for a UI.A style can be applied to an individual View (from within a layout file)

   or to an entire Activity or application (from within the manifest file)

下面就来说说怎么进行style设置:

1.在资源文件夹里创建好style.xml和theme.xml

2.在XML文件中都要添加一个<resources>根节点

3.在根节点里面<style>增加一个全局唯一的名字,也可以选择增加一个父类属性,继承已有的style

通过两个例子来说明Style和Theme

Style:

在Values的Style.xml中写:

1 <!-- TextView设定的简单样式 -->
2     <style name="TextStyle">    
3         <item name="android:textColor">#008</item>
4         <item name="android:textSize">18sp</item>
5     </style>

在MainActivity当中添加一个TextView

1  <TextView
2         style="@style/TextStyle"   
3         android:layout_width="wrap_content"
4         android:layout_height="wrap_content"
5         android:text="我是没有改变style的TextView" />

在没有写style="@style/TextStyle"的时候运行出来TextView的字体就是普通效果:

再加了style="@style/TextStyle"引用style.xml定义的样式后TextView显示的字体变成了:

则对View的样式添加成功了的

Theme:

现在Values.中添加一个Theme.xml文件,在Theme.xml的<Resource>根节点里面写上<Style>并定义一些样式,在ANDROID的SDK中已经定义了很多Theme。

 Theme是针对窗体级别的,改变窗体样式,如果你想对应用的所有Activity的样式进行设置就要在AndroidManifest.xml中对<Application>中添加android:theme属性

如:

<Application  android:theme="@style/AppTheme"></Application>即可

 若想修改指定Activity就在AndroidManifest.xml对指定Activity中添加android:theme属性

如:

<activity  android:theme="@style/AppTheme" ></activity>

Android SDK定义好的一些常用theme:

下面的前三个之外直接复制就会出错。@是说明系统已经定义过的,@android:style/  是必须带上的。

?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" 平板风格显示

原文地址:https://www.cnblogs.com/liangstudyhome/p/3672199.html