本文摘自:http://androiddada.iteye.com/blog/1238909
在看他人的代碼學習時,無意中發現的。很多應用中的ListView每個item的背景都是白色漸變的,感覺很有立體感.
之前我一直以爲是一張美工提供的背景圖,今天終於明白了,其實是可以在xml中定義出這種效果的!
先看看效果!
純白色背景:
漸變:
模擬器的效果不如真機好,不過你可以根據自己需求跳轉漸變範圍~
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#ffffff"
android:endColor="#EDEDED"
android:angle="-90" />
</shape>
和圖片一樣 放入drawable 配置好把它看成圖片調用就好了~
控件xml中 調用 android:background="@drawable/pull_to_refresh_header_background" 是不是很簡單 ~
---------------------------------------以上是原文,以下爲自己試驗的代碼-------------------------------------------------首先在res下新建目錄drawable,再新建xml文件,white_gradient.xml 代碼如下
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- android:shape="rectangle" 指形狀爲長方形 去掉好像沒什麼反應-->
<!-- 漸變 -->
<gradient
android:angle="-90"
android:endColor="#EDEDED"
android:startColor="#ffffff" />
<!-- 描邊 -->
<stroke
android:width="2dp"
android:color="#dcdcdc" />
<!-- 圓角 -->
<corners android:radius="5dp" />
<!-- 實心 -->
<!-- <solid android:color="#ff9d77"/> -->
<!-- 填充 不知道有什麼用...目測是給Button之類的用的 -->
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
然後在佈局中調用此屬性,代碼如下
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dip"
android:background="@drawable/white_gradient" >
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dip"
android:background="#ffffff" >
</LinearLayout>
效果如下, 上層爲自定義背景的佈局,夏天爲單純白色背景的佈局..