Android 实现ListView圆角效果
今天,簡單講講如何實現(xiàn)使用
ListView顯示圓角。
其實代碼很多都可以解決,這是在網(wǎng)上搜索的一個解決的代碼。
無論是網(wǎng)站,還是APP,人們都愛看一些新穎的視圖效果。直角看多了,就想看看圓角,這幾年刮起了一陣陣的圓角設(shè)計風:CSS新標準納入圓角元素,特別是在iphone中幾乎隨處可見圓角設(shè)計,現(xiàn)在也開始出現(xiàn)很多圓角名片了。
現(xiàn)在就給大家實現(xiàn)一個圓角的ListView效果。 圓角的設(shè)計,我們并不追求到處都用,無處不用,android中有少數(shù)界面用直角確實容易顯得鋒利,和周邊界面太過對比而顯得不協(xié)調(diào),比如大欄目列表,設(shè)置等等,而采用圓角實現(xiàn),則會活潑,輕松的多,也融合的特別好。
??
先看下在IPhone中實現(xiàn)圓角效果的一個圖片:
具體實現(xiàn)???????????????????????????????????????????????????????????????????????????????
本文采用shape 方式實現(xiàn)。
l? 圓角背景list_corner_round_bg..xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"><!-- 漸變 --><gradient android:angle="180" android:endColor="#FFCCCCCC"android:startColor="@android:color/white" /><!-- 描邊 --><stroke android:width="1dp" android:color="@color/color_gray" /><!-- 實心填充 --><solid android:color="@color/color_white" /><!-- 圓角 --><corners android:bottomLeftRadius="8dip"android:bottomRightRadius="8dip" android:topLeftRadius="8dip"android:topRightRadius="8dip" /></shape>
l? List第一項背景 list_corner_round_top.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" ><!--漸變 <gradient android:startColor="#B5E7B8" android:endColor="#76D37B" android:angle="270"/>--><gradientandroid:angle="270"android:endColor="#40B9FF"android:startColor="#BFEEFF" /><cornersandroid:topLeftRadius="8dip"android:topRightRadius="8dip" /></shape>
l? List中間項背景list_corner_round_mid.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" ><!--漸變 <gradient android:startColor="#B5E7B8" android:endColor="#76D37B" android:angle="270"/>--><gradientandroid:angle="270"android:endColor="#40B9FF"android:startColor="#BFEEFF" /></shape>
l? List最后項背景 list_corner_round_bottom.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" ><!--漸變 <gradient android:startColor="#B5E7B8" android:endColor="#76D37B" android:angle="270"/>--><gradientandroid:angle="270"android:endColor="#40B9FF"android:startColor="#BFEEFF" /><cornersandroid:bottomLeftRadius="8dip"android:bottomRightRadius="8dip" /></shape>
l? List 只有一項的情況時的背景 list_corner_round.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" ><!--漸變 <gradient android:startColor="#B5E7B8" android:endColor="#76D37B" android:angle="270"/>--><gradientandroid:angle="270"android:endColor="#40B9FF"android:startColor="#BFEEFF" /><cornersandroid:bottomLeftRadius="8dip"android:bottomRightRadius="8dip"android:topLeftRadius="8dip"android:topRightRadius="8dip" /></shape>
l? 繼承ListView的CornerListView.lava
public class CornerListView extends ListView {public CornerListView(Context context) {super(context);}public CornerListView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}public CornerListView(Context context, AttributeSet attrs) {super(context, attrs); ;}@Overridepublic boolean onInterceptTouchEvent(MotionEvent ev) {switch (ev.getAction()) {case MotionEvent.ACTION_DOWN:int x = (int) ev.getX();int y = (int) ev.getY();int itemnum = pointToPosition(x, y);if (itemnum == 0) {if (itemnum == (getAdapter().getCount() - 1)) {// 只有一項setSelector(R.drawable.list_corner_round);} else {// 第一項setSelector(R.drawable.list_corner_round_top);}} else if (itemnum == (getAdapter().getCount() - 1))// 最后一項setSelector(R.drawable.list_corner_round_bottom);else {// 中間一項setSelector(R.drawable.list_corner_round_mid);}break;case MotionEvent.ACTION_UP:break;}return super.onInterceptTouchEvent(ev);} }
l? 主界面布局
<com.aaron.csdn.CornerListViewandroid:id="@+id/local_listView"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_margin="10dp"android:background="@drawable/list_corner_round_bg"android:cacheColorHint="#00000000"android:divider="#000000"android:dividerHeight="0.5px"android:fadingEdge="none" />
注意事項?????????????????????????????????????????????????????????????????????????????
solid:實心,就是填充的意思
android:color指定填充的顏色
gradient:漸變
android:startColor和android:endColor分別為起始和結(jié)束顏色,ndroid:angle是漸變角度,必須為45的
整數(shù)倍。
另外漸變默認的模式為android:type="linear",即線性漸變,可以指定漸變?yōu)閺较驖u
變,android:type="radial",徑向漸變需要指定半徑android:gradientRadius="50"。
?
stroke:描邊
android:width="2dp" 描邊的寬度,android:color描邊的顏色。
我們還可以把描邊弄成虛線的形式,設(shè)置方式為:
android:dashWidth="5dp"
android:dashGap="3dp"
其中android:dashWidth表示'-'這樣一個橫線的寬度,android:dashGap表示之間隔開的距離。
corners:圓角
android:radius為角的弧度,值越大角越圓。
效果??????????????????????????????????????????????????????????????????????????????????????
?
?
?
這里簡單講講,其實就是先寫三個shape文件,一個是上面都是圓角,一個是下面都是圓角,一個是上下都是圓角。然后自定義一個listview,重寫onInterceptTouchEvent,讓listview根據(jù)item所在位置和listview的數(shù)據(jù)總數(shù)來確定布局。
Android 實現(xiàn)ListView圓角效果就講完了。
就這么簡單。
總結(jié)
以上是生活随笔為你收集整理的Android 实现ListView圆角效果的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 保存ArrayListO
- 下一篇: android sina oauth2.