有些东西看多了,就厌烦了:extjs对我这种感觉最为强烈。甚至,有时觉得设计之殇是审美疲劳。
直角看多了,就想看看圆角,不知何时,这几年刮起了一阵阵的圆角设计风:CSS新标准纳入圆角元素,iphone中几乎随处可见圆角设计,也开始出现很多圆角名片了...
今天我们就实现一个圆角的ListView效果。
圆角的设计,我们并不追求到处都用,无处不用,android中有少数界面用直角确实容易显得锋利,和周边界面太过对比而显得不协调,比如大栏目列表,设置等等,而采用圆角实现,则会活泼,轻松的多,也融合的特别好。

1.感觉
实际上在Android中因为SDK中没有默认对圆角的一个完整的支持,需要麻烦自定义设置才能实现完美的圆角效果,所以绝大多数应用都是采用分组直角列表这种样式。
所以我觉得很有必要让大家看看这些少数的不一样的东西,看看有什么不一样的感觉。
先让我们来看两张图片:

左边的是Android的一个应用的设置界面,右边是iphone系统的设置界面。
ps:上述只是效果,并不是说左边的圆角列表就是用listview是实现的,事实上它是用LinearLayout布局一个一个堆起来的。

2.原理
通过判断ListView上点击的项的位置,我们切换不同的选择器,当然这个切换的动作我们需要定义在重写ListView的onInterceptTouchEvent()方法中。

我把它封装成了一个类

/**
*
* @ClassName: CornerListView
* @Description: 判断ListView上点击的项的位置,选择不同的位置
* @author ws
* @date 2013-12-11 上午10:53:25
*
*/
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);
}
}

3.定义选择器
如果只有一项,我们需要四个角都是圆角,app_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"/>
<corners android:topLeftRadius="4dip"
android:topRightRadius="4dip"
android:bottomLeftRadius="4dip"
android:bottomRightRadius="4dip"/>
</shape>

如果是顶部第一项,则上面两个角为圆角,app_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"/>
<corners android:topLeftRadius="4dip"
android:topRightRadius="4dip"/>
</shape>

如果是底部最后一项,则下面两个角为圆角,app_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"/>
<corners android:bottomLeftRadius="4dip"
android:bottomRightRadius="4dip" />
</shape>

如果是中间项,则应该不需要圆角, app_list_corner_shape.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"/>
</shape>

4.背景图片
因为默认的情况下,ListView就要显示一个圆角的边框,这个我们使用一张9patch背景图片来实现app_list_corner_border.9.png:


在这里提示一下,做9patch背景图片的时候,记得把内容区域定义为边框线以内的区域。

 5.初步实现

 参考前面提供的素材和核心代码,我们初步实现如下

布局文件:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/set_manageaccout_linearlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/gray"
android:orientation="vertical" > <RelativeLayout
android:id="@+id/content_frame_mycollection_toprelative"
android:layout_width="match_parent"
android:layout_height="50dp" > <ImageButton
android:id="@+id/set_manageaccount_imagebutton_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:background="@drawable/content_frame_imagebutton_back" /> <TextView
android:id="@+id/content_frame_mycollection_textview_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/set_manageaccount_textview_title"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout> <ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray" /> <ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" > <com.jzlfapp.model.CornerListView
android:id="@+id/set_manageaccount_list1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:cacheColorHint="@android:color/transparent"
android:background="@drawable/bg_setting_item_normal" >
</com.jzlfapp.model.CornerListView> <com.jzlfapp.model.CornerListView
android:id="@+id/set_manageaccount_list2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:cacheColorHint="@android:color/transparent"
android:background="@drawable/bg_setting_item_normal" >
</com.jzlfapp.model.CornerListView>
</LinearLayout>
</ScrollView> </LinearLayout>

布局文件

6.效果图

参考资料地址:http://www.cnblogs.com/qianxudetianxia/archive/2011/09/19/2068760.html

android小知识之圆角ListView的更多相关文章

  1. android小知识之多个listview在同一界面只有item高亮

    我的工程里面一个activity有两个有圆角的listview,就是 自定义的 CornerListView继承ListView,  然后  我想圆角的listview A点击之后一个item会高亮  ...

  2. android小知识之EditText输入框之值监控以及类型限制(数字,英语字母,下划线,是否为星号密码)

    1.设置EditText的值监听事件 . <span style="font-size:14px;color:#990000;"> EditText ed=new Ed ...

  3. Android 小知识

    1.判断sd卡是否存在 boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environmen ...

  4. android小知识之SparseArray(HaspMap替换)

    最近编程时,发现一个针对HashMap<Integer, E>的一个提示: 翻译过来就是:用SparseArray<E>来代替会有更好性能.那我们就来看看源码中SparseAr ...

  5. android小知识之邮箱地址输入自动完成

    虽然不难,但是容易忘记,做个备忘吧 package com.guet.zhuge; import android.app.Activity; import android.os.Bundle; imp ...

  6. android小知识之意图(intent)

    android中的意图有显示意图和隐式意图两种, 显示意图要求必须知道被激活组件的包和class 隐式意图只需要知道跳转activity的动作和数据,就可以激活对应的组件 A 主activity  B ...

  7. android小知识之自定义通知(toast)

    Toast是较为熟悉的通知,但默认方式比较单调,可以根据自己的需求自定义,在统一UI风格的时候可以单独拿出来做一个工具类来使用. 下面我在Fragment中定义的一个按键弹出自定义Toast,在Act ...

  8. android小知识之中如何获取当前时间

    百度整理过来的 [java] view plaincopyprint? import    java.text.SimpleDateFormat; SimpleDateFormat    format ...

  9. Android小知识汇总

    1.Android Studio 将module编译打包成aar文件,module依赖的 (例如 compile 'com.zhy:autolayout:1.4.3' )不会被打包进入aar文件,应用 ...

随机推荐

  1. poj2121--暴力解法

    #include<iostream> #include<string> using namespace std; ]={"negative","z ...

  2. TCP/IP详解之:Ping程序、Traceroute程序

    Ping程序: ping程序是通过发送一份ICMP回显请求报文(即ICMP报文的一种,其类型为8,代码为0)给主机,并等待返回ICMP回显应答 来测试另一台主机是否可达. ping程序不用经过传输层, ...

  3. OpenCV学习(1)OpenCV简介

    简介 OpenCV的全称是:Open Source Computer Vision Library,OpenCV是一个开源的跨平台的计算机视觉库,可以运行在Linux.Windows和Mac OS操作 ...

  4. python heapq

    这个模块(build-in)实现了一个堆的数据结构,完美的解决了Top-K问题,以后解决Top-K问题的时候,直接把这个模块拿来用就可以了 注意,默认的heap是一个小顶堆! heapq模块提供了如下 ...

  5. 如何正确理解正则表达式中的分隔符 \b

    前言:好久不见,博客园. 最近在学习研究regex,其中有个特迷惑自己的知识点是分隔符 ( word boundary) [\b] (注:为了方便,后文都以[]来包含字符,并不是reg规则里面的[] ...

  6. php面向对象编程学习之高级特性

    前几天写了一篇关于php面向对象基础知识的博客,这两天看了php面向对象的高级特性,写出来记录一下吧,方便以后拿出来复习. 面向对象除了最基本的定义类之外,最主要就是因为面向的一些高级特性,运用这些高 ...

  7. ubuntu下lamp环境配置及将window代码迁移至linux系统

    因为最近要用需要去实现项目中的一个功能,比较好的做法就是在http://i.cnblogs.com/EditPosts.aspx?opt=1linux中实现.所以最近就将自己的代码全部迁移到linux ...

  8. Oracle EBS-SQL (SYS-5):sys_配置文件查询.sql

    select    distinct l.profile_option_name,             v.profile_option_value,             fu.user_na ...

  9. RejexLib

    http://www.regexlib.com/ http://www.brics.dk/automaton/index.html http://code.google.com/p/automatap ...

  10. (12)Visual Studio 2012如何透过电子邮件部署Xamarin.Android App

    原文 Visual Studio 2012如何透过电子邮件部署Xamarin.Android App Android App在部署到实机的时候不像iOS的App限制你一定要使用向Apple申请的开发者 ...