android 開發(fā)教程之日歷項(xiàng)目實(shí)踐(三)
二、創(chuàng)建樣式
日歷顯示的表格線,使用 Cell 填充圖形的邊框來實(shí)現(xiàn),為了統(tǒng)一,我們先定義邊框線的顏色及線條精細(xì)。
另外還要定義一系統(tǒng)填充樣式等。
創(chuàng)建 color:
color_calendar_border 表格線
color_calendar_title_gregorian 標(biāo)題欄日期年月文字的顏色color_calendar_title_lunar 標(biāo)題欄農(nóng)歷color_calendar_title_startcolor_calendar_title_endcolor_calendar_title_addition 標(biāo)題欄 節(jié)日,節(jié)氣color_calendar_weekindex 年單位周序號(hào)color_calendar_weekindex_backgroundcolor_calendar_weekend 周末color_calendar_weekend_backgroundcolor_calendar_header 表頭color_calendar_header_backgroundcolor_calendar_outrange 非本月日期color_calendar_outrange_backgroundcolor_calendar_normal_gregorian 公歷日期color_calendar_normal_lunar 農(nóng)歷日期color_calendar_normal_backgroundcolor_calendar_today_gregorian 今天公歷日期color_calendar_today_lunar 今天農(nóng)歷日期color_calendar_today_backgroundcolor_calendar_solarterm 節(jié)氣color_calendar_festival 節(jié)日color_calendar_pressed 點(diǎn)擊單元格填充背景
color_calendar_focused 焦點(diǎn)單元格填充背景
點(diǎn)擊 下圖 菜單 Search 下面的圖標(biāo)(New Android XML File)
選擇 Resource Type -> Values,輸入文件名 -> colors,選擇 Root Element -> resources,點(diǎn)擊 Finish。
定義 color_calendar_border
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="color_calendar_border">#3fff</color>
<color name="color_calendar_title_gregorian">#cfff</color>
<color name="color_calendar_title_lunar">#cfff</color>
<color name="color_calendar_title_start">#c000</color>
<color name="color_calendar_title_end">#6000</color>
<color name="color_calendar_title_addition">#f63</color>
<color name="color_calendar_weekindex">#3fff</color>
<color name="color_calendar_weekindex_background">#369f</color>
<color name="color_calendar_weekend">#9fff</color>
<color name="color_calendar_weekend_background">#3f99</color>
<color name="color_calendar_header">#9fff</color>
<color name="color_calendar_header_background">#6000</color>
<color name="color_calendar_outrange">#3fff</color>
<color name="color_calendar_outrange_background">#3fff</color>
<color name="color_calendar_normal_gregorian">#cfff</color>
<color name="color_calendar_normal_lunar">#9fff</color>
<color name="color_calendar_normal_background">#0000</color>
<color name="color_calendar_today_gregorian">#cfff</color>
<color name="color_calendar_today_lunar">#9fff</color>
<color name="color_calendar_today_background">#06c</color>
<color name="color_calendar_solarterm">#c0c3</color>
<color name="color_calendar_festival">#cf90</color>
<color name="color_calendar_pressed">#306c</color>
<color name="color_calendar_focused">#606c</color>
</resources>
Color 的值由四部分組成:透明度,Red, Green, Blue,每部分可以用一位或兩位十六進(jìn)制數(shù)字表示,透明度可以省略。
如:
ffff 或 ffffffff 表示不透明白色,前面的透明度可以省略:fff 或 ffffff
7f00 表示半透明的紅色
更多請(qǐng)查看:http://developer.android.com/guide/topics/resources/more-resources.html#Color
將顏色定義統(tǒng)一放在一個(gè)文件中,是出于兩點(diǎn)考慮,一是多處用到同一種顏色定義,這樣一處修改,相應(yīng)的位置都會(huì)跟著變,另外則是為了修改方便,無須到處去找某一個(gè)文件。上面的 color_calendar_border 被表格的各種狀態(tài)填充圖形用到,而像 color_calendar_weelndex_background 只有一處用到,如果不想統(tǒng)一管理,也可以不在這里定義,在定義 shape 時(shí),直接使用固定值。
創(chuàng)建 dimen:
點(diǎn)擊 下圖 菜單 Search 下面的圖標(biāo)(New Android XML File)

