Android中shape的簡單介紹

均屬于筆記,僅供個人參考,有問題歡迎指正,整理模式

Android中使用shape來定義控件的一些簡單顯示屬性,如按鈕的背景等,應(yīng)用靈活方便。

1,示例代碼:

<?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">?

? ? ? ? ? ? <!-- 漸變 -->?

? ? ? ? ? ? <gradient? ?

? ? ? ? ? ? ? ? android:startColor="#55B4FE"? ?

? ? ? ? ? ? ? ? android:endColor="#3d8FFB"? ?

? ? ? ? ? ? ? ? android:angle="-90"?

? ? ? ? ? ? ? ? android:type="linear"/>? ?

? ? ? ? ? ? <!-- 描邊 -->? ?

? ? ? ? ? ? <stroke android:width="1dip"?

? ? ? ? ? ? ? ? android:color="#d3d3d3"?

? ? ? ? ? ? ? ? />?

? ? ? ? ? ? <!-- 圓角 -->? ?

? ? ? ? ? ? <corners?

? ? ? ? ? ? ? ? android:topRightRadius="0dp"? ? ?

? ? ? ? ? ? ? ? android:bottomLeftRadius="10dp"? ?

? ? ? ? ? ? ? ? android:topLeftRadius="0dp"? ? ?

? ? ? ? ? ? ? ? android:bottomRightRadius="10dp"? ? ?

? ? ? ? ? ? ? ? />?


? ? ? ? </shape>? ? ? ? ? ? ? ?

? ? </item>?

? ? <item>?

? ? ? ? <shape xmlns:android="http://schemas.android.com/apk/res/android"?

? ? ? ? ? ? android:shape="rectangle">?


? ? ? ? ? ? <solid android:color="#ffffff" />? ? ? ? ? ?

? ? ? ? ? ? <!-- 描邊 -->? ?

? ? ? ? ? ? <stroke android:width="1dip"?

? ? ? ? ? ? ? ? android:color="#d3d3d3"?

? ? ? ? ? ? ? ? />?

? ? ? ? ? ? <!-- 圓角 -->? ?

? ? ? ? ? ? <corners?

? ? ? ? ? ? ? ? android:topRightRadius="0dp"? ? ?

? ? ? ? ? ? ? ? android:bottomLeftRadius="10dp"? ?

? ? ? ? ? ? ? ? android:topLeftRadius="0dp"? ? ?

? ? ? ? ? ? ? ? android:bottomRightRadius="10dp"? ? ?

? ? ? ? ? ? ? ? />?


? ? ? ? </shape>?

? ? </item>?

</selector>?

2,說明:

gradient:漸變

android:startColor 漸變開始的顏色

android:endColor 漸變結(jié)束的顏色

android:centerColor 中間點的顏色

ndroid:angle是漸變角度,必須為45的整數(shù)倍。

android:type? linear線性漸變;radial徑向漸變

android:gradientRadius 徑向漸變的半徑

solid:填充

android:color 使用的填充顏色

stroke:描邊

android:width 描邊的寬度,

android:color 描邊的顏色。

我們還可以把描邊弄成虛線的形式,設(shè)置方式為:

android:dashWidth="5dp" 一個'-'的寬度

android:dashGap="3dp" 間隔的寬度

corners:圓角

android:radius為角的弧度,值越大角越圓。

分開設(shè)置:

android:topRightRadius="20dp"? ? 右上角?

android:bottomLeftRadius="20dp"? ? 右下角?

android:topLeftRadius="1dp"? ? 左上角?

android:bottomRightRadius="0dp"? ? 左下角?

padding:內(nèi)間隔

3,將代碼保存在res/drawable目錄中,在使用時直接引入文件即可,如:

<Button?

? ? android:id="@+id/testButton"?

? ? android:layout_width="wrap_content"?

? ? android:layout_height="wrap_content"?

? ? android:background="@drawable/test_button_bg"/>?

4,示例代碼和注釋

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

<!--?

? shape drawable xml文件中定義的一個幾何圖形,定義在res/drawable/目錄下,文件名filename稱為訪問的資源ID?

? 在代碼中通過R.drawable.filename進行訪問,在xml文件中通過@[package:]drawable/filename進行訪問。?

-->?

<!--?

? android:shape=["rectangle" | "oval" | "line" | "ring"]?

? shape的形狀,默認為矩形,可以設(shè)置為矩形(rectangle)、橢圓形(oval)、線性形狀(line)、環(huán)形(ring) 下面的屬性只有在android:shape="ring時可用:?

