关于Android的style和theme

style是什么?

A style is a collection of properties that specify the look and format for a View or window. A style can specify properties such as height, padding, font color, font size, background color, and much more. A style is defined in an XML resource that is separate from the XML that specifies the layout.
来自developer.android

翻译一下
style是指定视图或窗口的外观和格式属性的集合。样式可以指定诸如高度,填充,字体颜色,字体大小,背景颜色,以及更多的特性。style是在从指定布局XML单独的XML资源定义。

举个栗子

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CodeFont" parent="@android:style/TextAppearance.Medium">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">monospace</item>
</style>
</resources>
<TextView
style="@style/CodeFont"
android:text="@string/hello" />

style作用?

  • 设计与内容分开
  • 可继承
  • 便于统一风格

style如何书写?

↑↑↑看上面的栗子↑↑↑

关于继承系统的书写

<style name="CodeFont" parent="@android:style/TextAppearance.Medium">
...
</style>

关于继承自定义的书写

<style name="CodeFont1" parent="CodeFont">
...
</style>

关于继承系统theme的书写

<style name="LightThemeSelector" parent="android:Theme.Holo.Light">
...
</style>
<style name="LightThemeSelector" parent="@android:style/Theme.Holo.Light">
...
</style>

关于继承自定义theme的书写

<style name="LightThemeSelector" parent="@style/Theme.AppCompat">
...
</style>

style与theme的区别?

  • Theme是针对窗体级别的,改变窗体样式;
  • Style是针对窗体元素级别的,改变指定控件或者Layout的样式。

系统自带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” : 用系统桌面为应用程序背景,无标题栏,全屏
android:theme=”Theme.Translucent : 透明背景
android:theme=”Theme.Translucent.NoTitleBar” : 透明背景并无标题
android:theme=”Theme.Translucent.NoTitleBar.Fullscreen” : 透明背景并无标题,全屏
android:theme=”Theme.Panel “: 面板风格显示
android:theme=”Theme.Light.Panel” : 平板风格显示

theme的一些属性

名称作用
android:windowIsTranslucent设置透明属性(防止启动时候的闪屏)
android:windowBackground设置背景图片
android:windowAnimationStyleActivity进入退出动画
android:windowNoTitle不显示标题栏
android:textColor默认字体颜色
android:windowFullscreen是否全屏
android:windowIsFloating是否浮现在activity之上
android:backgroundDimEnabled背景是否模糊显示
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Style 主要用于定义 Android 应用程序的外观和样式。它是一种基于 XML 的风格定义方式,可以用来定义应用程序中的布局、颜色、字体、尺寸等。Android Style 可以应用于整个应用程序、单个 Activity 或者某个视图。 Android Style 的定义通常存储在 res/values/styles.xml 文件中。在该文件中,可以使用 `<style>` 标签来定义样式,使用 `<item>` 标签来定义样式的具体属性。 以下是一个示例的 Android Style 定义: ```xml <!-- 定义一个基础样式 --> <style name="AppTheme" parent="Theme.AppCompat.Light"> <!-- 定义颜色 --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <!-- 定义字体 --> <item name="android:typeface">monospace</item> <!-- 定义尺寸 --> <item name="android:textSize">16sp</item> </style> <!-- 定义一个特定页面的样式 --> <style name="MainActivityStyle" parent="AppTheme"> <!-- 添加特定属性 --> <item name="android:background">@drawable/background_main</item> </style> ``` 在布局文件或者代码中,可以通过使用 `android:theme` 属性来应用样式。例如: ```xml <!-- 在布局文件中应用样式 --> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click Me" android:theme="@style/MainActivityStyle" /> ``` 通过使用 Android Style,可以统一定义应用程序的外观,提高开发效率,并且方便后续的修改和维护。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值