都是用工具翻譯的,不對的地方懇請大家指出來。
Shape Drawable原版
This is a generic shape defined in XML.
- file location:
-
The filename is used as the resource ID.res/drawable/filename.xml
compiled resource datatype: - Resource pointer to a
.GradientDrawable
resource reference: - In Java:
In XML:R.drawable.filename
@[package:]drawable/filename
syntax: -
<?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>
elements: -
-
The shape drawable. This must be the root element.
attributes:
- String. Required. Defines the XML namespace, which must be
."http://schemas.android.com/apk/res/android"
- Keyword. Defines the type of shape. Valid values are:
Value Desciption "rectangle"
A rectangle that fills the containing View. This is the default shape. "oval"
An oval shape that fits the dimensions of the containing View. "line"
A horizontal line that spans the width of the containing View. This shape requires the
element to define the width of the line.<stroke>
"ring"
A ring shape.
xmlns:android
android:shape
:android:shape="ring"
- Dimension. The radius for theinner part of the ring (the hole in the middle), as a dimension value or dimension resource.
- Float. The radius for the innerpart of the ring, expressed as a ratio of the ring's width. For instance, if
, then the inner radius equals the ring's width divided by 5. Thisvalue is overridden byandroid:innerRadiusRatio="5"
. Default value is 9.android:innerRadius
- Dimension. The thickness of thering, as a dimension value or dimension resource.
- Float. The thickness of the ring,expressed as a ratio of the ring's width. For instance, if
, thenthe thickness equals the ring's width divided by 2. This value is overridden byandroid:thicknessRatio="2"
. Default value is 3.android:innerRadius
- Boolean. "true" if this is used asa
. This should normally be "false" or your shape may not appear.LevelListDrawable
android:innerRadius
android:innerRadiusRatio
android:thickness
android:thicknessRatio
android:useLevel
- String. Required. Defines the XML namespace, which must be
-
Creates rounded corners for the shape. Applies only when the shape is a rectangle.
attributes:
- Dimension. The radius for all corners, as a dimension value or dimension resource. This is overridden for eachcorner by the following attributes.
- Dimension. The radius for the top-left corner, as a dimension value or dimension resource.
- Dimension. The radius for the top-right corner, as a dimension value or dimension resource.
- Dimension. The radius for the bottom-left corner, as a dimension value or dimension resource.
- Dimension. The radius for the bottom-right corner, as a dimension value or dimension resource.
android:radius
android:topLeftRadius
android:topRightRadius
android:bottomLeftRadius
android:bottomRightRadius
to set a default cornerradius greater than 1, but then override each and every corner with the values you reallywant, providing zero ("0dp") where you don't want rounded corners.android:radius
-
Specifies a gradient color for the shape.
attributes:
- Integer. The angle for the gradient, in degrees. 0 is left to right, 90 isbottom to top. It must be a multiple of 45. Default is 0.
- Float. The relative X-position for the center of the gradient (0 - 1.0).
- Float. The relative Y-position for the center of the gradient (0 - 1.0).
- Color. Optional color that comes between the start and end colors, as ahexadecimal value or color resource.
- Color. The ending color, as a hexadecimalvalue or color resource.
- Float. The radius for the gradient. Only applied when
.android:type="radial"
- Color. The starting color, as a hexadecimalvalue or color resource.
- Keyword. The type of gradient pattern to apply. Valid values are:
Value Description "linear"
A linear gradient. This is the default. "radial"
A radial gradient. The start color is the center color. "sweep"
A sweeping line gradient. - Boolean. "true" if this is used as a
.LevelListDrawable
android:angle
android:centerX
android:centerY
android:centerColor
android:endColor
android:gradientRadius
android:startColor
android:type
android:useLevel
-
Padding to apply to the containing View element (this pads the position of the Viewcontent, not the shape).
attributes:
- Dimension. Left padding, as a dimension value or dimension resource.
- Dimension. Top padding, as a dimension value or dimension resource.
- Dimension. Right padding, as a dimension value or dimension resource.
- Dimension. Bottom padding, as a dimension value or dimension resource.
android:left
android:top
android:right
android:bottom
-
The size of the shape.
attributes:
- Dimension. The height of the shape, as a dimension value or dimension resource.
- Dimension. The width of the shape, as a dimension value or dimension resource.
android:height
android:width
, you can restrict scaling by setting theImageView
toandroid:scaleType
."center"
-
A solid color to fill the shape.
attributes:
- Color. The color to apply to the shape, as a hexadecimalvalue or color resource.
android:color
-
A stroke line for the shape.
attributes:
- Dimension. The thickness of the line, as a dimension value or dimension resource.
- Color. The color of the line, as ahexadecimal value or color resource.
- Dimension. The distance between line dashes, as a dimension value or dimension resource. Only valid if
is set.android:dashWidth
- Dimension. The size of each dash line, as a dimension value or dimension resource. Only valid if
is set.android:dashGap
android:width
android:color
android:dashGap
android:dashWidth
<shape>
<corners>
<gradient>
<padding>
<size>
<solid>
<stroke>
example: -
- XML file saved at
:res/drawable/gradient_box.xml
This layout XML applies the shape drawable to a View:<?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 application code gets the shape drawable and applies it to a View:<TextView android:background="@drawable/gradient_box" android:layout_height="wrap_content" android:layout_width="wrap_content" />
Resources res = getResources() ; Drawable shape = res. getDrawable (R.drawable.gradient_box); TextView tv = (TextView)findViewByID(R.id.textview); tv.setBackground(shape);
see also: -
-
ShapeDrawable
-
-
檔案名會作為資源的ID來使用。res/drawable/filename.xml
資源資料類型: - 可以參考
。GradientDrawable
資源引用: - In Java:
In XML:R.drawable.filename
@[package:]drawable/filename
文法: -
<pre name="code" class="html"><?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必須作為根節點。
shape的屬性:
- String字元類型。必須定義xml的命名空間, 必須是
。"http://schemas.android.com/apk/res/android"
- 枚舉關鍵字。 定義shape的形狀。有效參數是:
Value Desciption "rectangle"
填充包含視圖的矩形。shape屬性值預設矩形(rectangle)。 "oval"
适合包含視圖的尺寸的橢圓形。 "line"
一條水準線,跨越包含視圖的寬度。這種形狀需要通過<stroke>元素來定義線的寬度。 "ring"
環形。
xmlns:android
android:shape
的情況下使用:android:shape="ring"
- Dimension 類型。 設定環形裡面的圓的半徑 (中間的圓/洞),設定一個具體尺寸 值或者參考 dimension resource。
- Float 類型. 設定環形裡面的圓的半徑, 用環形寬度的比例來表示。比如, 如果設定
, 那麼裡面圓的半徑為環形的寬度除以5android:innerRadiusRatio="5"
。這個屬性會被
屬性所覆寫。預設值為9。android:innerRadius
- Dimension 類型。環形的厚度,設定具體尺寸值或者參考 dimension resource。
- Float 類型。環形的厚度,用環形的寬度的比例來表示。比如:
,那麼裡面圓的厚度為環形的寬度除以2。如果設定 android:thicknessRatio="2"
這個屬性會被
屬性所覆寫。預設值為3。android:innerRadius
- Boolean 類型。如果被用來做
那麼為“true”。這個通常為false或者你的形狀不可見。LevelListDrawable
android:innerRadius
android:innerRadiusRatio
android:thickness
android:thicknessRatio
android:useLevel
- String字元類型。必須定義xml的命名空間, 必須是
-
為你的形狀設定圓角。隻有形狀為矩形的時候才能用。
屬性:
- Dimension類型。 設定四邊都為圓角, 尺寸值或者參考 dimension resource.。這個屬性會被以下屬性覆寫。
- Dimension類型。 設定左上角為圓角, 尺寸值或者參考 dimension resource。
- Dimension類型。 設定右上角為圓角, 尺寸值或者參考 dimension resource。
- Dimension類型。設定左下角為圓角, 尺寸值或者參考 dimension resource。
- Dimension類型。設定右下角為圓角, 尺寸值或者參考 dimension resource。
android:radius
android:topLeftRadius
android:topRightRadius
android:bottomLeftRadius
android:bottomRightRadius
屬性設定一個大于1的預設值,不過會被你設定的每個角覆寫, 如果你不希望設定圓角則可以設定為0(0dp)android:radius
-
為形狀指定一個漸變的顔色.
屬性:
- Integer類型。漸變的角度,用角度表示。 0 從左到右 90 從下到上。必須是45的倍數。預設值為0。
- Float類型。 相對X位置為漸變的中心 (0 - 1.0).
- Float類型。 相對Y位置為漸變的中心(0 - 1.0).
- Color類型。 設定從開始到結束之間的顔色,16進制值或者參考 color resource.
- Color類型。 結束的顔色,16進制值或者參考 color resource.
- Float類型。 漸變的半徑,隻有當
android:type="radial"的時候才能用。
- Color類型。 開始顔色, 16進制值或者參考 color resource.
- 枚舉關鍵字類型。 漸變模型方式, 有效參數值為:
Value Description "linear"
線性漸變,預設值 "radial"
發散漸變。開始顔色為過程顔色 "sweep"
流線漸變。 - Boolean類型。 如果被用作
則設在"true"。LevelListDrawable
android:angle
android:centerX
android:centerY
android:centerColor
android:endColor
android:gradientRadius
android:startColor
android:type
android:useLevel
-
包含視圖的内邊距(視圖的填充位置,不是形狀)。
屬性:
- Dimension類型。左内邊距,尺寸值或者參考 dimension resource.
- Dimension類型。 上内邊距, 尺寸值或者參考 dimension resource.
- Dimension類型。 右内邊距,尺寸值或者參考 dimension resource.
- Dimension類型。下内邊距, 尺寸值或者參考 dimension resource.
android:left
android:top
android:right
android:bottom
-
形狀的尺寸。
屬性:
- Dimension類型。形狀的高度,尺寸值或者參考 dimension resource.
- Dimension類型。 形狀的寬度,尺寸值或者參考 dimension resource.
android:height
android:width
為android:scaleType
"center"來限制縮放。
-
形狀填充純色。
屬性:
- Color類型。顔色值,16進制值或者參考 color resource.
android:color
-
為形狀畫線。
屬性:
- Dimension類型。 線的寬度, 尺寸值或者參考 dimension resource.
- Color類型。線的顔色,16進制值或者參考 color resource.
- Dimension類型。行之間破折号間距,尺寸值或者參考 dimension resource.
設定有效值了才生效(即設定虛線)。隻有屬性android:dashWidth
- Dimension類型。虛線的尺寸, 尺寸值或者參考 dimension resource.
隻有屬性android:
設定有效值了才生效。dashGap
android:width
android:color
android:dashGap
android:dashWidth
<shape>
<corners>
<gradient>
<padding>
<size>
<solid>
<stroke>
例子: -
-
:xml路徑:res/drawable/gradient_box.xml
-
Layout 的中的一個控件引用該shape形狀:<?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>
代碼擷取并應用到控件上:<TextView android:background="@drawable/gradient_box" android:layout_height="wrap_content" android:layout_width="wrap_content" />
Resources res = getResources(); Drawable shape = res. getDrawable(R.drawable.gradient_box); TextView tv = (TextView)findViewByID(R.id.textview); tv.setBackground(shape);
另請參閱: -
-
ShapeDrawable
-
Shape Drawable翻譯版
這是一個在xml中定義的一般形狀。- 檔案位置: