上兩篇講了佈局控件一些常用的屬性介紹。今天講下shape。
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="10dp"/>
<solid android:color="@android:color/black"/>
<stroke android:color="@color/colorAccent" android:width="1px"/>
</shape>
shape有幾種形狀:
android:shape=“rectangle|line|oval|ring”// 矩形、線條、橢圓、圓環
shape有幾種屬性:
corners:圓角半徑
gradient:顏色漸變
padding:內邊距
size:尺寸大小
solid:填充顏色
stroke:邊框
下面將一一介紹各個屬性都有哪些參數可以設置:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:useLevel="false"// 必須爲false,否則無法顯示
android:innerRadiusRatio="2.5"// 比率=圓環寬度/圓環內徑
android:innerRadius="20dp"// 圓環內徑
android:thickness="10dp"// 圓環厚度
android:thicknessRatio="2.5"// 比率=圓環寬度/圓環厚度
android:tint="@color/colorAccent"// shape顏色,solid顏色會失效
>
<corners
android:bottomLeftRadius="10dp"// 坐下角
android:bottomRightRadius="10dp"// 右下角
android:topLeftRadius="10dp"// 左上角
android:topRightRadius="10dp"/>// 右上角
<corners
android:radius="10dp" />
這兩段代碼效果是一樣的,都是表示圓角半徑爲10dp。
<gradient
android:angle="45"// 漸變角度,只能是45的倍數
android:startColor=""// 漸變的初始顏色
android:endColor=""// 漸變的終止顏色
android:centerColor=""// 漸變中心的顏色
android:centerX="50%"// 漸變中點的x座標:float:0~1
android:centerY="50%"// 漸變中點的y座標:float:0~1
android:gradientRadius="0dp"// 漸變半徑,type=radial
android:type="sweep|radial|linear"// 漸變效果:掃描、濺射、線性
android:useLevel="true|false"/>// 暫未用過
<padding // 內邊距
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp"/>
<size // 設置圖形固定大小
android:height="10dp"
android:width="200dp"/>
<solid // 設置填充圖形的顏色
android:color="@android:color/black"/>
<stroke // 設置線條邊框
android:color="@android:color/black"
android:width="1px"// 邊框寬度
android:dashGap="10dp"// 虛線間隙
android:dashWidth="10dp"/> // 虛線寬度
注意:當shape="line"時,View的android:layout_height(size的height)值要比stroke的width大,否則不會顯示;當畫虛線時,需要關閉硬件加速,可再View的佈局中添加android:layerType=“software”,當然還有其他方法。