Android中res資源文件下除了定義基本的Layout佈局文件、存放適配圖片資源等功能外,還提供了定義menu菜單, Values主題,動畫,繪圖等功能。
使用這些功能很強大,可以減小代碼量與圖片資源。
這裏介紹下shape(畫圖)的使用,shape包含了corners gradient padding size solid stroke等屬性,繪製一個簡單的圖像還是很容易的。
step1:在Resource Type中選中Drawable ,再在Root Element中選中shape中建立名爲myshape的xml文件
step2:對myshape的屬性進行設定
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 圓角 -->
<corners android:radius ="8dp"/>
<!-- 漸變 -->
<gradient android:startColor ="#ffffff" android:endColor="#000000" android:angle ="45"/>
<!-- 邊距 -->
<padding
android:left= "10dp"
android:top= "10dp"
android:right= "10dp"
android:bottom= "10dp"/>
<!-- 大小 -->
<size
android:width= "800dp"
android:height= "300dp"/>
<!-- 填充 -->
<!-- <solid
android:color="#ff0000"/> -->
<!-- 描邊 -->
<stroke
android:width= "2dp"
android:color= "#00ff00"/>
</shape>
step3:在Layout佈局文件中引用
<RelativeLayout
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:background= "@drawable/shape_bak"/>
效果如下:
ctrlz present!