Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 基礎shape

Android 基礎shape

編輯:關於Android編程

介紹

簡單來說,shape就是用來在xml文件中定義形狀,代碼解析之後就可以當做Drawable一樣使用

官方說明

關於shape定義的drawable

文件位置:res/drawable/filename.xml

編譯資源類型:GradientDrawable

文件引用:
InJava:R.drawable.filename
In XML:@[package:]drawable/filename

語法:


<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 xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    
    <solid android:color="@color/colorAccent" />

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    
    <solid android:color="@color/colorAccent" />

    
    
    
    <stroke
        android:width="3dp"
        android:color="@color/colorPrimaryDark"
        android:dashGap="0dp"
        android:dashWidth="10dp" />

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    
    <solid android:color="@color/colorAccent" />

    
    
    
    <stroke
        android:width="3dp"
        android:color="@color/colorPrimaryDark"
        android:dashGap="4dp"
        android:dashWidth="10dp" />
    
    
    
    
    
    
    <corners
        android:bottomLeftRadius="60dp"
        android:radius="30dp"
        android:topRightRadius="120dp" />

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    
    
    
    <stroke
        android:width="3dp"
        android:color="@color/colorPrimaryDark"
        android:dashGap="4dp"
        android:dashWidth="10dp" />
    
    
    
    
    
    
    <corners
        android:bottomLeftRadius="60dp"
        android:radius="30dp"
        android:topRightRadius="120dp" />
    
    <gradient
        android:angle="45"
        android:centerColor="@color/stone"
        android:endColor="@color/pink"
        android:startColor="@color/yellow" />


<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<gradient
    android:centerColor="@color/pink"
    android:endColor="@color/yellow"
    android:startColor="@color/colorPrimary" />
<size
    android:width="400dp"
    android:height="400dp" />

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<gradient
    android:angle="90"
    android:centerColor="@color/pink"
    android:endColor="@color/yellow"
    android:startColor="@color/colorPrimary" />

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    <stroke
        android:width="9dp"
        android:color="@color/pink" />

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:innerRadius="100dp"
    android:thickness="50dp"
    android:useLevel="false"
    >
    <gradient android:startColor="@color/colorAccent"
        android:endColor="@color/yellow"
        android:centerColor="@color/pink"/>