定義:這是一個通用的,可以自定義形狀的xml。
用法:
<?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>形狀,預設矩形。
屬性 | 說明 |
---|---|
android:shape | 值:"rectangle"(預設):矩形;"oval":橢圓形;"line":線性形狀;"ring":環形。PS,此表以下屬性僅當android:shape="ring"時使用。 |
android:innerRadius | shape為ring時,内環半徑 |
android:innerRadiusRatio | 浮點型,以環的寬度比率來表示内環的半徑 例如,如果android:innerRadiusRatio=“5”,表示内環半徑等于環的寬度除以5,這個值是可以被覆寫的,預設為9. |
android:thickness | shape為ring時,環的厚度 |
android:thicknessRatio | 浮點型,以環的寬度比率來表示環的厚度 例如,如果android:thicknessRatio="2",那麼環的厚度就等于環的寬度除以2。這個值是可以被android:thickness覆寫的,預設值是3. |
android:useLevel | boolean值,如果當做是LevelListDrawable使用時值為true,否則為false. |
<corners>圓角
屬性 | 說明 |
---|---|
android:radius | 整型 半徑 |
android:topLeftRadius | 整型 左上角半徑 |
android:topRightRadius | 整型 右上角半徑 |
android:bottomLeftRadius | 整型 左下角半徑 |
android:bottomRightRadius | 整型 右下角半徑 |
<gradient>漸變
屬性 | 說明 |
---|---|
android:angle | 整型 漸變角度(PS:當angle=0時,漸變色是從左向右。 然後逆時針方向轉,當angle=90時為從下往上。angle必須為45的整數倍) |
android:centerX | 整型 漸變中心X點坐标的相對位置 |
android:centerY | 整型 漸變中心Y點坐标的相對位置 |
android:centerColor | 整型 漸變中間顔色,即開始顔色與結束顔色之間的顔色 |
android:endColor | 顔色值 結束顔色 |
android:gradientRadius | 整型 漸變色半徑.當 android:type="radial" 時才使用。單獨使用 android:type="radial"會報錯。 |
android:startColor | 顔色值 起始顔色 |
android:type | linear 線性漸變,這是預設設定 radial 放射性漸變,以開始色為中心。 sweep 掃描線式的漸變。 |
android:useLevel | ["true" | "false"] 如果要使用LevelListDrawable對象,就要設定為true。設定為true無漸變。false有漸變色 |
<solid>内部填充
屬性 | 說明 |
android:color | 顔色值 填充顔色 |
<stroke>描邊
屬性 | 說明 |
---|---|
android:width | 整型 描邊的寬度 |
android:color | 顔色值 描邊的顔色 |
android:dashGap | 整型 表示描邊為虛線時,虛線之間的間隔 即“ - - - - ”。 |
android:dashWidth | 整型 表示描邊的樣式是虛線的寬度, 值為0時,表示為實線。值大于0則為虛線。 |
官方文檔:
XML file saved at res/drawable/gradient_box.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#FFFF0000"
android:endColor="#80FF00FF"
android:angle="45"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<corners android:radius="8dp" />
</shape>
This layout XML applies the shape drawable to a View:
<TextView
android:background="@drawable/gradient_box"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
This application code gets the shape drawable and applies it to a View:
Resources res = getResources();
Drawable shape = res. getDrawable(R.drawable.gradient_box);
TextView tv = (TextView)findViewByID(R.id.textview);
tv.setBackground(shape);
效果:
resource_drawable_shape_rectangle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="45"
android:endColor="#80FF00FF"
android:startColor="#FFFF0000" />
<padding
android:bottom="7dp"
android:left="7dp"
android:right="7dp"
android:top="7dp" />
<corners android:radius="15dp" />
</shape>
resource_drawable_shape_oval.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<gradient
android:angle="45"
android:endColor="@color/yellow"
android:startColor="@color/red" />
<stroke android:color="@color/green"
android:width="5dp"
android:dashWidth="1dp"
android:dashGap="1dp"/>
</shape>
resource_drawable_shape_line.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke android:width="2dp" android:color="@color/blue"/>
<size android:height="5dp" />
</shape>
resource_drawable_shape_ring.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="50dp"
android:shape="ring"
android:thickness="10dp"
android:useLevel="false">
<solid android:color="@color/red" />
</shape>
效果: