Overview
一個(gè)drawable資源是通常是一個(gè)可以被繪制在屏幕上的圖形描述,drawables被使用來(lái)描述形狀,顏色,邊界,漸變,等等。可以在Activity中的View中應(yīng)用。
典型的使用是用來(lái)定制特定的視圖或者上下文中顯示的視圖圖形,Drawables試圖在XML中定義并且可以被應(yīng)用在Java或者xml的View中
對(duì)于各個(gè)版本的Android默認(rèn)的Drawables,查看 androiddrawables site
Usage
Drawables 可以被用在各種不同的場(chǎng)合,但是下面5種是最重要,也是最需要掌握的
- Shape Drawables - 描述一個(gè)形狀屬性,列如stroke,fill,和padding。
- StateList Drawables - 為了不同情況使用的Drawables 列表。
- LayerList Drawables - 定義一個(gè)混合的Drawables組,混合成一個(gè)復(fù)雜的結(jié)果。
- NinePatch Drawables - 一個(gè)擁有伸縮區(qū)域的PNG文件,允許適當(dāng)?shù)恼{(diào)整大小。
- Vector Drawables - 定義復(fù)雜的基于XML的矢量圖像。
Drawing Shapes
Shape 可以通過(guò)以下的屬性描述:例如corners描述周圍,gradient描述背景,padding描述空間,solid描述背景顏色,stroke描述邊緣。
Solid Color Shapes
<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shap="rectangle">
<corners android:radius="4dp"/>
<stroke android:width="4dp" android:color="#C1E1A6" />
<solid android:color="#118C4E"/>
<padding android:left="20dp" android:top="20dp" android:right="20dp" android:bottom="20dp" />
</shape>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/solid_color_shape" android:textColor="#ffffff"
android:text="@string/hello_world" />