目錄
什么是Shape?
shape屬性
子標簽屬性
corners (圓角)
solid (填充色)
gradient (漸變)
stroke (描邊)
padding (內邊距)
size (大小)
特殊屬性
rectangle(矩形)
oval(橢圓)
line(線)
ring(圓環)
shape 用法
什么是Shape?
在Android開發中,我們可以使用shape定義各種各樣的形狀,也可以定義一些圖片資源。相對于傳統圖片來說,使用shape可以減少資源占用,減少安裝包大小,還能夠很好地適配不同尺寸的手機。
shape屬性
shape屬性的基本語法示例
<?xml version="1.0" encoding="utf-8"?>
<shapexmlns: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可以定義控件的一些展示效果,例如圓角,漸變,填充,描邊,大小,邊距; shape 子標簽就可以實現這些效果,shape 子標簽有下面幾個屬性:corners,gradient,padding,size,solid,stroke
corners (圓角)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" ><corners //定義圓角android:radius="10dp" //全部的圓角半徑;android:topLeftRadius="5dp" //左上角的圓角半徑;android:topRightRadius="5dp" //右上角的圓角半徑;android:bottomLeftRadius="5dp" //左下角的圓角半徑;android:bottomRightRadius="5dp" /> //右下角的圓角半徑。
</shape>
solid (填充色)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" ><solid android:color="#ffff00"/> //內部填充色
</shape>
gradient (漸變)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" ><gradientandroid:type=["linear" | "radial" | "sweep"] //共有3中漸變類型,線性漸變(默認)/放射漸變/掃描式漸變;android:angle="90" //漸變角度,必須為45的倍數,0為從左到右,90為從上到下;android:centerX="0.5" //漸變中心X的相當位置,范圍為0~1;android:centerY="0.5" //漸變中心Y的相當位置,范圍為0~1;android:startColor="#24e9f2" //漸變開始點的顏色;android:centerColor="#2564ef" //漸變中間點的顏色,在開始與結束點之間;android:endColor="#25f1ef" //漸變結束點的顏色;android:gradientRadius="5dp" //漸變的半徑,只有當漸變類型為radial時才能使用;android:useLevel="false" /> //使用LevelListDrawable時就要設置為true。設為false時才有漸變效果。
</shape>
stroke (描邊)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" ><strokeandroid:width="1dp" //描邊的寬度android:color="#ff0000" //描邊的顏色// 以下兩個屬性設置虛線android:dashWidth="1dp" //虛線的寬度,值為0時是實線android:dashGap="1dp" />//虛線的間隔
</shape>
padding (內邊距)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" ><paddingandroid:left="10dp" //左內邊距;android:top="10dp" //上內邊距;android:right="10dp" //右內邊距;android:bottom="10dp" /> //下內邊距。
</shape>
size (大小)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" ><sizeandroid:width="50dp" //寬度android:height="50dp" />// 高度
</shape>
特殊屬性
Shape可以定義當前Shape的形狀的,比如矩形,橢圓形,線形和環形;這些都是通過 shape 標簽屬性來定義的, shape 標簽有下面幾個屬性:rectangle,oval,line,ring
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape=["rectangle" | "oval" | "line" | "ring"] //shape的形狀,默認為矩形,可以設置為矩形(rectangle)、橢圓形(oval)、線性形狀(line)、環形(ring)//下面的屬性只有在android:shape="ring"時可用:android:innerRadius="10dp" // 內環的半徑;android:innerRadiusRatio="2" // 浮點型,以環的寬度比率來表示內環的半徑;android:thickness="3dp" // 環的厚度;android:thicknessRatio="2" // 浮點型,以環的寬度比率來表示環的厚度;android:useLevel="false"> // boolean值,如果當做是LevelListDrawable使用時值為true,否則為false。
</shape>
rectangle(矩形)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><solid android:color="@color/colorPrimary"/>
</shape>
oval(橢圓)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="oval"><solid android:color="@color/colorPrimary"/><size android:height="100dp"android:width="100dp"/>
</shape>
line(線)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="line"><strokeandroid:width="1dp"android:color="@color/colorAccent"android:dashGap="3dp"//虛線間距android:dashWidth="4dp"/>//虛線寬度<size android:height="3dp"/>
</shape>
ring(圓環)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="ring"android:useLevel="false"android:innerRadius="20dp" // 內環的半徑android:thickness="10dp"> // 圓環寬度<!--useLevel需要設置為false--><solid android:color="@color/colorAccent"/>
</shape>
shape 用法
1.?在res/drawable下新建 shape_text.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><cornersandroid:radius="5dp"android:topLeftRadius="15dp"android:topRightRadius="15dp"android:bottomLeftRadius="15dp"android:bottomRightRadius="15dp" /><gradientandroid:startColor="#FF0000"android:endColor="#80FF00"android:angle="45" /><paddingandroid:left="10dp"android:top="10dp"android:right="10dp"android:bottom="10dp" /><sizeandroid:width="200dp"android:height="200dp" /><solid android:color="#ffff9d" /><strokeandroid:width="2dp"android:color="#dcdcdc" />
</shape>
2.?在布局中引用 shape_text.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Shape測試"android:background="@drawable/shape_text"android:textSize="15sp"android:textColor="@android:color/black"/>
</LinearLayout>
shape 使用示例
在src-main-res-drawable下,右鍵 New-Drawable Resource File
會生成一個這樣的文件
然后在上面代碼中找一個示例
然后在我們的 layout 文件中使用 shape,使用效果圖如下
?