生活随笔
收集整理的這篇文章主要介紹了
Android之shape属性详解
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
有時候 ,為了滿足一些需求,我們要用到 shape 去定義 一些背景,shape 的用法 跟圖片一樣 ,可以給View設(shè)置 Android:background=”@drawable/shape”, 定義的shape 文件,放在 res/shape 目錄下
通常我們可以用shape 做 button 的背景選擇器,也可以做切換tab 時,底部的下劃線。
先看我們用shape 都可以做什么
shape下面 一共有6個子節(jié)點, 常用的 就只有 四個,padding 和size 一般用不到。
- corners ———-圓角
- gradient ———-漸變
- padding ———-內(nèi)容離邊界距離
- size ————大小
- solid ———-填充顏色
- stroke ———-描邊
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" ><corners
android:radius="9dp"android:topLeftRadius="2dp"android:topRightRadius="2dp"android:bottomLeftRadius="2dp"android:bottomRightRadius="2dp"/><gradient
android:startColor="@android:color/white"android:centerColor="@android:color/black"android:endColor="@android:color/black"android:useLevel="true"android:angle="45"android:type="radial"android:centerX="0"android:centerY="0"android:gradientRadius="90"/><padding
android:left="2dp"android:top="2dp"android:right="2dp"android:bottom="2dp"/><size
android:width="50dp"android:height="50dp"/><solid
android:color="@android:color/white"/><stroke
android:width="2dp"android:color="@android:color/black"android:dashWidth="1dp"android:dashGap="2dp"/>
</shape>
shape 做虛線
拿shape 做虛線,shape 設(shè)置為line , stroke 是描邊屬性,
其中 dashGap dashWidth 兩個屬性彼此一起存在才生效。
dashGap :兩段之間的空隙寬度、
dashWidth :一段線的寬度
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="line" ><stroke
android:dashGap="3dp"android:dashWidth="8dp"android:width="1dp"android:color="#009999" />
</shape>
效果如下
shape做漸變實線
gradient 表示漸變
angle 漸變角度,45的倍數(shù)。
startColor endColor centerColor 起 止 中 的顏色
<shape xmlns:android="http://schemas.android.com/apk/res/android" ><gradient
android:type="linear"android:angle="0"android:endColor="#F028A2"android:startColor="#2A99F3" />
</shape>
效果如下
shape 做view背景選擇器
這里注意 ,item 的 state_pressed=true 是選擇狀態(tài),按下,另一個不設(shè)置 就是 正常狀態(tài)。
solid :是填充顏色
corners:設(shè)置 四個角的弧度
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_pressed="true" ><shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><solid android:color="#ffffff" /> <corners
android:topRightRadius="10dp" android:bottomLeftRadius="10dp" android:topLeftRadius="10dp" android:bottomRightRadius="10dp" /></shape></item><item ><shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><gradient android:startColor="#55B4FE" android:endColor="#3d8FFB" android:type="linear"/> <corners
android:topRightRadius="10dp" android:bottomLeftRadius="10dp" android:topLeftRadius="10dp" android:bottomRightRadius="10dp" /> </shape></item></selector>
效果如下
shape 做矩形
android:shape=”rectangle”選為矩形
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"><gradient
android:type="linear"android:startColor="@android:color/holo_blue_bright"android:centerColor="@android:color/holo_green_dark"android:endColor="@android:color/holo_red_light"android:useLevel="true"android:angle="45"/>
<stroke
android:width="2dp"android:color="@android:color/black"/></shape>
效果如下
shape 作描邊矩形 和 橢圓
shape 作描邊矩形 和 橢圓
這里注意shape
android:shape=”oval” 橢圓
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"><solid
android:color="@android:color/holo_blue_bright"/><stroke
android:width="2dp"android:color="@android:color/black"android:dashWidth="1dp"android:dashGap="2dp"/><corners android:topLeftRadius="20dp"
android:topRightRadius="20dp"
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp" android:radius="50dp"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"><solid android:color="@android:color/holo_orange_light"/><stroke
android:width="2dp"android:color="@android:color/black"/></shape>
效果如下
代碼
ShapeDemo代碼
參考鏈接
*Android shape屬性整理 - Because if I don’t write it down, I’ll forget it - 博客頻道 - CSDN.NET
總結(jié)
以上是生活随笔為你收集整理的Android之shape属性详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。