日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

ListView頁眉頁腳效果VS android背景漸變

系統 2780 0
大家都知道,在我們調用ListView的addFooterView()方法給List增加一個頁腳時,如果列表內容很多,超過了屏幕大小,那么頁腳就看不到了,可我們一般想要的效果是如下圖所示的,在ListView的內容超過屏幕時,頁腳還在屏幕的底部。

ListView頁眉頁腳效果VS android背景漸變

本文將介紹上圖所示的ListView效果,同時介紹一下在android中如何實現漸變效果,就像上圖中的頁眉頁腳的背景色一樣。

實現上面的效果主要使用幾個RelativeLayout標簽和ListView組合即可,代碼如下所示:
    
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<RelativeLayout android:id="@+id/listHeader"
		android:background="@drawable/jbshape" android:layout_alignParentTop="true"
		android:gravity="center_horizontal" android:layout_width="fill_parent"
		android:layout_height="wrap_content">
		<TextView android:text="IdeasAndroid 列表演示" android:textColor="#000000"
			android:textSize="18dip" android:layout_width="wrap_content"
			android:layout_height="wrap_content"></TextView>
	</RelativeLayout>
	<RelativeLayout android:id="@+id/listFooter"
		android:background="@drawable/jbshape" android:gravity="center_horizontal"
		android:layout_alignParentBottom="true" android:layout_width="fill_parent"
		android:layout_height="wrap_content">
		<Button android:id="@+id/prePage" android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:text="上一頁"
			android:layout_alignParentLeft="true"></Button>
		<Button android:layout_width="wrap_content"
			android:layout_gravity="right" android:layout_height="wrap_content"
			android:text="下一頁" android:layout_toRightOf="@id/prePage"></Button>
	</RelativeLayout>
	<ListView android:id="@+id/myListView" android:layout_width="fill_parent"
		android:layout_height="fill_parent" android:layout_below="@id/listHeader"
		android:layout_above="@id/listFooter">
	</ListView>
</RelativeLayout>

  

幾個關鍵點:
1、在頁眉(id為listHeader)使用屬性android:layout_alignParentTop=”true”
聲明頁眉部分與父視圖的頂部對齊。
2、在頁腳(id為listFooter)使用屬性android:layout_alignParentBottom=”true” 聲明其與父視圖的底部對齊。
3、在 ListView中使用屬性android:layout_below=”@id/listHeader” android:layout_above=”@id/listFooter” 聲明ListView位于listHeader的下方,位于listFooter的上方。

這樣我們的頁眉頁腳效果就實現了。

再來看看是怎么實現漸變的?

我們在res/drawable目錄下新建一個叫jbshape.xml的文件,內容如下所示:
    
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
	android:shape="rectangle">
	<gradient android:startColor="#509245" android:centerColor="#3e8532"
		android:endColor="#509245" android:type="linear" android:angle="90"
		android:centerX="0.5" android:centerY="0.5" />
	<padding android:left="7dp" android:top="7dp" android:right="7dp"
		android:bottom="7dp" />
	<corners android:radius="4dp" />
</shape>

  

這里就不多講了,相信你一看就能看懂,android:shape 配置的是圖形的形式,主要包括方形、圓形等,本例中為方形。gradient節點主要配置起點顏色、終點顏色、中間點的坐標、中間點的顏色、漸變角度(90度為上下漸變,0為左右漸變),padding節點主要配置上下左右邊距,corners節點配置四周園角的半徑。更詳細的配置參見http://www.ideasandroid.com/android/sdk/docs/guide/topics/resources/drawable-resource.html 。

使用漸變就更簡單了,如第一部分代碼中所示的,直接用android:background=”@drawable/jbshape” 配置背景為剛才配置的漸變圖形即可。

好了,本文就介紹到這。如果還有不明白的地方請下載下面的代碼,然后在本地測試一下就明白了。

代碼下載:http://www.ideasandroid.com/android/demo/HeaderBottomListDemo.rar

ListView頁眉頁腳效果VS android背景漸變


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 黄大仙区| 平定县| 聊城市| 徐州市| 汉中市| 启东市| 东方市| 翁牛特旗| 玛纳斯县| 莲花县| 大田县| 惠安县| 苍溪县| 泉州市| 老河口市| 柞水县| 石家庄市| 南阳市| 师宗县| 杭锦后旗| 淮阳县| 六枝特区| 无锡市| 灵石县| 晋州市| 永丰县| 宜章县| 兴业县| 旬阳县| 大新县| 济源市| 兰西县| 南汇区| 贡觉县| 神木县| 滕州市| 青冈县| 岗巴县| 呼和浩特市| 景德镇市| 辽阳县|