1.样式化常见组件

1.1 问题

你要让自己的应用程序在所有用户可能运行的Android版本上创建一致的外观和体验,同时减少维护这些自定义元素所需的代码量。

1.2 解决方案

(API Level1)
可以将定义应用程序外观常见属性抽象化到XML样式中。样式是视图自定义属性的集合,如文本大小或背景色,这些属性应该应用于应用程序内的多个视图。将这些属性抽象化到样式中,就可以在单个位置定义公共的元素,使得代码更易于更新和维护。
Android还支持将多个样式共同分组到称为“主题”的全局元素中。主题被应用于整个上下文(如Activity或应用程序),并且定义了应适用于该上下文中所有视图的样式。在应用程序中启动的每一个Activity都应用了一个主题,即使你没有定义任何主题。在此情况下,改为应用默认的系统主题。

1.3 实现机制

为研究样式的概念,接下来创建如图所示的Activity布局。
这里写图片描述
样式化的小部件

从图中可以看到,此视图中一些元素的外观需要定制,使其不同于通过所应用的默认系统主题样式化的常见外观。一种方法是直接在Activity布局中定义适用于全部视图的所有属性。如果这样做的话,则使用的代码如下所示:
res/layout/activity_styled.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:textStyle="bold"
        android:text="Select One"/>
    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:minHeight="@dimen/buttonHeight"
            android:button="@null"
            android:background="@drawable/background_radio"
            android:gravity="center"
            android:text="One"/>
        <RadioButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:minHeight="@dimen/buttonHeight"
            android:button="@null"
            android:background="@drawable/background_radio"
            android:gravity="center"
            android:text="Two"/>
        <RadioButton  
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:minHeight="@dimen/buttonHeight"
            android:button="@null"
            android:background="@drawable/background_radio"
            android:gravity="center"
            android:text="Three"/>
    </RadioGroup>

 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:textStyle="bold"
        android:text="Select All"/>
    <TableRow>
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minHeight="@dimen/buttonHeight"
            android:minWidth="@dimen/checkboxWidth"
            android:button="@null"
            android:gravity="center"
            android:textStyle="italic"
            android:textColor="@color/text_checkbox"
            android:text="One"/>
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minHeight="@dimen/buttonHeight"
            android:minWidth="@dimen/checkboxWidth"
            android:button="@null"
            android:gravity="center"
            android:textStyle="italic"
            android:textColor="@color/text_checkbox"
            android:text="Two"/>
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minHeight="@dimen/buttonHeight"
            android:minWidth="@dimen/checkboxWidth"
            android:button="@null"
            android:gravity="center"
            android:textStyle="italic"
            android:textColor="@color/text_checkbox"
            android:text="Three"/>
    </TableRow>
    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minWidth="@dimen/buttonWidth"
            android:background="@drawable/background_button"
            android:textColor="@color/accentPink"
            android:text="@android:string/ok"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minWidth="@dimen/buttonWidth"
            android:background="@drawable/background_button"
            android:textColor="@color/accentPink"
            android:text="@android:string/cancel"/>
    </TableRow>
</TableLayout>

在此代码中,我们突出强调了每个视图中与其他相同类型视图共用的属性。这些属性使按钮、文本标题和可选中的元素具有相同的外观。其中有很多重复出现的代码,我们可以通过样式进行简化。

首先,我们需要创建新的资源文件,并且使用style标记定义每个属性组。
以下代码显示了完整的抽象化代码:
res/values/styles.xml

<resources>
    <!-- 小部件的样式 -->
    <style name="LabelText"  parent="android:TextAppearance.Large">
        <item name="android:textStyle">bold</item>
    </style>

    <style name="FormButton"  parent="android:Widget.Button">
        <item name="android:minWidth">@dimen/buttonWidth</item>
        <item name="android:background">@drawable/background_button</item>
        <item name="android:textColor">@color/accentPink</item>
    </style>

    <style name="FormRadioButton" parent="android:Widget.CompoundButton.RadioButton">
        <item name="android:minHeight">@dimen/buttonHeight</item>
        <item name="android:button">@null</item>
        <item name="android:background">@drawable/background_radio</item>
        <item name="android:gravity">center</item>
    </style>

    <style name="FormCheckBox" parent="android:Widget.CompoundButton.CheckBox">
        <item name="android:minHeight">@dimen/buttonHeight</item>
        <item name="android:minWidth">@dimen/checkboxWidth</item>
        <item name="android:button">@null</item>
        <item name="android:gravity">center</item>
        <item name="android:textStyle">italic</item>
        <item name="android:textColor">@color/text_checkbox</item>
    </style>
</resources>

style组将需要应用于每个视图类型的公共属性分组在一起。视图仅可以接受单个样式定义,因此必须在一个组中聚集用于此视图的所有属性。然而,样式支持继承性,这就使我们可以级联每个样式的定义,之后再将它们应用于视图。
请注意每个样式如何声明父样式,父样式是我们应继承的基础框架样式。父样式不是必需的,但因为每个视图上存在的单一样式规则,使用自定义版本覆盖默认样式可替代主题的默认值。如果没有继承基础父样式,则必须定义视图需要的所有属性。通过框架的基础样式扩展小部件的样式,可确保我们只需要添加希望定制的、默认主题外观之外的属性。

显式或隐式的父样式声明:
样式继承采用两种形式之一。如前所示,样式可以显式声明其父样式:

  <style name="BaseStyle" />
  <style name="NewStyle"  parent="BaseStyle" />

NewStyle是BaseStyle的扩展,包括在父样式中定义的所有属性。样式还支持隐式父样式声明语法,如下所示:

  <style name="BaseStyle" />
  <style name="BaseStyle.Extended"/>

BaseStyle.Extended以相同的方式从Base Style继承其属性。此版本的功能与显式示例相同,只是更加简洁。两种方式不应混用,如果混用,就无法实现在单个样式中采用多个父样式。最终,人们始终优先选择显式父样式声明,而代码的可读性就会降低。

我们可以对原始布局文件应用新的样式,得到的简化版本如下所示:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="@style/LabelText"
        android:text="Select One"/>
    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton
            style="@style/FormRadioButton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="One"/>
        <RadioButton
            style="@style/FormRadioButton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Two"/>
        <RadioButton
            style="@style/FormRadioButton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Three"/>
    </RadioGroup>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="@style/LabelText"
        android:text="Select All"/>

    <TableRow>
        <CheckBox
            style="@style/FormCheckBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="One"/>
        <CheckBox
            style="@style/FormCheckBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Two"/>
        <CheckBox
            style="@style/FormCheckBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Three"/>
    </TableRow>
    <TableRow>
        <Button
            style="@style/FormButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@android:string/ok"/>
        <Button
            style="@style/FormButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@android:string/cancel"/>
    </TableRow>
</TableLayout>

通过对每个视图应用样式属性,就可以避免重复的显式属性引用,而改用对每个元素只引用一次。此行为的一个例外情况是TextView头部,它接受特殊的android:textAppearance属性。此属性获取一个样式引用,并且仅应用于文本格式化属性(大小、样式、颜色等)。使用TextView时,仍然可以同时应用单独的样式属性。这样,TextView实例在对单个视图使用多种样式的框架中就可以得到支持。

主题

在Android中,主题(Theme)就是一种应用到整个应用程序或某个Activity的外观风格。使用主题有两个选择,使用系统主题或者自定义主题。无论采用哪种方法,都要在AndroidManifest.xml文件中设置主题,代码如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    ……>
<!--通过application标签来设置全局主题-->
    <application android:theme="APPLICATION_THEME_NAME"
        ……>
<!--通过application标签来设置单个主题-->
        <activity android:name=".Activity"
            android:theme="ACTIVITY_THEME_NAME"
           ……>
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
                 ……
            </intent-filter>
        </activity>
    </application>
</manifest>