選擇 Resource Type -> Values,輸入文件名 -> dimens,選擇 Root Element -> resources,點(diǎn)擊 Finish。
完成的 xml 文件內(nèi)容:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="dimen_calendar_border">1dp</dimen>
</resources>
尺寸的單位主要有六種:dp, sp, pt, px, mm, in,更多介紹請(qǐng)參照:http://developer.android.com/guide/topics/resources/more-resources.html#Dimension
創(chuàng)建 Color State List
在我們的日歷中,單元格有三種狀態(tài),分別是無焦點(diǎn),按下,有焦點(diǎn),為了在不同的狀態(tài)下顯示不同的顏色,可以定義 Color State List。
關(guān)于 Color State List,更多請(qǐng)參照:http://developer.android.com/guide/topics/resources/color-list-resource.html。
Color State List 列表:
colorlist_calendar_normalcolorlist_calendar_outrange
colorlist_calendar_weekend
colorlist_calendar_today
點(diǎn)擊 下圖 菜單 Search 下面的圖標(biāo)(New Android XML File)
選擇 Resource Type -> Drawable,輸入文件名 -> colorlist_calendar_outrange,選擇 Root Element -> selector,點(diǎn)擊 Finish。
完成的 xml 文件:<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@color/color_calendar_pressed"/>
<item android:state_focused="true" android:color="@color/color_calendar_focused"/>
<item android:color="@color/color_calendar_outrange_background"/>
</selector>
其它也同樣創(chuàng)建。
創(chuàng)建的 drawable:
shape_calendar_titlebar.xml 主畫面標(biāo)題欄填充背景shape_calendar_header.xml 表頭填充背景shape_calendar_cell_weekindex.xml 年為單元的周序號(hào)單元格填充背景shape_calendar_cell_weekend.xml 周末日期單元格填充背景shape_calendar_cell_normal.xml 當(dāng)月普通日期單元格填充背景shape_calendar_cell_outrange.xml 非當(dāng)前月日期單元格填充背景shape_calendar_cell_today.xml 今天單元格填充背景
點(diǎn)擊 下圖 菜單 Search 下面的圖標(biāo)(New Android XML File)
選擇 Resource Type -> Drawable,輸入文件名 -> shpae_calendar_titlebar,選擇 Root Element -> shape,點(diǎn)擊 Finish。
輸入 shape 定義:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="3dp" />
<gradient android:angle="90"
android:startColor="@color/color_calendar_title_start"
android:endColor="@color/color_calendar_title_end"
/>
</shape>
這段定義代碼會(huì)幫我們生成一個(gè)圓角矩形,填充顏色是上下漸變的。
radius = 圓角大小
angle = 漸變填充方向(45的位數(shù),0-360,90 表示從上往下漸變填充)
startColor, endColor = 填充的起始與終止顏色定義
其它的也按此一一創(chuàng)建,但表格的填充矩形,不要圓角,刪除 radius 或設(shè)為 0
如:shape_calendar_cell_outrange.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/colorlist_calendar_outrange" />
<stroke android:width="@dimen/dimen_calendar_border"
android:color="@color/color_calendar_border" />
</shape>
solid = 填充色,這里用前面定義的 color state list,來實(shí)現(xiàn)不同狀態(tài)下,填充不同顏色。
stroke = 矩形邊框,width = 邊框線粗細(xì), color = 邊框線顏色
創(chuàng)建 style
打開 res/styles.xml,添加樣式定義。由于樣式與畫面設(shè)計(jì)相關(guān),在我們?cè)O(shè)計(jì)界面時(shí),還要相應(yīng)調(diào)整,所以在使用時(shí),一一添加。這里給出一個(gè) sample:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<style name="style_calendar_title">
<item name="android:background">@drawable/shape_calendar_titlebar</item>
</style>
<style name="style_calendar_title_gregorian">
<item name="android:textSize">36sp</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">@color/color_calendar_title_gregorian</item>
<item name="android:layout_marginLeft">25dp</item>
</style>
... ...
</resources>
- Android 一個(gè)日歷控件的實(shí)現(xiàn)代碼
- Android開發(fā)之日歷CalendarView用法示例
- Android實(shí)現(xiàn)自定義日歷
- android 開發(fā)教程之日歷項(xiàng)目實(shí)踐(一)
- Android自定義控件實(shí)現(xiàn)可多選課程日歷CalendarView
- android 開發(fā)教程之日歷項(xiàng)目實(shí)踐(二)
- Android可簽到日歷控件的實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)日歷控件示例代碼
- Android 仿日歷翻頁(yè)、仿htc時(shí)鐘翻頁(yè)、數(shù)字翻頁(yè)切換效果
- Android?Studio簡(jiǎn)單實(shí)現(xiàn)自定義日歷
相關(guān)文章
Android App中使用ListFragment的實(shí)例教程
這篇文章主要介紹了Android App中使用ListFragment的實(shí)例教程,ListFragment的內(nèi)容是以列表(list)的形式顯示的Fragment,需要的朋友可以參考下2016-05-05Android 中Fragment與Activity通訊的詳解
這篇文章主要介紹了Android 中Fragment與Activity通訊的詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握如何通信的,需要的朋友可以參考下2017-10-10Android控件之EditView常用屬性及應(yīng)用方法
本篇文章介紹了,Android控件之EditView常用屬性及應(yīng)用方法。需要的朋友參考下2013-04-04Android中使用Theme來解決啟動(dòng)app時(shí)出現(xiàn)的空白屏問題
相信大多數(shù)人一開始都會(huì)對(duì)啟動(dòng)app的時(shí)候出現(xiàn)先白瓶或者黑屏然后才進(jìn)入第一個(gè)界面,例如:SplashActivity。那這是什么原因造成的呢?下面小編給大家介紹下2016-12-12Android開發(fā)之完成登陸界面的數(shù)據(jù)保存回顯操作實(shí)例
這篇文章主要介紹了Android開發(fā)之完成登陸界面的數(shù)據(jù)保存回顯操作實(shí)現(xiàn)方法,結(jié)合完整實(shí)例形式較為詳細(xì)的分析了Android針對(duì)登錄數(shù)據(jù)的保存及回顯操作技巧,需要的朋友可以參考下2015-12-12Android基礎(chǔ)之獲取LinearLayout的寬高
LinearLayout是線性布局控件,它包含的子控件將以橫向或豎向的方式排列,按照相對(duì)位置來排列所有的widgets或者其他的containers,超過邊界時(shí),某些控件將缺失或消失。有的時(shí)候,我們需要想獲取LinearLayout寬高,下面通過這篇文章來跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。2016-11-11Android Flutter自適應(yīng)瀑布流案例詳解
這篇文章主要介紹了Android Flutter自適應(yīng)瀑布流案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09Android入門之實(shí)現(xiàn)自定義可復(fù)用的BaseAdapter
這篇文章主要為大家詳細(xì)介紹了Android如何構(gòu)建一個(gè)可復(fù)用的自定義BaseAdapter,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Android有一定的幫助,需要的可以參考一下2022-11-11Android 圖片添加水印的實(shí)現(xiàn)方法
這篇文章主要介紹了Android 圖片添加水印的實(shí)現(xiàn)方法的相關(guān)資料,添加水印的原理就是在畫布Canvas上繪制圖形、圖片、文字等等, 得到你想要的效果圖片,需要的朋友可以參考下2017-07-07