? android:innerRadius 尺寸,內(nèi)環(huán)的半徑。?

? android:innerRadiusRatio 浮點型,以環(huán)的寬度比率來表示內(nèi)環(huán)的半徑,?

? 例如,如果android:innerRadiusRatio,表示內(nèi)環(huán)半徑等于環(huán)的寬度除以5,這個值是可以被覆蓋的,默認為9.?

? android:thickness ? 尺寸,環(huán)的厚度?

? android:thicknessRatio ? 浮點型,以環(huán)的寬度比率來表示環(huán)的厚度,例如,如果android:thicknessRatio="2",?

? 那么環(huán)的厚度就等于環(huán)的寬度除以2。這個值是可以被android:thickness覆蓋的,默認值是3.?

? android:useLevel boolean值,如果當做是LevelListDrawable使用時值為true,否則為false.?

? -->?

<shape?

? xmlns:android="http://schemas.android.com/apk/res/android"?

? android:shape="rectangle">?


? <!--?

? ? 圓角?

? ? android:radius ? 整型 半徑?

? ? android:topLeftRadius ? 整型 左上角半徑?

? ? android:topRightRadius ? 整型 右上角半徑?

? ? android:bottomLeftRadius 整型 左下角半徑?

? ? android:bottomRightRadius? 整型 右下角半徑?

? -->?

? <corners

? ? android:radius="8dp"?

? ? android:topLeftRadius="5dp"?

? ? android:topRightRadius="15dp"?

? ? android:bottomLeftRadius="20dp"?

? ? android:bottomRightRadius="25dp"

? ? />?


? <!--?

? ? 漸變色?

? ? android:startColor? 顏色值 起始顏色?

? ? android:endColor 顏色值 結(jié)束顏色?

? ? android:centerColor 整型 ? 漸變中間顏色,即開始顏色與結(jié)束顏色之間的顏色?

? ? android:angle ? 整型 ? 漸變角度(PS:當angle=0時,漸變色是從左向右。 然后逆時針方向轉(zhuǎn),當angle=90時為從下往上。angle必須為45的整數(shù)倍)?

? ? android:type ["linear" | "radial" | "sweep"] 漸變類型(取值:linear、radial、sweep)?

? ? ? ? ? ? ? linear 線性漸變,這是默認設(shè)置?

? ? ? ? ? ? ? radial 放射性漸變,以開始色為中心。?

? ? ? ? ? ? ? sweep 掃描線式的漸變。?

? ? android:useLevel ["true" | "false"] ? 如果要使用LevelListDrawable對象,就要設(shè)置為true。設(shè)置為true無漸變。false有漸變色?

? ? android:gradientRadius 整型 漸變色半徑.當 android:type="radial" 時才使用。單獨使用 android:type="radial"會報錯。?

? ? android:centerX ? 整型 ? 漸變中心X點坐標的相對位置?

? ? android:centerY ? 整型 ? 漸變中心Y點坐標的相對位置?

? -->?

? <gradient?

? ? android:startColor="#FFFF0000"?

? ? android:endColor="#80FF00FF"?

? ? android:angle="45"?

? ? />?


? <!--?

? ? 內(nèi)邊距,即內(nèi)容與邊的距離?

? ? android:left 整型 左內(nèi)邊距?

? ? android:top 整型 上內(nèi)邊距?

? ? android:right? 整型 右內(nèi)邊距?

? ? android:bottom? 整型 下內(nèi)邊距?

? ? -->?

? <padding?

? ? android:left="10dp"?

? ? android:top="10dp"?

? ? android:right="10dp"?

? ? android:bottom="10dp"?

? ? />?


? <!--?

? ? size 大小?

? ? android:width? 整型 寬度?

? ? android:height? 整型 高度?

? -->?

? <size?

? ? android:width="600dp"?

? ? />?


? <!--?

? ? 內(nèi)部填充?

? ? android:color? 顏色值 填充顏色?

? -->?

? <solid?

? ? android:color="#ffff9d77"?

? ? />?


? <!--?

? ? 描邊?

? ? android:width ? 整型? 描邊的寬度?

? ? android:color ? 顏色值 描邊的顏色?

? ? android:dashWidth? 整型? 表示描邊的樣式是虛線的寬度, 值為0時,表示為實線。值大于0則為虛線。?

? ? android:dashGap 整型? 表示描邊為虛線時,虛線之間的間隔 即“ - - - - ”?

? -->?

? <stroke?

? ? android:width="2dp"?

? ? android:color="#dcdcdc"

? ? />?

</shape>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容