(1)系统主题
Android框架中打包的styles.xml和themes.xml文件中包含了一些主题选项,其中是一些有用的自定义属性。完整的清单可查阅SDK文档中的R.style,下面是几个常用实例:

  • Theme.Light:标准主题的变体,该主题的背景和用户元素使用相反的颜色主题。它是Android3.0以前版本的应用程序默认推荐使用的基础主题。
  • Theme.NoTitleBar.Fullscreen:移除标题栏和状态栏,全屏显示(去掉屏幕上所有的组件)。
  • Theme.Dialog:让Activity看起来像对话框的有用主题。
  • Theme.Holo.Light:(API Level11)使用逆配色方案的主题并默认拥有一个ActionBar。这是Android3.0上应用程序默认推荐的基础主题。
  • Theme.Holo.Light.DarkActionBar:(API Level14)使用逆配色方案的主题,但ActionBar是黑色实线的。这是Android4.0上应用程序默认推荐的基础主题。
  • Theme.Material.Light:(API Level21)通过小型的原色调色板控制的简化颜色方案主题,此主题还支持使用提供的原色对标准小部件着色。这是Android5.0上应用程序默认推荐的基础主题。

注意:
使用AppCompat库时,应改为使用这些主题的每个主题的其他版本(例如,Theme.AppCompat.Light.DarkActionBar)。

以下代码通过设置AndroidManifest.xml文件中的android:theme属性,将一个系统主题应用到整个应用程序中。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    ……>
<!--通过application标签来设置全局主题-->
    <application android:theme="Theme.NoTitleBar"
        ……>
              ……
    </application>
</manifest>

(2)自定义主题
有时候系统提供的主题还不能满足要求。毕竟,系统提供的主题并不能自定义窗口中的所有元素。定义自定义主题能方便地解决这个问题。
找到项目目录res/values下的styles.xml文件,如果没有就创建一个。记住,主题就是应用范围更广的风格样式,所以两者是在同一个地方定义的。与窗口自定义有关的主题元素可以在SDK的R.attr引用中找到,下面是常用的一些元素:

  • android:windowNotitle:控件是否要移除默认的标题栏;设为true以移除标题栏。
  • android:windowFullscreen:控件是否移除系统状态栏;设为true以移除状态栏并全屏显示。
  • android:windowBackground:将某个颜色或Drawable资源设为背景。
  • android:windowContentOverlay:窗口内容的前景之上放置的Drawable资源。默认情况下,就是状态栏下的阴影;可以用任何资源代替默认的状态栏,或者设为null(XML中为@null)以将其移除。

此外,Material主题接受一系列颜色属性,这些属性用于对应用程序界面小部件着色:

  • android:colorPrimary:用于对主要的界面元素着色,如ActionBar和滚动边界发光特效。同样也影响最近对标题栏颜色的操作。
  • android:colorPrimaryDark:对系统控件着色,如状态栏的背景。
  • android:colorAccent:应用于拥有焦点或已激活控件的默认颜色。
  • android:colorControlNormal:重写没有焦点或未激活控件的颜色。
  • android:colorControlActivated:重写拥有焦点或已激活控件的颜色。如果同时定义了强调色,则替换了该颜色。
  • android:colorControlHighlight:重写正在按下的控件的颜色。

以下代码就是一个styles.xml文件示例,其中创建了一个自定义主题,以便为应用程序界面提供品牌特有的颜色。
res/values/styles.xml

<resources>
    <style name="BaseAppTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <!-- Action bar 的背景色 -->
        <item name="colorPrimary">@color/primaryBlue</item>
        <!-- 状态栏的着色 -->
        <item name="colorPrimaryDark">@color/primaryDarkBlue</item>
        <!-- 应用于所有拥有焦点/已激活控件的默认颜色-->
        <item name="colorAccent">@color/accentPink</item>
        <!-- 未选择控件的颜色 -->
        <item name="colorControlNormal">@color/controlNormalGreen</item>
        <!-- 已激活控件的颜色,重写强调色 -->
        <item name="colorControlActivated">@color/controlActivatedGreen</item>
    </style>
</resources>

注意,主题也可以从父主题继承属性,所以不需要从头创建整个主题。在这个示例中,我们选择了继承Android默认的系统主题,只自定义我们要修改的属性。所有平台主题都定义在Android包的res/values/theme.xml文件中。关于样式和主题的更多细节可查阅SDK文档。
以下代码展示了如何在AndroidManifest.xml中对单个Activity示例应用这些主题:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    ……>
<!--通过application标签来设置全局主题-->
    <application 
        ……>
<!--通过activity标签来设置单独的主题-->
        <activity android:name=".ActivityOne"
                    android:theme = "@style/AppTheme"
          ……>
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值