Android中shape自定義形狀遇到問題

可繪製對象資源
  可繪製對象資源是一般概念,是指可在屏幕上繪製的圖形,以及可以使用 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官方文檔

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章