手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >android开发之方形圆角listview代码分享
android开发之方形圆角listview代码分享
摘要:先看效果图:首先,你得写一个类我们命名为CornerListView[java]复制代码代码如下:/***圆角ListView示例*@Des...

先看效果图:

android开发之方形圆角listview代码分享1

首先,你得写一个类我们命名为CornerListView

[java]

复制代码 代码如下:

/**

* 圆角ListView示例

* @Description: 圆角ListView示例

* @FileName: CornerListView.java

*/

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);

}

@Override

public 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 == AdapterView.INVALID_POSITION)

break;

else{

if(itemnum==0){

if(itemnum==(getAdapter().getCount()-1)){

setSelector(R.drawable.<SPAN>app_list_corner_round</SPAN>);

}else{

setSelector(R.drawable.<SPAN>app_list_corner_round_top</SPAN>);

}

}else if(itemnum==(getAdapter().getCount()-1))

setSelector(R.drawable.<SPAN>app_list_corner_round_bottom</SPAN>);

else{

setSelector(R.drawable.<SPAN>app_list_corner_shape</SPAN>);

}

}

break;

case MotionEvent.ACTION_UP:

break;

}

return super.onInterceptTouchEvent(ev);

}

}

/**

* 圆角ListView示例

* @Description: 圆角ListView示例

* @FileName: CornerListView.java

*/

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);

}

@Override

public 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 == AdapterView.INVALID_POSITION)

break;

else{

if(itemnum==0){

if(itemnum==(getAdapter().getCount()-1)){

setSelector(R.drawable.app_list_corner_round);

}else{

setSelector(R.drawable.app_list_corner_round_top);

}

}else if(itemnum==(getAdapter().getCount()-1))

setSelector(R.drawable.app_list_corner_round_bottom);

else{

setSelector(R.drawable.app_list_corner_shape);

}

}

break;

case MotionEvent.ACTION_UP:

break;

}

return super.onInterceptTouchEvent(ev);

}

}

其中,app_list_corner_round

[html]

复制代码 代码如下:

<SPAN><?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android">

<gradient android:startColor="#BFEEFF"

android:endColor="#40B9FF"

android:angle="270"/>

<corners android:topLeftRadius="6dip"

android:topRightRadius="6dip"

android:bottomLeftRadius="6dip"

android:bottomRightRadius="6dip"/>

</shape> </SPAN>

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android">

<gradient android:startColor="#BFEEFF"

android:endColor="#40B9FF"

android:angle="270"/>

<corners android:topLeftRadius="6dip"

android:topRightRadius="6dip"

android:bottomLeftRadius="6dip"

android:bottomRightRadius="6dip"/>

</shape>

app_list_corner_round_top

[html]

复制代码 代码如下:

<SPAN><?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android">

<gradient android:startColor="#BFEEFF"

android:endColor="#40B9FF"

android:angle="270"/>

<corners android:topLeftRadius="6dip"

android:topRightRadius="6dip"/>

</shape> </SPAN>

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android">

<gradient android:startColor="#BFEEFF"

android:endColor="#40B9FF"

android:angle="270"/>

<corners android:topLeftRadius="6dip"

android:topRightRadius="6dip"/>

</shape>

app_list_corner_round_bottom

[html]

复制代码 代码如下:

<SPAN><?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android">

<gradient android:startColor="#BFEEFF"

android:endColor="#40B9FF"

android:angle="270"/>

<corners android:bottomLeftRadius="6dip"

android:bottomRightRadius="6dip" />

</shape> </SPAN>

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android">

<gradient android:startColor="#BFEEFF"

android:endColor="#40B9FF"

android:angle="270"/>

<corners android:bottomLeftRadius="6dip"

android:bottomRightRadius="6dip" />

</shape>

app_list_corner_shape

[html]

复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android">

<gradient android:startColor="#BFEEFF"

android:endColor="#40B9FF"

android:angle="270"/>

</shape>

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android">

<gradient android:startColor="#BFEEFF"

android:endColor="#40B9FF"

android:angle="270"/>

</shape>

写好了之后,就可以在你的代码中直接像listview一样调用。

【android开发之方形圆角listview代码分享】相关文章:

解析Android开发优化之:从代码角度进行优化的技巧

Android用户界面开发之:TextView的使用实例

Android UI开发 View自绘控件 分享

Android开发之软键盘用法实例分析

Android 通用型手电筒代码

Android开发之OpenGL ES 颜色

android panellistview 圆角实现代码

android 为应用程序创建桌面快捷方式技巧分享

android 设置圆角图片实现代码

android书架效果实现原理与代码

精品推荐
分类导航