Android主题theme和样式style总结
Android中的theme和style对页面的效果影响还是很大的,有的程序启动后页面的背景颜色为白色,字体为黑色,但是有的程序又是相反的,这是因为Android默认的主题显示不一样,各个编辑工具对主题和样式的默认设置有时候是不一样的,所以导致了刚开始的效果不一样。
主题theme和样式style既可以设置默认窗体的样式、字体的大小和颜色,还可以设置是否显示标题栏或设置标题栏的文字大小、颜色和背景,甚至隐藏标题栏或状态栏。
注意标题栏和状态栏是不一样的,标题显示的是App的名称和图标,状态栏显示的是手机的电量、信号等数据,两者都是可以隐藏的。
设置主题和样式,既可以对整个App设置也可以对单个页面(Activity)设置。如果有相同的属性,Activity中的属性会覆盖掉Application中的。
示例:对整个App设置
<application android:label="@string/app_name" android:icon="@drawable/heart"
android:name=".config.MyApplication"
android:theme="@style/BaseStyle"
>
对单个页面设置:
<activity android:name=".activity.MyActivity"
android:label="@string/app_name"
android:theme="@style/MyStyle"
>
当然style属性的设置要在res中的values文件夹中的styles文件(没有就要自己创建)
比如:
<style name="BaseStyle" parent="android:Theme.Light">
<item name="android:windowTitleSize">30sp</item>
<item name="android:windowTitleBackgroundStyle">@style/BaseStyleBackground</item>
<item name="android:windowTitleStyle">@style/BaseStyleText</item>
</style>
<style name="BaseStyleBackground">
<item name="android:background">#000</item>
</style>
<style name="BaseStyleText">
<item name="android:textColor">#fff</item>
</style>
<style name="MyStyle">
<item name="android:textColor">#000</item>
<item name="android:gravity">center</item>
<item name="android:background">#fff</item>
</style>
Style中不仅可以设置很多属性还可以设置对齐方式!有些属性是设置具体值,有些属性是设置true或false的值。
下面是一些属性的列举供大家参考:
下面是一些android自带的属性(可以直接在Application或Activity中设置):
android:theme="@android:style/Theme.Dialog" : Activity显示为对话框模式
android:theme="@android:style/Theme.NoTitleBar" : 不显示应用程序标题栏
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" : 不显示应用程序标题栏,并全屏
android:theme="@android:style/Theme.Light ": 背景为白色
android:theme="@android:style/Theme.Light.NoTitleBar" : 白色背景并无标题栏
android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen" : 白色背景,无标题栏,全屏
android:theme="@android:style/Theme.Black" : 背景黑色
android:theme="@android:style/Theme.Black.NoTitleBar" : 黑色背景并无标题栏
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" : 黑色背景,无标题栏,全屏
android:theme="@android:style/Theme.Wallpaper" : 用系统桌面为应用程序背景
android:theme="@android:style/Theme.Wallpaper.NoTitleBar" : 用系统桌面为应用程序背景,且无标题栏
android:theme="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen" : 用系统桌面为应用程序背景,无标题栏,全屏
android:theme="@android:style/Theme.Translucent : 透明背景
android:theme="@android:style/Theme.Translucent.NoTitleBar" : 透明背景并无标题
android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" : 透明背景并无标题,全屏
android:theme="@android:style/Theme.Panel ": 面板风格显示
android:theme="@android:style/Theme.Light.Panel" : 平板风格显示
使用:
<application android:label="@string/app_name" android:icon="@drawable/heart"
android:name=".config.MyApplication"
android:theme="@android:style/Theme.NoTitleBar"
>
这时默认所有的页面都是没有标题栏的。不要忘记前面的android:,因为这个是android系统提供的。
但是要注意一个Application或Activity只能设置一个theme,如果要设置多个样式的值还是要设置style!
下面是style的设置,其中style的name是可以自己定义的,需要拿来调用,而item的name是固定的,是android中已经定义好的。你会发现所有item的name都是android:下的!
一个sytle中 可以有一个或多个item的属性的设置。
Style样式XML文件(存放在res/values/):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomText" parent="@style/Text">
<item name="android:textSize">20sp</item>
<item name="android:textColor">#008</item>
</style>
</resources>
同样可以应用以上样式到TextView的XML文件(存放在res/layout/):
<?xml version="1.0" encoding="utf-8"?>
<EditText
style="@style/CustomText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello, World!" />
Style还可以对布局或控件(LinearLayout、TextView、ImageView等等)进行设置,比如多个TextView有相同的属性就可以抽成一个style,对这多个TextView设置style,就相当于设置了多个属性!
Style中item属性详解:
对Window的属性:
android:windowNoTitle 设置有没有标题栏true|false
android:windowFullScreen 设置全屏true|false
droid:windowIsFloating 设置是否浮现在activity之上true|false
android:windowIsTranslucent 设置window是否为透明
android:windowBackground 设置window的背景颜色
android:windowContentOverlay这个是定义contentoverlay的背景的
常见的:TextView的Style
android:textSize 设置字体大小
android:textColor 设置字体颜色
android:textColorHighlight 被选中文字的底色,默认为蓝色
android:textColorHint 设置提示信息文字的颜色,默认为灰色。与hint一起使用。
android:textColorLink 文字链接的颜色
android:textFilterEnabled 设为真时,列表会过滤根据用户的要求,过滤结果集。列表的适配器必须实现了 Filterable接口,才能使其可用
android:background 设置背景颜色(值可以是图片,也可以是十六进制颜色值)
android:textAppearance 设置文字的外观,如“android:textAppearance=“?android:attr/textAppearanceLargeInverse”这里引用的是系统自带的一个外观
android:textOff 未选中时按钮的文本
android:textOn 选中时按钮的文本
android:textStyle 字体,bold, italic, bolditalic
android:textScaleX控制字与字之间的间距
上面只是一些知识的收集罗列,有很多我也是没有用过的,遇到不懂的可以一起讨论。
本文主要是相对theme的介绍!
去掉所有Activity界面的标题栏
修改AndroidManifest.xml
在application 标签中添加android:theme=”@android:style/Theme.NoTitleBar”
补充一下;上面使用亮色背景,有时候弹出的对话框背景还是黑的,需要设置的属性是:
android:theme=”@android:style/Theme.Holo.Light”
Holo表示整天的意思。还有其他和Holo相关的属性就不一一罗列了。