您的位置:首页 > 移动开发 > Android开发

Android中shape自定义形状遇到问题

2017-08-22 13:08 197 查看
可绘制对象资源

  可绘制对象资源是一般概念,是指可在屏幕上绘制的图形,以及可以使用 getDrawable(int) 等 API 检索或者应用到具有 android:drawable 和 android:icon 等属性的其他 XML 资源的图形。共有多种不同类型的可绘制对象;

  这里笔者要说的是形状可绘制对象,这是在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="float"
android:centerY="float"
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>
形状可绘制对象,必须是根元素

  属性:

    xmlns:android 字符串;必备,定义 XML 命名空间,且必须是”http://schemas.android.com/apk/res/android“。

    android:shape 关键字;定义形状的类型,有效值为:

      rectangle:填充包含视图的矩形,默认形状。

      oval:适应包含视图尺寸的椭圆形状。

      line:跨越包含视图宽度的水平线。此形状需要
<stroke>
元素定义线宽。

      ring:环形;

    仅当 android:shape=”ring” 如下时才使用以下属性:

    android:innerRadius 尺寸;环内部(中间的孔)的半径,以尺寸值或尺寸资源表示。

    android:innerRadiusRatio 浮点型;环内部的半径,以环宽度的比率表示。

    android:thickness 尺寸;环的厚度,以尺寸值或尺寸资源表示。

    android:thicknessRatio 浮点型;环的厚度,表示为环宽度的比率。

    android:useLevel 布尔值;如果这用作 LevelListDrawable,则此值为“true”。这通常应为“false”,否则形状不会显示。

<corners>
为形状产生圆角,仅当形状为矩形时适用

  属性:

    android:radius 尺寸;所有角的半径,以尺寸值或尺寸资源表示。对于每个角,这会被以下属性覆盖。

    android:topLeftRadius 尺寸;左上角的半径,以尺寸值或尺寸资源表示。

    android:topRightRadius 尺寸;右上角的半径,以尺寸值或尺寸资源表示。

    android:bottomLeftRadius 尺寸;左下角的半径,以尺寸值或尺寸资源表示。

    android:bottomRightRadius 尺寸;右下角的半径,以尺寸值或尺寸资源表示。

    注意:(最初)必须为每个角提供大于 1 的角半径,否则无法产生圆角。如果希望特定角不要倒圆角,解决方法是

    使用 android:radius 设置大于 1 的默认角半径,然后使用实际所需的值替换每个角,为不希望倒圆角的角提供零(“0dp”)。

<gradient>
指定形状的渐变颜色

  属性:

    android:angle 整型;渐变的角度(度)。0 为从左到右,90 为从上到上。必须是 45 的倍数。默认值为 0。

    android:centerX 浮点型;渐变中心的相对 X 轴位置 (0 - 1.0)。

    android:centerY 浮点型;渐变中心的相对 Y 轴位置 (0 - 1.0)。

    android:centerColor 颜色;起始颜色与结束颜色之间的可选颜色,以十六进制值或颜色资源表示。

    android:endColor 颜色;结束颜色,表示为十六进制值或颜色资源。

    android:gradientRadius 浮点型;渐变的半径。仅在 android:type=”radial” 时适用。

    android:startColor 颜色;起始颜色,表示为十六进制值或颜色资源。

    android:type 关键字;要应用的渐变图案的类型。

      有效值为

        linear:线性渐变;这是默认值;

        radial:径向渐变;起始颜色为中心颜色;

        sweep:流线型渐变;

    android:useLevel 布尔值;如果这用作 LevelListDrawable,则此值为“true”。

<padding>
要应用到包含视图元素的内边距(这会填充视图内容的位置,而非形状)

  属性:

    android:left 尺寸;左内边距,表示为尺寸值或尺寸资源;

    android:top 尺寸;上内边距,表示为尺寸值或尺寸资源;

    android:right 尺寸;右内边距,表示为尺寸值或尺寸资源;

    android:bottom 尺寸;下内边距,表示为尺寸值或尺寸资源;

<size>
形状的大小

  属性:

    android:height 尺寸;形状的高度,表示为尺寸值或尺寸资源;

    android:width 尺寸;形状的宽度,表示为尺寸值或尺寸资源;

<solid>
用于填充形状的纯色

  属性:

    android:color 颜色;应用于形状的颜色,以十六进制值或颜色资源表示;

<stroke>
形状的笔划中线

  属性:

    android:width 尺寸;线宽,以尺寸值或尺寸资源表示;

    android:color 颜色;线的颜色,表示为十六进制值或颜色资源;

    android:dashGap 尺寸;短划线的间距,以尺寸值或尺寸资源表示。仅在设置了 android:dashWidth 时有效;

    android:dashWidth 尺寸;每个短划线的大小,以尺寸值或尺寸资源表示。仅在设置了 android:dashGap 时有效;

  笔者代码所犯的错误如下

  

  reset_setup_back_ground.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<!--android:angle是渐变角度,必须为45的整数倍。-->
<!--android:angle="100" 这是错误的写法-->
<gradient
android:angle="45"
android:endColor="#ffffff"
android:startColor="#000000" />

<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
<corners android:radius="5dp" />
</shape>


  reset_setup_background.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/reset_setup_back_ground" />
<item android:drawable="@android:color/transparent" />

</selector>


<TextView
android:id="@+id/reset_setup_text_view"
android:background="@drawable/reset_setup_background"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="重新进入设置向导"
android:textSize="18sp"
android:textColor="#000000"/>


  错误信息如下: FATAL EXCEPTION: mainProcess: neu.edu.cn.mobilesafer, PID: 16032java.lang.RuntimeException: Unable to start activity ComponentInfo{neu.edu.cn.mobilesafer/neu.edu.cn.mobilesafer.activity.SetupOverActivity}: android.view.InflateException: Binary XML file line #65: Error inflating class TextView 

  

  ......省略

  

Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #6 tag requires ‘angle’ attribute to be a multiple of 45

  没有按照示例,也没有仔细看官方文档 ,犯下这个小错误,不过看错误提示,还是很容易定位到,错误在哪里的,有错误不怕,慢慢定位,慢慢排除,还有就是,如果不是很确定,还是要认真看官方文档的,仔细看一下里面的要求;

参考文档:Google官方文档
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: