Android Shape控件美化實現(xiàn)代碼
如果你對Android系統(tǒng)自帶的UI控件感覺不夠滿意,可以嘗試下自定義控件,我們就以Button為例,很早以前Android123就寫到過Android Button按鈕控件美化方法里面提到了xml的selector構(gòu)造。當(dāng)然除了使用drawable這樣的圖片外今天Android開發(fā)網(wǎng)談下自定義圖形shape的方法,對于Button控件Android上支持以下幾種屬性shape、gradient、stroke、corners等。
我們就以目前系統(tǒng)的Button的selector為例說下:
<shape> <gradient android:startColor="#ff8c00" android:endColor="#FFFFFF" android:angle="270" /> <stroke android:width="2dp" android:color="#dcdcdc" /> <corners android:radius="2dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape>
對于上面,這條shape的定義,分別為漸變,在gradient中startColor屬性為開始的顏色,endColor為漸變結(jié)束的顏色,下面的angle是角度。接下來是stroke可以理解為邊緣,corners為拐角這里radius屬性為半徑,最后是相對位置屬性padding。
對于一個Button完整的定義可以為
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" > <shape> <gradient android:startColor="#ff8c00" android:endColor="#FFFFFF" android:angle="270" /> <stroke android:width="2dp" android:color="#dcdcdc" /> <corners android:radius="2dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape> </item> <item android:state_focused="true" > <shape> <gradient android:startColor="#ffc2b7" android:endColor="#ffc2b7" android:angle="270" /> <stroke android:width="2dp" android:color="#dcdcdc" /> <corners android:radius="2dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape> </item> <item> <shape> <gradient android:startColor="#ff9d77" android:endColor="#ff9d77" android:angle="270" /> <stroke android:width="2dp" android:color="#fad3cf" /> <corners android:radius="2dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape> </item> </selector>
注意Android123提示大家,以上幾個item的區(qū)別主要是體現(xiàn)在state_pressed按下或state_focused獲得焦點時,當(dāng)當(dāng)來判斷顯示什么類型,而沒有state_xxx屬性的item可以看作是常規(guī)狀態(tài)下。
以上就是對Android 控件美化Shape 資料的整理,有需要的同學(xué)參考下。
相關(guān)文章
詳細(xì)分析android的MessageQueue.IdleHandler
這篇文章主要介紹了android的MessageQueue.IdleHandler用法,很有參考價值,歡迎大家在下方留言區(qū)討論。2017-11-11android的RecyclerView實現(xiàn)拖拽排序和側(cè)滑刪除示例
在平時開發(fā)應(yīng)用的時候,經(jīng)常會遇到列表排序、滑動刪除的需求。這篇文章主要介紹了android的RecyclerView實現(xiàn)拖拽排序和側(cè)滑刪除示例,有興趣的可以了解一下。2017-02-02從零開始學(xué)android實現(xiàn)計算器功能示例分享(計算器源碼)
這篇文章主要介紹了android實現(xiàn)的計算器功能示例,可以加減乘除;可以倒退,可以清空文本,大家參考使用吧2014-02-02詳解基于Android App 安全登錄認(rèn)證解決方案
這篇文章主要介紹了基于Android App 安全登錄認(rèn)證解決方案,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09Android handle-message的發(fā)送與處理案例詳解
這篇文章主要介紹了Android handle-message的發(fā)送與處理案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08Android開發(fā)之多媒體文件獲取工具類實例【音頻,視頻,圖片等】
這篇文章主要介紹了Android開發(fā)之多媒體文件獲取工具類,結(jié)合實例形式分析了Android獲取音頻,視頻及圖片等多媒體資源的相關(guān)操作技巧,需要的朋友可以參考下2017-10-10Android資源文件與層次式導(dǎo)航超詳細(xì)講解
這篇文章主要介紹了Android資源文件與層次式導(dǎo)航,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-12-12Android拼圖游戲 玩轉(zhuǎn)從基礎(chǔ)到應(yīng)用手勢變化
這篇文章主要介紹了Android拼圖游戲的實現(xiàn)方法,教大家玩轉(zhuǎn)從基礎(chǔ)到應(yīng)用手勢變化,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10Android實現(xiàn)夜間模式切換功能實現(xiàn)代碼
現(xiàn)在很多App都有夜間模式,特別是閱讀類的App,夜間模式現(xiàn)在已經(jīng)是閱讀類App的標(biāo)配,本篇文章主要介紹了Android實現(xiàn)夜間模式功能實現(xiàn)代碼,有興趣的可以了解一下。2017-03-03