android的Drawable
Android Drawable XML Documentation
The missing manual! Each of the top-level elements are useable in Android XML files, in /res/drawable/. Some of them are allowed to be contained in others and this is noted below.
Element | Class | Description |
---|---|---|
<animated-rotate> | AnimatedRotateDrawable | A Drawable that can animate a rotation of another Drawable. |
<animation-list> | AnimationDrawable | An object used to create frame-by-frame animations, defined by a series of Drawable objects, which can be used as a View object's background. |
<bitmap> | BitmapDrawable | A Drawable that wraps a bitmap and can be tiled, stretched, or aligned. |
<clip> | ClipDrawable | A Drawable that clips another Drawable based on this Drawable's current level value. You can control how much the child Drawable gets clipped in width and height based on the level, as well as a gravity to control where it is placed in its overall container. Most often used to implement things like progress bars. |
<color> | ColorDrawable | A specialized Drawable that fills the Canvas with a specified color, with respect to the clip region. |
<inset> | InsetDrawable | A Drawable that insets another Drawable by a specified distance. This is used when a View needs a background that is smaller than the View's actual bounds. |
<layer-list> | LayerDrawable | A Drawable that manages an array of other Drawables. These are drawn in array order. |
<level-list> | LevelListDrawable | A resource that manages a number of alternate Drawables, each assigned a maximum numerical value. |
<nine-patch> | NinePatchDrawable | A resizeable bitmap, with stretchable areas that you define. |
<rotate> | RotateDrawable | A Drawable that can rotate another Drawable based on the current level value. The start and end angles of rotation can be controlled to map any circular arc to the level values range. |
<scale> | ScaleDrawable | A Drawable that changes the size of another Drawable based on its current level value. |
<selector> | StateListDrawable | Lets you assign a number of graphic images to a single Drawable and swap out the visible item based on state. |
<shape> | GradientDrawable | Basic method for drawing shapes via XML. |
<transition> | TransitionDrawable | An extension of <layer-list> that is intended to cross-fade between the first and second layer. The format is exactly the same as <layer-list>. |
<animated-rotate>
A Drawable that can animate a rotation of another Drawable.
Attributes
Name | Type | Default | Description |
---|---|---|---|
visible | boolean | parent|true | Determines if drawable is visible. |
frameDuration | integer | 150 | The duration of each frame, in milliseconds. |
framesCount | integer | 12 | Number of frames of rotation to animate. |
pivotX | float|fraction | .5 | The pivot point, as a fraction of the width. |
pivotY | float|fraction | .5 | The pivot point, as a fraction of the height. |
drawable | reference | null | The drawable to use for this item. Either this must be present or a drawable subelement must exist. |
Children
Element | Description |
---|---|
<[any drawable tag]> | Any drawable xml. Either this must be present or the 'drawable' attribute must be set. |
<[any drawable tag]>
Any drawable xml. Either this must be present or the 'drawable' attribute must be set.
<animation-list>
An object used to create frame-by-frame animations, defined by a series of Drawable objects, which can be used as a View object's background.
Attributes
Name | Type | Default | Description |
---|---|---|---|
visible | boolean | parent|true | Determines if drawable is visible. |
variablePadding | boolean | false | If true, allows the drawable's padding to change based on the current state that is selected. |
oneshot | boolean | false | If true, the animation will only run a single time and then stop. |
Children
Element | Description |
---|---|
<item> | A drawable for a frame of animation. |
<item>
A drawable for a frame of animation.
Attributes
Name | Type | Default | Description |
---|---|---|---|
drawable | reference | null | The drawable to use for this item. Either this must be present or a drawable subelement must exist. |
duration | integer | -1 | The duration of this frame, in milliseconds. Required. |
Children
Element | Description |
---|---|
<[any drawable tag]> | Any drawable xml. Either this must be present or the 'drawable' attribute must be set. |
<[any drawable tag]>
Any drawable xml. Either this must be present or the 'drawable' attribute must be set.
<bitmap>
A Drawable that wraps a bitmap and can be tiled, stretched, or aligned.
Attributes
Name | Type | Default | Description |
---|---|---|---|
src | reference | N/A | The source for the Bitmap. Required. |
antialias | boolean | false | Enables or disables antialiasing. |
filter | boolean | true | Enables or disables bitmap filtering. Filtering is used when the bitmap is shrunk or stretched to smooth its apperance. |
dither | boolean | true | Enables or disables dithering of the bitmap if the bitmap does not have the same pixel configuration as the screen (for instance: a ARGB 8888 bitmap with an RGB 565 screen). |
gravity | enum (top, bottom, left, right, center_vertical, fill_vertical, center_horizontal, fill_horizontal, center, fill, clip_vertical, clip_horizontal) | fill | Defines the gravity for the bitmap. The gravity indicates where to position the drawable in its container if the bitmap is smaller than the container. |
tileMode | enum (disabled, clamp, repeat, mirror) | disabled | Defines the tile mode. When the tile mode is enabled, the bitmap is repeated. Gravity is ignored when the tile mode is enabled. Values can be 'clamp' (replicates the edge color), 'repeat' (repeats the bitmap in both directions), 'mirror' (repeats the shader's image horizontally and vertically, alternating mirror images so that adjacent images always seam). |
<clip>
A Drawable that clips another Drawable based on this Drawable's current level value. You can control how much the child Drawable gets clipped in width and height based on the level, as well as a gravity to control where it is placed in its overall container. Most often used to implement things like progress bars.
Attributes
Name | Type | Default | Description |
---|---|---|---|
clipOrientation | enum (horizontal, vertical) | horizontal | The orientation for the clip. |
gravity | enum (top, bottom, left, right, center_vertical, fill_vertical, center_horizontal, fill_horizontal, center, fill, clip_vertical, clip_horizontal) | left | Specifies where to clip within the drawable. |
drawable | reference | null | The drawable to use for this item. Either this must be present or a drawable subelement must exist. |
Children
Element | Description |
---|---|
<[any drawable tag]> | Any drawable xml. Either this must be present or the 'drawable' attribute must be set. |
<[any drawable tag]>
Any drawable xml. Either this must be present or the 'drawable' attribute must be set.
<color>
A specialized Drawable that fills the Canvas with a specified color, with respect to the clip region.
Attributes
Name | Type | Default | Description |
---|---|---|---|
color | color | The color to fill the Canvas with. |
<inset>
A Drawable that insets another Drawable by a specified distance. This is used when a View needs a background that is smaller than the View's actual bounds.
Attributes
Name | Type | Default | Description |
---|---|---|---|
drawable | reference | null | The drawable to use for this item. Either this must be present or a drawable subelement must exist. |
visible | boolean | parent|true | Determines if drawable is visible. |
insetLeft | dimension | 0 | The left padding for the inset. |
insetRight | dimension | 0 | The right padding for the inset. |
insetTop | dimension | 0 | The top padding for the inset. |
insetBottom | dimension | 0 | The bottom padding for the inset. |
Children
Element | Description |
---|---|
<[any drawable tag]> | Any drawable xml. Either this must be present or the 'drawable' attribute must be set. |
<[any drawable tag]>
Any drawable xml. Either this must be present or the 'drawable' attribute must be set.
<layer-list>
A Drawable that manages an array of other Drawables. These are drawn in array order.
layer-list可以将多个图片按照顺序层叠起来,举个例子,要用2张图片叠加起来,看上去形成一张图片
Children
Element | Description |
---|---|
<item> | Layer for the layer-list. |
<item>
Layer for the layer-list.
Attributes
Name | Type | Default | Description |
---|---|---|---|
drawable | reference | null | The drawable to use for this item. Either this must be present or a drawable subelement must exist. |
left | dimension | 0 | Left padding of this layer. |
top | dimension | 0 | Top padding of this layer. |
right | dimension | 0 | Right padding of this layer. |
bottom | dimension | 0 | Bottom padding of this layer. |
id | id | View.NO_ID | The id of this layer. |
Children
Element | Description |
---|---|
<[any drawable tag]> | Any drawable xml. Either this must be present or the 'drawable' attribute must be set. |
<[any drawable tag]>
Any drawable xml. Either this must be present or the 'drawable' attribute must be set.
<level-list>
A resource that manages a number of alternate Drawables, each assigned a maximum numerical value.
Children
Element | Description |
---|---|
<item> | Drawable for the level. |
<item>
Drawable for the level.
Attributes
Name | Type | Default | Description |
---|---|---|---|
drawable | reference | null | The drawable to use for this item. Either this must be present or a drawable subelement must exist. |
minLevel | integer | 0 | The minimum level allowed for this item. |
maxLevel | integer | 0 | The maximum level allowed for this item. Required for each item tag. |
Children
Element | Description |
---|---|
<[any drawable tag]> | Any drawable xml. Either this must be present or the 'drawable' attribute must be set. |
<[any drawable tag]>
Any drawable xml. Either this must be present or the 'drawable' attribute must be set.
<nine-patch>
A resizeable bitmap, with stretchable areas that you define.
Attributes
Name | Type | Default | Description |
---|---|---|---|
src | reference | N/A | The source for the nine patch. Required. |
dither | boolean | true | Enables or disables dithering of the bitmap if the bitmap does not have the same pixel configuration as the screen (for instance: a ARGB 8888 bitmap with an RGB 565 screen). |
<rotate>
A Drawable that can rotate another Drawable based on the current level value. The start and end angles of rotation can be controlled to map any circular arc to the level values range.
Attributes
Name | Type | Default | Description |
---|---|---|---|
visible | boolean | parent|true | Determines if drawable is visible. |
fromDegrees | float | 0.0 | The initial rotation, used on the lowest level. |
toDegrees | float | 360.0 | The final rotation, used on the highest level. |
pivotX | float|fraction | .5 | The pivot point, as a fraction of the width. |
pivotY | float|fraction | .5 | The pivot point, as a fraction of the height. |
drawable | reference | null | The drawable to use for this item. Either this must be present or a drawable subelement must exist. |
Children
Element | Description |
---|---|
<[any drawable tag]> | Any drawable xml. Either this must be present or the 'drawable' attribute must be set. |
<[any drawable tag]>
Any drawable xml. Either this must be present or the 'drawable' attribute must be set.
<scale>
A Drawable that changes the size of another Drawable based on its current level value.
Attributes
Name | Type | Default | Description |
---|---|---|---|
scaleWidth | string|percentage | -1 | Scale width, expressed as a percentage of the drawable's bound. The value's format is XX%. For instance: 100%, 12.5%, etc. |
scaleHeight | string|percentage | -1 | Scale height, expressed as a percentage of the drawable's bound. The value's format is XX%. For instance: 100%, 12.5%, etc. |
scaleGravity | enum (top, bottom, left, right, center_vertical, fill_vertical, center_horizontal, fill_horizontal, center, fill, clip_vertical, clip_horizontal) | left | Specifies where the drawable is positioned after scaling. |
drawable | reference | N/A | The drawable to scale; must be defined. |
<selector>
Lets you assign a number of graphic images to a single Drawable and swap out the visible item based on state.
Attributes
Name | Type | Default | Description |
---|---|---|---|
visible | boolean | parent|true | Determines if drawable is visible. |
variablePadding | boolean | false | If true, allows the drawable's padding to change based on the current state that is selected. |
constantSize | boolean | false | If true, the drawable's reported internal size will remain constant as the state changes; the size is the maximum of all of the states. |
dither | boolean | true | Set to true to have the drawable dither its colors when drawn to a device with fewer than 8-bits per color component. |
Children
Element | Description |
---|---|
<item> | Sets up a drawable for a particular collection of states. |
<item>
Sets up a drawable for a particular collection of states.
Attributes
Name | Type | Default | Description |
---|---|---|---|
drawable | reference | null | The drawable to use for this item. Either this must be present or a drawable subelement must exist. |
state_focused | boolean | false | This state indicates the view is focused. |
state_window_focused | boolean | false | This state indicates the view's window has focus. |
state_enabled | boolean | false | This state indicates the view is enabled. |
state_checkable | boolean | false | This state indicates the view can be checked |
state_checked | boolean | false | This state indicates the view is checked. |
state_selected | boolean | false | This state indicates the view is selected. |
state_active | boolean | false | Used by SlidingTab. This state indicates that the view is active. |
state_single | boolean | false | Used by views that contain lists of items. This state indicates that the view is showing only one item. |
state_first | boolean | false | Used by views that contain lists of items. This state indicates that the view is showing the first item. |
state_middle | boolean | false | Used by views that contain lists of items. This state indicates that the view is showing the middle item. |
state_last | boolean | false | Used by views that contain lists of items. This state indicates that the view is showing the last item. |
state_pressed | boolean | false | Used by views that contain lists of items. This state indicates that the view is pressed. |
Children
Element | Description |
---|---|
<[any drawable tag]> | Any drawable xml. Either this must be present or the 'drawable' attribute must be set. |
<[any drawable tag]>
Any drawable xml. Either this must be present or the 'drawable' attribute must be set.
<shape>
Basic method for drawing shapes via XML.
Attributes
Name | Type | Default | Description |
---|---|---|---|
visible | boolean | parent|true | Determines if drawable is visible. |
shape | enum (rectangle, oval, line, ring) | rectangle | Determines the shape: rectangle (shape is a rectangle, possibly with rounded corners); oval (shape is an ellipse); line (shape is a line); ring (shape is a ring). |
innerRadiusRatio | float | 3.0 | Only valid if shape == 'ring'. Inner radius of the ring expressed as a ratio of the ring's width. For instance, if innerRadiusRatio=3, then the inner radius equals the ring's width divided by 3. This value is ignored if innerRadius is defined. |
innerRadius | float | -1 | Only valid if shape == 'ring'. Inner radius of the ring. When defined, innerRadiusRatio is ignored. When undefined, innerRadiusRatio's default is used. |
thicknessRatio | float | 9.0 | Only valid if shape == 'ring'. Thickness of the ring expressed as a ratio of the ring's width. For instance, if thicknessRatio=9, then the thickness equals the ring's width divided by 9. This value is ignored if thickness is defined. Default value is 9. |
thickness | float | -1 | Only valid if shape == 'ring'. Thickness of the ring. When defined, thicknessRatio is ignored. When undefined, thicknessRatio's default is used. |
useLevel | boolean | true | Only valid if shape == 'ring'. Allows one to draw only part of the ring (arc-wise), by modifying the drawable's level. This setting only makes sense in context of a <level-list> (LevelListDrawable). |
Children
Element | Description |
---|---|
<size> | Determines the size of the shape. |
<gradient> | Adds a background gradient to the shape. |
<solid> | Adds a solid background color to the shape. Overides gradient element. |
<stroke> | Adds a border to the shape. |
<corners> | Adds rounded corners to the shape. |
<padding> | The padding for the content within this drawable. (Does not pad graphics in any way.) |
<size>
Determines the size of the shape.
Attributes
Name | Type | Default | Description |
---|---|---|---|
width | dimension | -1 | Width of the shape. |
height | dimension | -1 | Height of the shape. |
<gradient>
Adds a background gradient to the shape.
Attributes
Name | Type | Default | Description |
---|---|---|---|
startColor | color | 0 | The color at the start of the gradient. |
centerColor | color | 0 | The color in the center of the gradient. Optional; if not included, there is no center color. |
endColor | color | 0 | The color at the end of the gradient. |
type | enum (linear, radial, sweep) | linear | Determines the type of gradient. |
centerX | float|fraction | .5 | Determines the location of the centerColor. Ranges from 0 to 1. Ignored if centerColor is undefined. |
centerY | float|fraction | .5 | Determines the location of the centerColor. Ranges from 0 to 1. Ignored if centerColor is undefined. |
angle | float | 0 | Only valid if type == 'linear'. Determines the angle of a linear gradient. Must be a multiple of 45 degrees. |
gradientRadius | float|fraction | N/A | Only valid if type == 'radial' or 'sweep'. Required if type == 'radial'. Determines the radius of the gradient. |
useLevel | boolean | false | Determines the amount of the gradient to be drawn, based on the level of the shape. Affects all three gradient types. |
<solid>
Adds a solid background color to the shape. Overides gradient element.
Attributes
Name | Type | Default | Description |
---|---|---|---|
color | color | 0 | The color of the background. |
<stroke>
Adds a border to the shape.
Attributes
Name | Type | Default | Description |
---|---|---|---|
width | dimension | 0 | The width of the stroke. |
color | color | 0 | The color of the stroke. |
dashWidth | dimension | 0 | The width of each dash. Ignored unless dashGap is also defined. |
dashGap | dimension | 0 | The width of gaps between eahc dash. Ignored unless dashWidth is also defined. |
<corners>
Adds rounded corners to the shape.
Attributes
Name | Type | Default | Description |
---|---|---|---|
radius | dimension | 0 | The radius of every corner. |
topLeftRadius | dimension | radius | Determines the radius of the top left corner. Ignored unless radius for all corners is defined, either through 'radius' or the other corners' attributes. |
topRightRadius | dimension | radius | Determines the radius of the top right corner. Ignored unless radius for all corners is defined, either through 'radius' or the other corners' attributes. |
bottomLeftRadius | dimension | radius | Determines the radius of the bottom left corner (buggy; is actually bottom right corner). Ignored unless radius for all corners is defined, either through 'radius' or the other corners' attributes. |
bottomRightRadius | dimension | radius | Determines the radius of the bottom right corner (buggy; is actually bottom left corner). Ignored unless radius for all corners is defined, either through 'radius' or the other corners' attributes. |
<padding>
The padding for the content within this drawable. (Does not pad graphics in any way.)
Attributes
Name | Type | Default | Description |
---|---|---|---|
left | dimension | 0 | Left padding. |
top | dimension | 0 | Top padding. |
right | dimension | 0 | Right padding. |
bottom | dimension | 0 | Bottom padding. |
<transition>
An extension of <layer-list> that is intended to cross-fade between the first and second layer. The format is exactly the same as <layer-list>.
附上一份中文版的:
Drawable资源是对图像的一个抽象,你可以通过getDrawable(int)得到并绘制到屏幕上。这里有几种不同类型的Drawable:
Bitmap File
一个Bitmap图像文件(.png、.jpg或.gif)。BitmapDrawable。
Nine-Patch File
一个带有伸缩区域的PNG文件,可以基于content伸缩图片(.9.png)。NinePatchDrawable。
State List
一个XML文件,为不同的状态引用不同的Bitmap图像(例如,当按钮按下时使用不同的图片)。StateListDrawable。
Color
定义在XML中的资源,指定一个矩形(圆角可以有)的颜色。PaintDrawable。
Shape
一个XML文件,定义了一个几何形状,包括颜色和渐变。ShapeDrawable。
AnimationDrawable资源的说明在【Animation资源】文章中。
Bitmap File
基本的Bitmap图像。Android支持几种不同格式的Bitmap文件:.png(最佳)、.jpg(可接受)、.gif(不要)。
注意:Bitmap文件可能会被aapt工具进行无损图像压缩优化。例如,一个真彩色的PNG(不超过256色)可能会被转换成一个带有颜色板的8位PNG。这样做能保证图片质量一样,但减少内存占用。因此,需要了解的是放在这个文件夹下的二进制图像在编译时可能会发生变更。如果你打算以位流方式读取图像来转化成Bitmap的话,可以把它们放到res/raw文件中,在这里,它们不会被优化。
File Location:
res/drawable/filename.png (.png, .jpg, 或.gif)
文件名会被当作资源ID使用。
Complied Resource Datatype:
指向BitmapDrawable的资源指针。
Resource Reference:
R.drawable.filename(Java)
@[package:]drawable/filename(XML)
Example:
在res/drawable/myimage.png位置保存了一张图片,在Layout XML中可以应用这个图片到一个View上:
<ImageViewandroid:layout_height="wrap_content"android:layout_width="wrap_content"android:src="@drawable/myimage" />
下面的代码可以以 Drawable方式得到图片:
Resources res = getResources();Drawable drawable = res.getDrawable(R.drawable.myimage);Nine-Patch File
NinePatch是一种PNG图像,可以定义拉伸区域,当View的content超出图像边界的话,Android会拉伸它。典型用法是把这个图像设置为View的背景,而这个View至少有一个尺寸设置为“wrap_content”,当这个View变大来容纳content时,Nine-Patch图像也会拉伸来匹配View的大小。
File Location:
res/drawable/filename.9.png
文件名将被当作资源ID使用。
Complied Resource Datatype:
指向NinePatchDrawable的资源指针。
Resource Reference:
R.drawable.filename(Java)
@[package:]drawable/filename(XML)
Example:
在res/drawable/myninepatch.9.png位置保存了一张图片,在Layout XML中可以应用这个图片到一个View上:
<Buttonandroid:layout_height="wrap_content"android:layout_width="wrap_content"android:background="@drawable/myninepatch" />
State List
StateListDrawable是定义在XML中的Drawable对象,能根据状态来呈现不同的图像。例如,Button存在多种不同的状态(pressed、focused或other),使用StateListDrawable,你可以为Button的每个状态提供不同的按钮图像。
你可以在XML文件中描述状态列表。在<selector>元素里的每个<item>代表每个图像。每个<item>使用不同的特性来描述使用的时机。
当每次状态改变时,StateList都会从上到下遍历一次,第一个匹配当前状态的item将被使用——选择的过程不是基于“最佳匹配”,只是符合state的最低标准的第一个item。
File Location
Res/drawable/filename.xml
文件名将被当作资源ID使用。
Complied Resource Datatype:
指向StateListDrawable的资源指针。
Resource Reference:
R.drawable.filename(Java)
@[package:]drawable/filename(XML)
Syntax:
<?xml version="1.0" encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"android:constantSize=["true" |"false"]android:dither=["true" |"false"]android:variablePadding=["true" |"false"] ><itemandroid:drawable="@[package:]drawable/drawable_resource"android:state_pressed=["true" |"false"]android:state_focused=["true" |"false"]android:state_selected=["true" |"false"]android:state_active=["true" |"false"]android:state_checkable=["true" |"false"]android:state_checked=["true" |"false"]android:state_enabled=["true" |"false"]android:state_window_focused=["true" |"false"] /></selector>
Elements:
<selector>
必须。必须是根元素。可以包含一个或多个<item>元素。
Attributes:
xmlns:android
String。必须。定义XML的命名空间,必须是
“http://schemas.android.com/apk/res/android”。
android:constantSize
Boolean。“true”表示随着状态变化,Drawable的大小保持不变(所有状态中最大的size);“false”表示大小会变化。默认是false。
android:dither
Boolean。“true”表示当Bitmap和屏幕的不是相同的像素设定时支持Bitmap抖动(例如,ARGB 8888的Bitmap和RGB 565的屏幕);“false”表示不支持。默认是“true”。
android:variablePadding
Boolean。“true”表示Drawable的Padding可以变化;“false”表示Padding保持相同(所有状态的最大Padding)。使能这一特征需要在状态变化时处理Layout,一般都不支持。默认值是false。
<item>
定义特定状态的Drawable,通过它的特性指定。必须是<selector>的子元素。
Attributes:
android:drawable
Drawable资源。必须。指向一个Drawable资源。
android:state_pressed
Boolean。“true”表示按下状态使用(例如按钮按下);“false”表示非按下状态使用。
android:state_focused
Boolean。“true”表示聚焦状态使用(例如使用滚动球/D-pad聚焦Button);“false”表示非聚焦状态使用。
android:state_selected
Boolean。“true”表示选中状态使用(例如Tab打开);“false”表示非选中状态使用。
android:state_checkable
Boolean。“true”表示可勾选状态时使用;“false”表示非可勾选状态使用。(只对能切换可勾选—非可勾选的构件有用。)
android:state_checked
Boolean。“true”表示勾选状态使用;“false”表示非勾选状态使用。
android:state_enabled
Boolean。“true”表示可用状态使用(能接收触摸/点击事件);“false”表示不可用状态使用。
android:window_focused
Boolean。“true”表示应用程序窗口有焦点时使用(应用程序在前台);“false”表示无焦点时使用(例如Notification栏拉下或对话框显示)。
注意:记住一点,StateList中第一个匹配当前状态的item会被使用。因此,如果第一个item没有任何状态特性的话,那么它将每次都被使用,这也是为什么默认的值必须总是在最后(如下面的例子所示)。
Example:
XML文件保存在res/drawable/button.xml。
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_pressed="true"android:drawable="@drawable/button_pressed"/> <!-- pressed --><item android:state_focused="true"android:drawable="@drawable/button_focused"/> <!-- focused --><item android:drawable="@drawable/button_normal"/> <!-- default --></selector>
Layout XML将这个Drawable应用到一个View上:
<ImageViewandroid:layout_height="wrap_content"android:layout_width="wrap_content"android:src="@drawable/button"/>
Color
定义在XML中的color,可以当作Drawable使用,来填充矩形区域(圆角可以有)。这种Drawable的行为很像是颜色填充。
注意:Color Drawable是一种简单的资源,可以使用name特性来引用其值(不再是XML文件的名)。因此,你可以在一个XML文件中的<resources>元素下添加多个Color Drawable。
File Location:
res/drawable/filename.xml
文件名随意。元素的name将会当作资源ID使用。
Complied Resource Datatype:
指向PaintDrawable资源的指针。
Resource Reference:
R.drawable.color_name(Java)
@[package:]drawable/color_name(XML)
Syntax:
<?xml version="1.0" encoding="utf-8"?><resources><drawablename="color_name">color</drawable></resources>
Elements:
<resources>
必须。必须是根节点。
没有特性。
<drawable>
一个color Drawable。其值可以是任何有效的十六进制颜色值或者Color资源。Color值总是以“#”开头,后面紧跟Alpha-Red-Green-Blue信息,格式是:#RGB、#ARGB或者#AARRGGBB。
Attributes:
name
String。必须。Color的名字。这个名字将被当作资源ID使用。
Example:
XML文件保存在res/drawable/color.xml。
<?xml version="1.0" encoding="utf-8"?><resources><drawable name="solid_red">#f00</drawable><drawable name="solid_blue">#0000ff</drawable></resources>
Layout XML将会把这个Color Drawable应用到一个View上:
<TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:background="@drawable/solid_blue"/>
代码中获取Color Drawable并应用到View上:
Resources res =getResources();Drawable redDrawable = res.getDrawable(R.drawable.solid_red);TextView tv =(TextView) findViewByID(R.id.text);tv.setBackground(redDrawable);
Shape
定义在XML中的几何形状。
File Location:
res/drawable/filename.xml
文件名将被当作资源ID使用。
Complied Resource Datatype:
指向ShapeDrawable的资源指针。
Resource Reference:
R.drawable.filename(Java)
@[package:]drawable/filename(XML)
Syntax:
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape=["rectangle" |"oval" | "line" |"ring"] ><gradientandroid: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:usesLevel=["true" |"false"] /><solidandroid:color="color"/><strokeandroid:width="integer"android:color="color"android:dashWidth="integer"android:dashGap="integer"/><paddingandroid:left="integer"android:top="integer"android:right="integer"android:bottom="integer"/><cornersandroid:radius="integer"android:topLeftRadius="integer"android:topRightRadius="integer"android:bottomLeftRadius="integer"android:bottomRightRadius="integer"/></shape>
Elements:
<shape>
必须。必须是根元素。
Attributes:
android:shape
Keyword。定义Shape的类型。有效的值包括:
Value | Desciption |
| 矩形。默认形状。 |
| 椭圆。 |
| 水平直线。需要<stroke>元素定义线的宽度。 |
| 环形。 |
接下来的特性只能在android:shape=”ring”时使用:
android:innerRadius
Dimension。内环的半径。
android:innerRadiusRatio
Float。以环的宽度比率来表示内环的半径。例如,如果android:innerRadiusRatio=”5”,内环半径等于环的宽度除以5。这个值可以被android:innerRadius覆盖。默认值是9。
android:thickness
Dimension。环的厚度。
android:thicknessRatio
Float。以环的宽度比率来表示环的厚度。例如,如果android:thicknessRatio=”2”,厚度就等于环的宽度除以2。这个值可以被android:thickness覆盖。默认值是3。
android:useLevel
Boolean。“true”表示可以当作LevelListDrawable使用。一般都为“false”。
<gradient>
为Shape指定渐变色。
Attributes:
android:angle
Integer。渐变色的角度值。0表示从左到右,90表示从下到上。必须是45的倍数,默认是0。
android:centerX
Float。渐变色中心的X相对位置(0-1.0)。当android:type=”linear”时无效。
android:centerY
Float。渐变色中心的Y相对位置(0-1.0)。当android:type=”linear”时无效。
android:centerColor
Color。可选的颜色,出现在start和end颜色之间。
android:endColor
Color。end颜色。
android:gradientRadius
Float。渐变色的半径。当android:type=”radial”时有效。
android:startColor
Color。start颜色。
android:type
Keyword。渐变色的样式。有效值为:
Value | Description |
| 线性渐变,默认值。 |
| 环形渐变。start颜色是处于中间的颜色。 |
| sweep渐变 |
android:useLevel
Boolean。“true”表示可以当作LevelListDrawable使用。
<solid>
填充shape的单一色。
Attributes:
android:color
Color。这个颜色会应用到shape上。
<stroke>
shape的线形。
Attributes:
android:width
Dimension。线的厚度。
android:color
Color。线的颜色。
android:dashGap
Dimension。间断线间的距离。仅在android:dashWidth设定时有效。
android:dashWidth
Dimension。间断线的大小。仅在android:dashGap设定时有效。
<padding>
内部View元素的边距。
Attributes:
android:left
Dimension。左内边距。
android:top
Dimension。上内边距。
android:right
Dimension。右内边距。
android:bottom
Dimension。下内边距。
<corners>
为shape创建圆角。当shape是一个矩形时有效。
Attributes:
android:radius
Dimension。圆角的半径。会被下面的特性覆盖。
android:topLeftRadius
Dimension。左上圆角半径。
android:topRightRadius
Dimension。右上圆角半径。
android:bottomLeftRadius
Dimension。左下圆角半径。
android:bottomRightRadius
Dimension。右下圆角半径。
Examples:
XML文件保存在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"><gradientandroid: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>
Layout XML将被当作ShapeDrawable应用到一个View上:
<TextViewandroid:background="@drawable/gradient_box"android:layout_height="wrap_content"android:layout_width="wrap_content"/>
代码中也可以获得ShapeDrawable,然后应用到View上:
Resources res =getResources();Drawable shape = res. getDrawable(R.drawable.gradient_box);TextView tv =(TextView)findViewByID(R.id.textview);tv.setBackground(shape);
参考:http://idunnolol.com/android/drawables.html
http://www.cnblogs.com/xirihanlin/archive/2010/06/14/1758145.html
更多相关文章
- 箭头函数的基础使用
- NPM 和webpack 的基础使用
- Python list sort方法的具体使用
- 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
- android 代码混淆总结
- Android(安卓)- 插件使用整理 Android(安卓)Styler
- android注解框架ButterKnife详细使用文档(v7.0.1)
- 关于Android中是否可以使用全局变量的问题
- Android(安卓)sqlite例子 有外键的使用