ShapeDrawable資源

Demo

MainActivity.java

public class MainActivity extends Activity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
   }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:id="@+id/btn"
        android:layout_centerInParent="true"
        android:textSize="16sp"
        android:background="@drawable/btn_bg"
        android:textColor="#fff"
        android:text="點擊" />
</RelativeLayout>

res/drawable/btn_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners
        android:radius="10dp"></corners>
    <solid
        android:color="#07B226"></solid>
    <size
        android:height="50dp"
        android:width="100dp"></size>
</shape>

運行

這裏寫圖片描述

Button的背景變成了帶圓角的長方形。

ShapeDrawable資源

資源路徑

res/drawable/filename.xml

資源的使用

在代碼中使用:R.drawable.filename

在XML中使用:@[package:]drawable/filename


語法

<?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>
根標籤。

屬性

android:shape
值爲關鍵字,表示不同的形狀。

關鍵字 關鍵字描述
“Computer” 生成的視圖爲矩形,不設置android:shape屬性默認爲矩形
“oval” 生成的視圖爲橢圓形狀
“line” 生成的視圖爲線性,需要<stroke>標籤定義線寬。
“ring” 生成的視圖爲環形

(僅當 android:shape=”ring” 時才使用以下屬性)

android:innerRadius
值爲尺寸,環內部(中間的孔)的半徑,以尺寸值或尺寸資源表示。

android:innerRadiusRatio
值爲浮點型,環內部的半徑,以環寬度的比率表示。

android:thickness
值爲浮點型。環的厚度,表示爲環寬度的比率。例如,如果 android:thicknessRatio=”2”,則厚度等於環寬度除以 2。此值被 android:innerRadius 覆蓋。默認值爲 3。

android:useLevel
值爲布爾值,如果這用作 LevelListDrawable,則此值爲“true”。這通常應爲“false”,否則形狀不會顯示。


<corners>
爲形狀產生圓角。僅當形狀爲矩形時適用。

屬性

android:radius
值爲尺寸。所有角的半徑,以尺寸值或尺寸資源表示。對於每個角,這會被以下屬性覆蓋。

android:topLeftRadius
值爲尺寸。左上角的半徑,以尺寸值或尺寸資源表示。

android:topRightRadius
值爲尺寸。右上角的半徑,以尺寸值或尺寸資源表示。

android:bottomLeftRadius
值爲尺寸。左下角的半徑,以尺寸值或尺寸資源表示。

android:bottomRightRadius
值爲尺寸。右下角的半徑,以尺寸值或尺寸資源表示。


<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 時有效。

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