Android 基础(十三) shape
介紹
簡單來說,shape就是用來在xml文件中定義形狀,代碼解析之后就可以當做Drawable一樣使用
官方說明
關于shape定義的drawable
文件位置:res/drawable/filename.xml
編譯資源類型:GradientDrawable
文件引用:
In Java: R.drawable.filename
In XML: @[package:]drawable/filename
語法:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape=["rectangle" | "oval" | "line" | "ring"] > <corners android:radius="integer"android:topLeftRadius="integer"android:topRightRadius="integer"android:bottomLeftRadius="integer"android:bottomRightRadius="integer" /><gradient android:angle="integer"android:centerX="integer"android:centerY="integer"android:centerColor="integer"android:endColor="color"android:gradientRadius="integer"android:startColor="color"android:type=["linear" | "radial" | "sweep"]android:useLevel=["true" | "false"] /><padding android:left="integer"android:top="integer"android:right="integer"android:bottom="integer" /><size android:width="integer"android:height="integer" /><solid android:color="color" /><stroke android:width="integer"android:color="color"android:dashWidth="integer"android:dashGap="integer" /> </shape>這里只做簡單的描述,主要看看使用方式。關于元素的詳細說明,請看 shape說明
實際使用
矩形
填充(solid)
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><!--填充顏色 --><solid android:color="@color/colorAccent" /> </shape>描邊(stroke)
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><!--填充顏色 --><solid android:color="@color/colorAccent" /><!--描邊顏色--><!--android:dashGap 虛線間距,這里設置為0則顯示的為實線--><!--android:dashWidth 虛線寬度--><stroke android:width="3dp"android:color="@color/colorPrimaryDark"android:dashGap="0dp"android:dashWidth="10dp" /> </shape>圓角(corner)
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><!--填充顏色 --><solid android:color="@color/colorAccent" /><!--描邊顏色--><!--android:dashGap 虛線間距,這里設置為0則顯示的為實線--><!--android:dashWidth 虛線寬度--><stroke android:width="3dp"android:color="@color/colorPrimaryDark"android:dashGap="4dp"android:dashWidth="10dp" /><!--圓角--><!--android:randius 設置4個叫的圓角半徑,會被特定的圓角設定覆蓋--><!--android:bottomLeftRadius 左下角的圓角半徑--><!--android:bottomRightRadius 右下角的圓角半徑--><!--android:topLeftRandius 左上角的圓角半徑--><!--android:topRightRadius 右上角的圓角半徑--><corners android:bottomLeftRadius="60dp"android:radius="30dp"android:topRightRadius="120dp" /> </shape>漸變(gradient):linear
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><!--描邊顏色--><!--android:dashGap 虛線間距,這里設置為0則顯示的為實線--><!--android:dashWidth 虛線寬度--><stroke android:width="3dp"android:color="@color/colorPrimaryDark"android:dashGap="4dp"android:dashWidth="10dp" /><!--圓角--><!--android:randius 設置4個叫的圓角半徑--><!--android:bottomLeftRadius 左下角的圓角半徑--><!--android:bottomRightRadius 右下角的圓角半徑--><!--android:topLeftRandius 左上角的圓角半徑--><!--android:topRightRadius 右上角的圓角半徑--><corners android:bottomLeftRadius="60dp"android:radius="30dp"android:topRightRadius="120dp" /><!--漸變--><gradient android:angle="45"android:centerColor="@color/stone"android:endColor="@color/pink"android:startColor="@color/yellow" /></shape>漸變(gradient):radial
<gradientandroid:angle="90"android:startColor="@color/colorPrimary"android:centerColor="@color/pink"android:endColor="@color/yellow"android:gradientRadius="400dp"android:type="radial"/>漸變(gradient):sweep
<gradientandroid:startColor="@color/colorPrimary"android:centerColor="@color/pink"android:endColor="@color/yellow"android:gradientRadius="400dp"android:type="sweep"/>圓形
正圓
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <gradient android:centerColor="@color/pink"android:endColor="@color/yellow"android:startColor="@color/colorPrimary" /> <size android:width="400dp"android:height="400dp" /> </shape>橢圓
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <gradient android:angle="90"android:centerColor="@color/pink"android:endColor="@color/yellow"android:startColor="@color/colorPrimary" /> </shape>線條
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="line"><stroke android:width="9dp"android:color="@color/pink" /> </shape>環形
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="ring"android:innerRadius="100dp"android:thickness="50dp"android:useLevel="false"><gradient android:startColor="@color/colorAccent"android:endColor="@color/yellow"android:centerColor="@color/pink"/> </shape>android:useLevel=”false”這個屬性值一定要設置成false,根據google官網上的解釋:
以下屬性只能在android:shape="ring"的時候使用:
| android:innerRadius | 尺寸,內環的半徑 |
| android:thickness | 尺寸,環的厚度 |
| android:innerRadiusRatio | 浮點型,以環的寬度比率來表示內環的半徑, 例如,如果android:innerRadiusRatio=5,表示內環半徑等于環的寬度除以5,這個值是可以被android:innerRadius的值覆蓋,默認為9. |
| android:thicknessRatio | 浮點型,以環的寬度比率來表示環的厚度,例如,如果android:thicknessRatio=2, 那么環的厚度就等于環的寬度除以2。這個值是可以被android:thickness覆蓋的,默認值是3. |
| android:useLevel | boolean值,如果當做是LevelListDrawable使用時值為true,否則為false.這個值一般為false,否則你的環可能不會出現 |
其他說明
這些自己定義的shape為根節點的drawable xml文件,可以用來當成背景使用在Button,TextView等視圖
上,同時由于可以設置size大小,也可以用來制作簡單的圖標等??偠灾?#xff0c;每個細小的東西,都有挖掘的
價值,感覺這里面還有一些東西我沒有注意到,還要好好的看一下文檔。
最后,google鏡像網站,xsoftlab,當然有條件的建議使用Google官網。
轉載于:https://www.cnblogs.com/lanzhi/p/6467191.html
總結
以上是生活随笔為你收集整理的Android 基础(十三) shape的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java笔试题解答和部分面试题
- 下一篇: myeclipse部署ssh项目工程