Android?shape與selector標(biāo)簽使用詳解
Android中提供一種xml的方式,讓我們可以自由地定義背景,比較常用的就是shape
標(biāo)簽和selector
標(biāo)簽
shape
shape的翻譯為形狀的意思,一般用來(lái)定義背景的形狀,如長(zhǎng)方形,線條,圓形
- rectangle 矩形 默認(rèn)
- oval 橢圓
- line 線條
- ring 環(huán)形
簡(jiǎn)單使用:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:tint="@color/white" android:shape="rectangle"> </shape>
tint是用來(lái)設(shè)置背景顏色
上述代碼即為白色的矩形,效果如下圖:
一般我們將shape當(dāng)做根標(biāo)簽來(lái)使用
corners 圓角
corners標(biāo)簽,即為圓角的意思,可定義的屬性如下
屬性 | 說(shuō)明 |
---|---|
radius | 定義4個(gè)方向圓角寬度 |
topRightRadius | 右上角圓角寬度 |
bottomLeftRadius | 左下角圓角寬度 |
bottomRightRadius | 右下角圓角寬度 |
topLeftRadius | 左上角圓角寬度 |
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:tint="@color/white" android:shape="rectangle"> <corners android:radius="12dp"/> </shape>
上述代碼即為圓角矩形的效果:
stroke 邊框
屬性 | 說(shuō)明 |
---|---|
color | 邊框顏色 |
width | 邊框?qū)挾?/td> |
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="12dp"/> <stroke android:color="@color/read_dot_bg" android:width="1dp"/> </shape>
注意: 這里代碼中刪除了shape中的tint屬性,因?yàn)閠int屬性會(huì)優(yōu)先級(jí)較高,導(dǎo)致邊框無(wú)法顯示出來(lái)!
solid 填充背景色
color 背景顏色
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="12dp"/> <stroke android:color="@color/read_dot_bg" android:width="1dp"/> <solid android:color="@color/white"/> </shape>
上述代碼,將背景設(shè)置了白色,且邊框也能正常顯示
gradient 漸變
屬性 | 說(shuō)明 |
---|---|
startColor | 開始顏色 |
endColor | 結(jié)束顏色 |
angle | 角度 0 90 180 270 可以設(shè)置漸變的方向 |
type | 漸變類型,linear:線性 radial:輻射狀 sweep:掃射 |
angle屬性記憶的方法是:先記住默認(rèn)的方向,startColor到endColor,方向是從上到下,然后以逆時(shí)針為方向轉(zhuǎn)動(dòng),如果為0,則是逆時(shí)針轉(zhuǎn)動(dòng)90°,以此類推
測(cè)試的方向,如果是45°的倍數(shù),也是稍微有所區(qū)別
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="12dp"/> <stroke android:color="@color/read_dot_bg" android:width="1dp"/> <solid android:color="@color/white"/> <gradient android:gradientRadius="5dp" android:startColor="@color/white" android:endColor="@color/font_blue"/> </shape>
PS: 注意solid和gradient兩個(gè)標(biāo)簽的順序,兩者聯(lián)用,位于xml下面的會(huì)覆蓋上面的:
圓形背景
圓形背景,即設(shè)置了shape屬性為oval
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <size android:width="5dp" android:height="5dp" /> <solid android:color="#80011627" /> </shape>
同時(shí),加上了個(gè)size標(biāo)簽,用來(lái)定義寬高,這樣才會(huì)顯示出圓形,不然就是橢圓
這里size標(biāo)簽寬高似乎可以是任意值,因?yàn)槭鞘噶?,?yīng)用到View中會(huì)自動(dòng)伸縮
PS:同理,如果想要正方形,設(shè)置shape屬性了rectangle,同時(shí)加上size標(biāo)簽即可,如下圖
ripple 水波紋
水波紋,需要用ripple標(biāo)簽,不過(guò)只支持Android5.0以上的版本,寫法如下
<?xml version="1.0" encoding="utf-8"?> <ripple xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:color="@color/colorPrimary" tools:targetApi="lollipop"> <!--上面的是漣漪(水波紋)的顏色--> <!--下面的則是背景色--> <item> <shape android:shape="rectangle"> <solid android:color="@color/colorAccent" /> <corners android:radius="4dp" /> </shape> </item> </ripple>
使用的話,View中background屬性引用上面的xml文件即可
在自定義 <ripple/>
時(shí),我們一般把它放到 drawable-v21 文件夾下, 在drawable文件夾下放置兼容低版本的普通 Drawable 文件,如 <shape/>
或者 <selector/>
。
selector 標(biāo)簽
有時(shí)候需要自定義下按鈕的點(diǎn)擊變化背景等樣式,就可以用到此標(biāo)簽來(lái)定義相關(guān)的點(diǎn)擊變化效果
常用屬性如下表所示:
屬性 | 說(shuō)明 |
---|---|
state_pressed | 設(shè)置是否按壓狀態(tài),一般在true時(shí)設(shè)置該屬性,表示已按壓狀態(tài),默認(rèn)為false |
state_selected | 設(shè)置是否選中狀態(tài),true表示已選中,false表示未選中 |
state_checkable | 設(shè)置是否勾選狀態(tài),主要用于CheckBox和RadioButton,true表示已被勾選,false表示未被勾選 |
state_checked | 設(shè)置勾選是否可用狀態(tài),類似state_enabled,只是state_enabled會(huì)影響觸摸或點(diǎn)擊事件,state_checkable影響勾選事件 |
state_focused | 設(shè)置是否獲得焦點(diǎn)狀態(tài),true表示獲得焦點(diǎn),默認(rèn)為false,表示未獲得焦點(diǎn) |
state_enabled | 設(shè)置觸摸或點(diǎn)擊事件是否可用狀態(tài),一般只在false時(shí)設(shè)置該屬性,表示不可用狀態(tài) |
文本選中變色示例
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!--選中的顏色--> <item android:color="@color/read_dot_bg" android:state_checked="true" /> <!--未選中的顏色 --> <item android:color="@color/black" android:state_checked="false" /> <!--默認(rèn)的顏色--> <item android:color="@color/black" /> </selector>
checkbox選中效果變化示例
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!--選中的背景--> <item android:drawable="@drawable/radio_background_checked" android:state_checked="true" /> <!--未選中背景 --> <item android:drawable="@drawable/radio_background_uncheck" android:state_checked="false" /> <!--默認(rèn)的背景--> <item android:drawable="@drawable/radio_background_normal" /> </selector>
補(bǔ)充
關(guān)于透明色效果
透明色是定義在#
后面前面的兩個(gè)數(shù)值,是十六進(jìn)制
PS:注意,似乎也有規(guī)則是在后面加上兩位數(shù)值代表透明度
如:#011627
-> #80011627
透明色
其中80即為透明度的十六進(jìn)制,表示透明度為50%,可以參考下面透明度大全表格
<solid android:color="#4DFFFFF2"> </solid>
透明度大全
不透度對(duì)應(yīng)16進(jìn)制大全(0-100)不透明度16進(jìn)制
不透明度 | 16進(jìn)制 |
---|---|
0% | 00 |
1% | 03 |
2% | 05 |
3% | 08 |
4% | 0A |
5% | 0D |
6% | 0F |
7% | 12 |
8% | 14 |
9% | 17 |
10% | 1A |
11% | 1C |
12% | 1F |
13% | 21 |
14% | 24 |
15% | 26 |
16% | 29 |
17% | 2B |
18% | 2E |
19% | 30 |
20% | 33 |
21% | 36 |
22% | 38 |
23% | 3B |
24% | 3D |
25% | 40 |
26% | 42 |
27% | 45 |
28% | 47 |
29% | 4A |
30% | 4D |
31% | 4F |
32% | 52 |
33% | 54 |
34% | 57 |
35% | 59 |
36% | 5C |
37% | 5E |
38% | 61 |
39% | 63 |
40% | 66 |
41% | 69 |
42% | 6B |
43% | 6E |
44% | 70 |
45% | 73 |
46% | 75 |
47% | 78 |
48% | 7A |
49% | 7D |
50% | 80 |
51% | 82 |
52% | 85 |
53% | 87 |
54% | 8A |
55% | 8C |
56% | 8F |
57% | 91 |
58% | 94 |
59% | 96 |
60% | 99 |
61% | 9C |
62% | 9E |
63% | A1 |
64% | A3 |
65% | A6 |
66% | A8 |
67% | AB |
68% | AD |
69% | B0 |
70% | B3 |
71% | B5 |
72% | B8 |
73% | BA |
74% | BD |
75% | BF |
76% | C2 |
77% | C4 |
78% | C7 |
79% | C9 |
80% | CC |
81% | CF |
82% | D1 |
83% | D4 |
84% | D6 |
85% | D9 |
86% | DB |
87% | DE |
88% | E0 |
89% | E3 |
90% | E6 |
91% | E8 |
92% | EB |
93% | ED |
94% | F0 |
95% | F2 |
96% | F5 |
97% | F7 |
98% | FA |
99% | FC |
100% | FF |
到此這篇關(guān)于Android shape與selector標(biāo)簽使用的文章就介紹到這了,更多相關(guān)Android shape與selector標(biāo)簽使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android應(yīng)用的Material設(shè)計(jì)的布局兼容性的一些要點(diǎn)總結(jié)
這篇文章主要介紹了Android應(yīng)用的Material設(shè)計(jì)的布局兼容性的一些要點(diǎn)總結(jié),文中還給了一個(gè)RecyclerView布局管理的例子,需要的朋友可以參考下2016-04-04Android 單例模式的四種實(shí)現(xiàn)方式
單例模式作為設(shè)計(jì)模式之一,使用場(chǎng)景非常多。本文講述了Android實(shí)現(xiàn)單例模式的幾種方式2021-05-0521天學(xué)習(xí)android開發(fā)教程之SQLite分頁(yè)讀取
21天學(xué)習(xí)android開發(fā)教程之SQLite分頁(yè)讀取,Android包含了常用于嵌入式系統(tǒng)的SQLite,免去了開發(fā)者自己移植安裝的功夫,感興趣的朋友可以參考一下2016-02-02Android自定義DataTimePicker實(shí)例代碼(日期選擇器)
本篇文章主要介紹了Android自定義DataTimePicker實(shí)例代碼(日期選擇器),非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-01-01Android內(nèi)嵌Unity并實(shí)現(xiàn)互相跳轉(zhuǎn)的實(shí)例代碼
這篇文章主要介紹了Android內(nèi)嵌Unity并實(shí)現(xiàn)互相跳轉(zhuǎn)的實(shí)例代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11Android 中Fragment與Activity通訊的詳解
這篇文章主要介紹了Android 中Fragment與Activity通訊的詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家理解掌握如何通信的,需要的朋友可以參考下2017-10-10Android?Scroller實(shí)現(xiàn)彈性滑動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android?Scroller實(shí)現(xiàn)彈性滑動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Android APK文件在電腦(PC虛擬機(jī))上面運(yùn)行方法
APK是Android系統(tǒng)的發(fā)布的工程包,很多時(shí)候我們想在電腦上而非Android手機(jī)上面運(yùn)行它,需要的朋友可以了解下2012-12-12