都是用工具翻译的,不对的地方恳请大家指出来。
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中定义的一般形状。- 文件位置: