Android樣式的開發 Style篇

2021-08-09 03:22:28 字數 4467 閱讀 1741

前面鋪墊了那麼多,終於要講到本系列的終篇,整合所有資源,定義成統一的樣式。

哪些該定義成統一的樣式呢?舉幾個例子吧:

每個頁面標題欄的標題基本會有一樣的字型大小、顏色、對齊方式、內間距、外間距等,這就可以定義成樣式;

很多按鈕也都使用一致的背景、內間距、文字顏色、文字大小、文字的對齊方式等,這也可以定義成樣式;

網路載入的進度條基本也都是一樣的,同樣可以定義成樣式;

android的樣式一般定義在res/values/styles.xml檔案中,其中有乙個根元素

,而具體的每種樣式定義則是通過下的子標籤

其中,statelistanimator指定狀態改變時的動畫,button_state_list_anim_material的**如下:

<?xml version="1.0" encoding="utf-8"?>

xmlns:android=

"">

android:state_pressed=

"true"

android:state_enabled=

"true"

>

android:propertyname=

"translationz"

android:duration=

"@integer/button_pressed_animation_duration"

android:valueto=

"@dimen/button_pressed_z_material"

android:valuetype=

"floattype"

/>

android:propertyname=

"elevation"

android:duration=

"0"android:valueto=

"@dimen/button_elevation_material"

android:valuetype=

"floattype"

/>

android:state_enabled=

"true"

>

android:propertyname=

"translationz"

android:duration=

"@integer/button_pressed_animation_duration"

android:valueto=

"0"android:startdelay=

"@integer/button_pressed_animation_delay"

android:valuetype=

"floattype"

/>

android:propertyname=

"elevation"

android:duration=

"0"android:valueto=

"@dimen/button_elevation_material"

android:valuetype=

"floattype"

/>

android:propertyname=

"translationz"

android:duration=

"0"android:valueto=

"0"android:valuetype=

"floattype"

/>

android:propertyname=

"elevation"

android:duration=

"0"android:valueto=

"0"android:valuetype=

"floattype"

/>

可以看到,每種狀態的動畫為屬性動畫集,屬性動畫的用法請參考property animation篇。

現在我想繼承widget.material.button樣式,改變背景和文字顏色,那麼,**如下:

其中,@drawable/bg_btn_selector和@color/text_btn_selector**的實現請參照selector篇。

有些按鈕,我只想改變文字顏色,但背景想讓它透明,這時就可以用點字首的方式繼承以上的樣式,**如下:

引用的時候只要在相應的button裡新增style就可以了,**如下:

android:layout_width=

"wrap_content"

android:layout_height=

"wrap_content"

android:onclick=

"onaction"

android:text=

"@string/btn_action"

style=

"@style/buttonnormal.transparent"

/>

有時候,定義的樣式太多,如果都放在styles.xml檔案裡,那這檔案也太臃腫了。因此,可以將樣式分類拆分成多個檔案。android系統本身也拆分為多個檔案存放的,如下列表全都是樣式檔案:

styles.xml

styles_device_defaults.xml

styles_holo.xml

styles_leanback.xml

styles_material.xml

styles_micro.xml

themes.xml

themes_device_defaults.xml

themes_holo.xml

themes_leanback.xml

themes_material.xml

themes_micro.xml

其中,主要分為兩大類,styles定義了簡單的樣式,而themes則定義了主題。

標籤或

android系統提供了多套主題,檢視android的frameworks/base/core/res/res/values目錄,就會看到有以下幾個檔案(目前為止):

自定義主題也很簡單,只要繼承某一父主題,然後在

標籤或主題的定義示例如下:

其中,windowanimation重新指定了activity的轉場動畫,以下為activity_close_exit的示例**:

<?xml version="1.0" encoding="utf-8"?>

xmlns:android=

""android:shareinterpolator=

"false"

android:zadjustment=

"top"

>

android:fromalpha=

"0.0"

android:toalpha=

"1.0"

android:interpolator=

"@interpolator/decelerate_quart"

android:fillenabled=

"true"

android:fillbefore=

"false"

android:fillafter=

"true"

android:duration=

"200"

/>

android:fromydelta=

"8%"

android:toydelta=

"0"android:fillenabled=

"true"

android:fillbefore=

"true"

android:fillafter=

"true"

android:interpolator=

"@interpolator/decelerate_quint"

android:duration=

"350"

/>

這是比較簡單的檢視動畫,檢視動畫具體用法可參考view animation篇。

"true"

android:icon=

"@mipmap/ic_launcher"

android:label=

android:theme=

>

Android 樣式開發

記得以前學習 qt widget 的時候用 qss 來實現介面的樣式,html 是通過 css 來實現介面的樣式,同理,現在在 android 裡面,它也有一套可以實現介面樣式開發的機制,它提供了 shapedrawable statelistdrawable layerdrawable clipd...

Android樣式的開發 layer list篇

寫於2015 09 09 android樣式的開發 shape篇 android樣式的開發 selector篇 android樣式的開發 layer list篇 android樣式的開發 drawable彙總篇 android樣式的開發 view animation篇 android樣式的開發 pr...

VUE中樣式的動態繫結 class 和 style

class繫結dom元素的整體樣式 style繫結dom元素中的部分樣式,例如背景顏色還是字型大小等 class的使用 1.現在這裡定義動態繫結樣式的名稱 2.編寫動態樣式的樣式 3.繫結動態的樣式 class oneclass,twoclass style的使用 1定義樣式名字及內容 2直接繫結對...