Android 中的 theme 和 style(1)

  • 时间:2019-02-24 20:43 作者:zidea 来源:zidea 阅读:542
  • 扫一扫,手机访问
摘要:从技术层面上 theme 和 style 没有什么区别要说到两者的不同就是我们在 Android 应用使用 Theme 和 Style 的方法是不同的。 style name="myStyle" item name="android:background"

从技术层面上 themestyle 没有什么区别

要说到两者的不同就是我们在 Android 应用使用 ThemeStyle 的方法是不同的。

 <style name="myStyle">        <item name="android:background">            @color/colorAccent        </item> </style>

属性(Attribute)

属性就是键值对的键,通常也会在其中公告值的格式和类型。定义了值的类型可以便于保存值,同时也便于根据值的类型读取值。

属性值的类型

  • 资源类型
    1 fraction
    属性定义
<attr name = "pivotX" format = "fraction" />

属性使用

<rotate android:pivotX = "200%"/>

2 float
属性定义

<attr name = "fromAlpha" format = "float" />

属性使用

<alpha android:fromAlpha = "1.0"/>

3 boolean
属性定义

<attr name = "focusable" format = "boolean" />

属性使用

<Button android:focusable = "true"/>

4 color
属性定义

<attr name = "textColor" format = "color" />

属性使用

<TextView android:textColor = "#00FF00" />

5 string
属性定义

<attr name = "text" format = "string" />

属性使用

<TextView android:text = "我是文本"/>

6 dimension
属性定义

<attr name = "layout_width" format = "dimension" />

属性使用

<Button android:layout_width = "42dip"/>

7 integer
属性定义

<attr name = "framesCount" format="integer" />

属性使用

<animated-rotate android:framesCount = "12"/>

  • 特殊类型
    这里之所以叫特殊类型是由于他们只会被在定义属性时使用。
    8 flag
    属性定义
<declare-styleable name="名称">    <attr name="gravity">            <flag name="top" value="0x30" />            <flag name="bottom" value="0x50" />            <flag name="left" value="0x03" />            <flag name="right" value="0x05" />            <flag name="center_vertical" value="0x10" />            ...    </attr></declare-styleable>

属性使用

<TextView android:gravity="bottom|left"/>

9 enum
属性定义

<declare-styleable name="名称">    <attr name="orientation">        <enum name="horizontal" value="0" />        <enum name="vertical" value="1" />    </attr></declare-styleable>

属性使用

<LinearLayout      android:orientation = "vertical"></LinearLayout>

10 reference
属性定义

<declare-styleable name = "名称">     <attr name = "background" format = "reference" /></declare-styleable>

属性使用

<ImageView android:background = "@drawable/图片ID"/>

属性的命名空间

android 框架提供 android schema 来给出该 xml 解析的规范,当然也可以自己定义 schema 来解释 xml 中 tag 和属性以及值的意义。

<android.support.constraint.ConstraintLayout    xmlns:android="http://schemas.android.com/apk/res/android"

自己设置属性

当然也可以自己定义属性,其实现在使用 android 库就存在少量自己设置属性,值得注意的是我们自己定义的属性在应用级别是全局的,假如两个属性名称相同势必造成在构建(build)项目发生冲突错误。

  • 命名空间的是全局的
  • 所有的属性都遵守应用的 schema

theme 和 style 的区别

主题

void setTheme(int themeResId);Theme getTheme();

可以在 Activity 中调用 setTheme() 而后传入 theme 资源id,就会在默认 theme 上增加自己配置或者定义的 theme。同样可以通过 getTheme 获取当前的 theme。
其实主题自己本身并没有做什么,仅仅是用于保存一个配置。

应用默认组件样式

android:editTextStyle
例如我们在布局引入 editText 我们就会应用一个 editTextStyle 默认的组件样式,组件默认的样式就是在主题(theme)中定义的。每一个组件都自己的不同的默认样式名称。例如 editText 的默认样式是 editTextStyle, textview 的默认样式是 textviewStyle。

颜色值

android:textColorPrimary
我们还有少量颜色值,通常大多数颜色值都是用于定义文本颜色。例如 textColorPrimary、textColorSecondary 和 teriary 等等。

文本外观样式

android:textAppearanceSmall

窗口配置值

android:windowActionBar
当您的 activity 或者 dialog 创立时候, 在内部的已经 创立 window,根据配置能否有 actionBar,假如有 actionBar 会加载 actionBar 视图。

  • 全部评论(0)
最新发布的资讯信息
【系统环境|】2FA验证器 验证码如何登录(2024-04-01 20:18)
【系统环境|】怎么做才能建设好外贸网站?(2023-12-20 10:05)
【系统环境|数据库】 潮玩宇宙游戏道具收集方法(2023-12-12 16:13)
【系统环境|】遥遥领先!青否数字人直播系统5.0发布,支持真人接管实时驱动!(2023-10-12 17:31)
【系统环境|服务器应用】克隆自己的数字人形象需要几步?(2023-09-20 17:13)
【系统环境|】Tiktok登录教程(2023-02-13 14:17)
【系统环境|】ZORRO佐罗软件安装教程及一键新机使用方法详细简介(2023-02-10 21:56)
【系统环境|】阿里云 centos 云盘扩容命令(2023-01-10 16:35)
【系统环境|】补单系统搭建补单源码搭建(2022-05-18 11:35)
【系统环境|服务器应用】高端显卡再度登上热搜,竟然是因为“断崖式”的降价(2022-04-12 19:47)
手机二维码手机访问领取大礼包
返回顶部