Android Shape Drawable 说明

都是用工具翻译的,不对的地方恳请大家指出来。

Shape Drawable原版


This is a generic shape defined in XML.

file location:
res/drawable/filename.xml
The filename is used as the resource ID.
compiled resource datatype:
Resource pointer to a GradientDrawable.
resource reference:
In Java: R.drawable.filename
In XML: @[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:
<shape>
The shape drawable. This must be the root element.

attributes:

xmlns:android
String. Required. Defines the XML namespace, which must be "http://schemas.android.com/apk/res/android".
android:shape
Keyword. Defines the type of shape. Valid values are:
ValueDesciption
"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<stroke> element to define the width of the line.
"ring"A ring shape.

The following attributes are used only when android:shape="ring":

android:innerRadius
Dimension. The radius for theinner part of the ring (the hole in the middle), as a dimension value or dimension resource.
android:innerRadiusRatio
Float. The radius for the innerpart of the ring, expressed as a ratio of the ring's width. For instance, if android:innerRadiusRatio="5", then the inner radius equals the ring's width divided by 5. Thisvalue is overridden by android:innerRadius. Default value is 9.
android:thickness
Dimension. The thickness of thering, as a dimension value or dimension resource.
android:thicknessRatio
Float. The thickness of the ring,expressed as a ratio of the ring's width. For instance, if android:thicknessRatio="2", thenthe thickness equals the ring's width divided by 2. This value is overridden by android:innerRadius. Default value is 3.
android:useLevel
Boolean. "true" if this is used asa LevelListDrawable. This should normally be "false" or your shape may not appear.
<corners>
Creates rounded corners for the shape. Applies only when the shape is a rectangle.

attributes:

android:radius
Dimension. The radius for all corners, as a dimension value or dimension resource. This is overridden for eachcorner by the following attributes.
android:topLeftRadius
Dimension. The radius for the top-left corner, as a dimension value or dimension resource.
android:topRightRadius
Dimension. The radius for the top-right corner, as a dimension value or dimension resource.
android:bottomLeftRadius
Dimension. The radius for the bottom-left corner, as a dimension value or dimension resource.
android:bottomRightRadius
Dimension. The radius for the bottom-right corner, as a dimension value or dimension resource.

Note: Every corner must (initially) be provided a cornerradius greater than 1, or else no corners are rounded. If you want specific cornerstonot be rounded, a work-around is to useandroid:radius 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.

<gradient>
Specifies a gradient color for the shape.

attributes:

android:angle
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.
android:centerX
Float. The relative X-position for the center of the gradient (0 - 1.0).
android:centerY
Float. The relative Y-position for the center of the gradient (0 - 1.0).
android:centerColor
Color. Optional color that comes between the start and end colors, as ahexadecimal value or color resource.
android:endColor
Color. The ending color, as a hexadecimalvalue or color resource.
android:gradientRadius
Float. The radius for the gradient. Only applied when android:type="radial".
android:startColor
Color. The starting color, as a hexadecimalvalue or color resource.
android:type
Keyword. The type of gradient pattern to apply. Valid values are:
ValueDescription
"linear"A linear gradient. This is the default.
"radial"A radial gradient. The start color is the center color.
"sweep"A sweeping line gradient.
android:useLevel
Boolean. "true" if this is used as a LevelListDrawable.
<padding>
Padding to apply to the containing View element (this pads the position of the Viewcontent, not the shape).

attributes:

android:left
Dimension. Left padding, as a dimension value or dimension resource.
android:top
Dimension. Top padding, as a dimension value or dimension resource.
android:right
Dimension. Right padding, as a dimension value or dimension resource.
android:bottom
Dimension. Bottom padding, as a dimension value or dimension resource.
<size>
The size of the shape.

attributes:

android:height
Dimension. The height of the shape, as a dimension value or dimension resource.
android:width
Dimension. The width of the shape, as a dimension value or dimension resource.

Note: The shape scales to the size of the containerView proportionate to the dimensions defined here, by default. When you use the shape in anImageView, you can restrict scaling by setting theandroid:scaleType to"center".

<solid>
A solid color to fill the shape.

attributes:

android:color
Color. The color to apply to the shape, as a hexadecimalvalue or color resource.
<stroke>
A stroke line for the shape.

attributes:

android:width
Dimension. The thickness of the line, as a dimension value or dimension resource.
android:color
Color. The color of the line, as ahexadecimal value or color resource.
android:dashGap
Dimension. The distance between line dashes, as a dimension value or dimension resource. Only valid if android:dashWidth is set.
android:dashWidth
Dimension. The size of each dash line, as a dimension value or dimension resource. Only valid if android:dashGap is set.
example:
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);
see also:

Shape Drawable翻译版


这是一个在xml中定义的一般形状。

