Android 中的 theme 和 style(1)

从技术层面上 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 视图。

猜你喜欢

转载自blog.csdn.net/weixin_34376986/article/details/88178383