官术网_书友最值得收藏!

Customizing the material theme

One of the most useful features of the material theme is the way it can take a small palette made of only a handful of colors and incorporate these colors into every aspect of a UI. Text and cursor colors, the way things are highlighted, and even system features such as the status and navigation bars can be customized to give our apps brand colors and an easily recognizable look.

The use of color in Material Design is a topic in itself, and there are strict guidelines regarding color, shade, and text, and these will be covered in detail later in the book. For now, we will just look at how we can use a style to apply our own colors to a material theme.

So as to keep our resources separate, and therefore easier to manage, we will define our palette in its own XML file. As we did earlier with the my_style.xml file, create a new values resource file in the values directory and call it colors. Complete the code as shown next:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="primary">#FFC107</color>
    <color name="primary_dark">#FFA000</color>
    <color name="primary_light">#FFECB3</color>
    <color name="accent">#03A9F4</color>
    <color name="text_primary">#212121</color>
    <color name="text_secondary">#727272</color>
    <color name="icons">#212121</color>
    <color name="divider">#B6B6B6</color>
</resources>

Tip

In the gutter to the left of the code, you will see small, colored squares. Clicking on these will take you to a dialog with a color wheel and other color selection tools for quick color editing.

We are going to apply our style to the entire app, so rather than creating a separate file, we will include our style in the theme that was set up by the project template wizard when we started the project. This theme is called AppTheme, as can be seen by opening the res/values/styles/styles.xml (v21) file. Edit the code in this file so that it looks like the following:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="android:Theme.Material.Light">
        <item name="android:colorPrimary">@color/primary</item>
        <item name="android:colorPrimaryDark">@color/primary_dark</item>
        <item name="android:colorAccent">@color/accent</item>
        <item name="android:textColorPrimary">@color/text_primary</item>
        <item name="android:textColor">@color/text_secondary</item>
    </style>
</resources>

Being able to set key colors, such as colorPrimary and colorAccent, allows us to incorporate our brand colors throughout the app, although the project template only shows us how we have changed the color of the status bar and app bar. Try adding radio buttons or text edit boxes to see how the accent color is applied. In the following figure, a timepicker replaces the original text view:

The XML for this looks like the following lines:

<TimePicker
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

For now, it is not necessary to know all the color guidelines. Until we get to them, there is an online material color palette generator at http://www.materialpalette.com/ that lets you try out different palette combinations and download color XML files that can just be cut and pasted into the editor.

With a complete and up-to-date development environment constructed, and a way to customize and adapt the material theme, we are now ready to look into how material specific widgets, such as card views, are implemented.

主站蜘蛛池模板: 西丰县| 海原县| 元朗区| 于都县| 石棉县| 宕昌县| 大埔区| 巨鹿县| 汾西县| 黄浦区| 兰西县| 济宁市| 阳信县| 增城市| 公安县| 青州市| 安泽县| 石嘴山市| 白玉县| 开平市| 安西县| 云梦县| 盘山县| 杂多县| 临沭县| 札达县| 芜湖市| 甘孜| 万年县| 扎囊县| 永吉县| 荔浦县| 通化市| 华蓥市| 呼图壁县| 东乌| 白玉县| 师宗县| 台南市| 石楼县| 固镇县|