文件位置:
res/drawable/filename.xml
文件名会作为资源的ID来使用。
资源数据类型:
可以参考 GradientDrawable
资源引用:
In Java: R.drawable.filename
In XML: @[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必须作为根节点。

shape的属性:

xmlns:android
String字符类型。必须定义xml的命名空间, 必须是 "http://schemas.android.com/apk/res/android"
android:shape
枚举关键字。 定义shape的形状。有效参数是:
ValueDesciption
"rectangle"填充包含视图的矩形。shape属性值默认矩形(rectangle)。
"oval"适合包含视图的尺寸的椭圆形
"line"一条水平线,跨越包含视图的宽度。这种形状需要通过<stroke>元素来定义线的宽度。
"ring"环形。

以下属性只有在 android:shape="ring" 的情况下使用:

android:innerRadius
Dimension 类型。 设置环形里面的圆的半径 (中间的圆/洞),设置一个具体尺寸 值或者参考 dimension resource
android:innerRadiusRatio
Float 类型. 设置环形里面的圆的半径, 用环形宽度的比例来表示。比如, 如果设置 android:innerRadiusRatio="5", 那么里面圆的半径为环形的宽度除以5 。这个属性会被 android:innerRadius属性所覆盖。默认值为9。
android:thickness
Dimension 类型。环形的厚度,设置具体尺寸值或者参考 dimension resource
android:thicknessRatio
Float 类型。环形的厚度,用环形的宽度的比例来表示。比如: 如果设置 android:thicknessRatio="2",那么里面圆的厚度为环形的宽度除以2。 这个属性会被 android:innerRadius属性所覆盖。默认值为3。
android:useLevel
Boolean 类型。如果被用来做 LevelListDrawable那么为“true”。这个通常为false或者你的形状不可见。
<corners>
为你的形状设置圆角。只有形状为矩形的时候才能用。

属性

android:radius
Dimension类型。 设置四边都为圆角, 尺寸值或者参考 dimension resource.。这个属性会被以下属性覆盖。
android:topLeftRadius
Dimension类型。 设置左上角为圆角, 尺寸值或者参考 dimension resource
android:topRightRadius
Dimension类型。 设置右上角为圆角, 尺寸值或者参考 dimension resource
android:bottomLeftRadius
Dimension类型。设置左下角为圆角, 尺寸值或者参考 dimension resource
android:bottomRightRadius
Dimension类型。设置右下角为圆角, 尺寸值或者参考 dimension resource

注意: 每个角设置的值必须大于1,否则不会有圆角效果。 如果你想设定某个特定的角不圆滑,那么用 android:radius 属性设置一个大于1的默认值,不过会被你设置的每个角覆盖, 如果你不希望设置圆角则可以设置为0(0dp)

<gradient>
为形状指定一个渐变的颜色.

属性:

android:angle
Integer类型。渐变的角度,用角度表示。 0 从左到右 90 从下到上。必须是45的倍数。默认值为0。
android:centerX
Float类型相对X位置为渐变的中心 (0 - 1.0).
android:centerY
Float类型相对Y位置为渐变的中心(0 - 1.0).
android:centerColor
Color类型。 设置从开始到结束之间的颜色,16进制值或者参考 color resource.
android:endColor
Color类型。 结束的颜色,16进制值或者参考  color resource.
android:gradientRadius
Float类型。 渐变的半径,只有当 android:type="radial"的时候才能用。
android:startColor
Color类型。 开始颜色, 16进制值或者参考  color resource.
android:type
枚举关键字类型。 渐变模型方式, 有效参数值为:
ValueDescription
"linear"线性渐变,默认值
"radial"发散渐变。开始颜色为过程颜色
"sweep"流线渐变。
android:useLevel
Boolean类型。 如果被用作 LevelListDrawable则设在"true"。
<padding>
包含视图的内边距(视图的填充位置,不是形状)。

属性:

android:left
Dimension类型。左内边距,尺寸值或者参考 dimension resource.
android:top
Dimension类型。 上内边距, 尺寸值或者参考 dimension resource.
android:right
Dimension类型。 右内边距,尺寸值或者参考 dimension resource.
android:bottom
Dimension类型。下内边距, 尺寸值或者参考 dimension resource.
<size>
形状的尺寸。

属性:

android:height
Dimension类型。形状的高度,尺寸值或者参考 dimension resource.
android:width
Dimension类型。 形状的宽度,尺寸值或者参考 dimension resource.

注意: 形状默认会等比例缩放到容器的尺寸。当你图片控件用到了shape形状,你可设置android:scaleType"center"来限制缩放。

<solid>
形状填充纯色。

属性:

android:color
Color类型。颜色值,16进制值或者参考 color resource.
<stroke>
为形状画线。

属性:

android:width
Dimension类型。 线的宽度, 尺寸值或者参考 dimension resource.
android:color
Color类型。线的颜色,16进制值或者参考 color resource.
android:dashGap
Dimension类型。行之间破折号间距,尺寸值或者参考 dimension resource. 只有属性android:dashWidth 设置有效值了才生效(即设置虚线)。
android:dashWidth
Dimension类型。虚线的尺寸, 尺寸值或者参考 dimension resource. 只有属性android: dashGap设置有效值了才生效。
例子:

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">
    <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>

Layout 的中的一个控件引用该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);
另请参阅:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值