Android支持自定義Shape, 以畫出需要的形狀,可以作為TextView, EditText, Button的背景drawable資源。Shape很簡單,就是一個XML文件,SDK文檔里描述其格式如下:
- 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:usesLevel=["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>
其支持的屬性沒有shadow, 做Web前端開發的同學寫CSS可以很方便地加一個shadow屬性值,如何給Android Shape加一個shadow,以得到類似的效果呢?
答案是使用layer-list ! 直接上代碼如下:
- xml version="1.0" encoding="utf-8"?>
- <layer-list xmlns:Android="http://schemas.android.com/apk/res/android">
-
- <item>
- <shape Android:shape="rectangle">
- <solid Android:color="#792a03" />
- <corners Android:radius="19dp" />
- shape>
- item>
-
- <item Android:top="1px">
- <shape Android:shape="rectangle">
- <gradient Android:startColor="#ffdb8f" android:endColor="#ffdb8f"
- Android:angle="270" />
- <padding Android:left="5dp" android:top="3dp" android:right="5dp"
- Android:bottom="3dp" />
- <corners Android:radius="20dp" />
- shape>
-
- item>
-
- layer-list>
將以上xml存成btn_test, 放到res/drawable/目錄下。 將該drawable xml設為一個TextView的backgroiund,
- <TextView
- Android:background="@drawable/btn_test"
-
- Android:layout_marginTop="20dip"
- Android:layout_marginLeft="5dip"
- Android:textColor="#792a03"
-
- Android:text="1天2小時14分20秒"
- Android:layout_width="wrap_content"
- Android:layout_height="wrap_content" />
其效果如下圖所示:
關於layer-list的進一步解釋見SDK文檔,如下:
Layer List
A LayerDrawable
is a drawable object that manages an array of other drawables. Each drawable in the list is drawn in the order of the list—the last drawable in the list is drawn on top.
Each drawable is represented by an
element inside a single
element.
- file location:
res/drawable/filename .xml
The filename is used as the resource ID. - compiled resource datatype:
- Resource pointer to a
LayerDrawable
. - resource reference:
- In Java:
R.drawable.filename
In XML: @[package :]drawable/filename
- syntax:
- xml version="1.0" encoding="utf-8"?>
- <layer-list
- xmlns:Android="http://schemas.android.com/apk/res/android" >
- <item
- Android:drawable="@[package:]drawable/drawable_resource"
- Android:id="@[+][package:]id/resource_name"
- Android:top="dimension"
- Android:right="dimension"
- Android:bottom="dimension"
- Android:left="dimension" />
- layer-list>
="http://schemas.android.com/apk/res/android" >
- elements:
-
- Required. This must be the root element. Contains one or more
elements. attributes:
xmlns:Android
- String . Required. Defines the XML namespace, which must be
"http://schemas.android.com/apk/res/android"
.
- Defines a drawable to place in the layer drawable, in a position defined by its attributes. Must be a child of a
element. Accepts child
elements. attributes:
Android:drawable
- Drawable resource . Required . Reference to a drawable resource.
android:id
- Resource ID . A unique resource ID for this drawable. To create a new resource ID for this item, use the form:
"@+id/name "
. The plus symbol indicates that this should be created as a new ID. You can use this identifier to retrieve and modify the drawable with View.findViewById()
or Activity.findViewById()
. android:top
- Integer . The top offset in pixels.
android:right
- Integer . The right offset in pixels.
android:bottom
- Integer . The bottom offset in pixels.
android:left
- Integer . The left offset in pixels.
All drawable items are scaled to fit the size of the containing View, by default. Thus, placing your images in a layer list at different positions might increase the size of the View and some images scale as appropriate. To avoid scaling items in the list, use a
element inside the
element to specify the drawable and define the gravity to something that does not scale, such as "center"
. For example, the following
defines an item that scales to fit its container View:
- <item Android:drawable="@drawable/image" />
To avoid scaling, the following example uses a
element with centered gravity:
- <item>
- <bitmap Android:src="@drawable/image"
- Android:gravity="center" />
- item>
- :src="@drawable/image" android:gravity="center" />
-
- example:
- XML file saved at
res/drawable/layers.xml
:
-
- xml version="1.0" encoding
------分隔線----------------------------
- 上一篇:App設計VS Web設計:互相學習到了什麼
- 下一篇:Windows環境利用VMware7.1.3 搭建iPhone開發環境