Android ListView圆角实现

来源:互联网 发布:上海惠普软件电话 编辑:程序博客网 时间:2024/05/16 17:07

当然除了使用drawable这样的图片外今天谈下自定义图形shape的方法,对于button控件android上支持以下几种属性shape、gradient、stroke、corners等。

我们就以目前系统的button的selector为例说下:

<shape>            <gradient                android:startcolor="#ff8c00"                android:endcolor="#ffffff"                android:angle="270" />            <stroke                android:width="2dp"                android:color="#dcdcdc" />            <corners                android:radius="2dp" />            <padding                android:left="10dp"                android:top="10dp"                android:right="10dp"                android:bottom="10dp" />        </shape>

 

对于上面,这条shape的定义,分别为渐变,在gradient中startcolor属性为开始的颜色,endcolor为渐变结束的颜色,下面的angle是角度。接下来是stroke可以理解为边缘,corners为拐角这里radius属性为半径,最后是相对位置属性padding。

对于一个button完整的定义可以为:

<?xml version="1.0" encoding="utf-8"?><selector    xmlns:android="http://www.norkoo.com">    <item android:state_pressed="true" >        <shape>            <gradient                android:startcolor="#ff8c00"                android:endcolor="#ffffff"                android:angle="270" />            <stroke                android:width="2dp"                android:color="#dcdcdc" />            <corners                android:radius="2dp" />            <padding                android:left="10dp"                android:top="10dp"                android:right="10dp"                android:bottom="10dp" />        </shape>    </item>    <item android:state_focused="true" >        <shape>            <gradient                android:startcolor="#ffc2b7"                android:endcolor="#ffc2b7"                android:angle="270" />            <stroke                android:width="2dp"                android:color="#dcdcdc" />            <corners                android:radius="2dp" />            <padding                android:left="10dp"                android:top="10dp"                android:right="10dp"                android:bottom="10dp" />        </shape>    </item>    <item>                <shape>            <gradient                android:startcolor="#ff9d77"                android:endcolor="#ff9d77"                android:angle="270" />            <stroke                android:width="2dp"                android:color="#fad3cf" />            <corners                android:radius="2dp" />            <padding                android:left="10dp"                android:top="10dp"                android:right="10dp"                android:bottom="10dp" />        </shape>    </item></selector>

 

注意!提示大家,以上几个item的区别主要是体现在state_pressed按下或state_focused获得焦点时,当当来判断显示什么类型,而没有state_xxx属性的item可以看作是常规状态下。

 

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

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

<item

android:color="hex_color"

android:state_pressed=["true" | "false"]

android:state_focused=["true" | "false"]

android:state_selected=["true" | "false"]

android:state_active=["true" | "false"]

android:state_checkable=["true" | "false"]

android:state_checked=["true" | "false"]

android:state_enabled=["true" | "false"]

android:state_window_focused=["true" | "false"] />

</selector>

 

elements:

<selector>

必须。必须是根元素。包含一个或多个<item>元素。

attributes:

xmlns:android

string,必须。定义xml的命名空间,必须是

“http://schemas.android.com/apk/res/android”.

<item>

定义特定状态的color,通过它的特性指定。必须是<selector>的子元素。

attributes:

android:color

16进制颜色。必须。这个颜色由rgb值指定,可带alpha。

这个值必须以“#”开头,后面跟随alpha-red-green-blue信息:

l #rgb

l #argb

l #rrggbb

l #aarrggbb

android:state_pressed

boolean。“true”表示按下状态使用(例如按钮按下);“false”表示非按下状态使用。

android:state_focused

boolean。“true”表示聚焦状态使用(例如使用滚动球/d-pad聚焦button);“false”表示非聚焦状态使用。

android:state_selected

boolean。“true”表示选中状态使用(例如tab打开);“false”表示非选中状态使用。

android:state_checkable

boolean。“true”表示可勾选状态时使用;“false”表示非可勾选状态使用。(只对能切换可勾选—非可勾选的构件有用。)

android:state_checked

boolean。“true”表示勾选状态使用;“false”表示非勾选状态使用。

android:state_enabled

boolean。“true”表示可用状态使用(能接收触摸/点击事件);“false”表示不可用状态使用。

android:window_focused

boolean。“true”表示应用程序窗口有焦点时使用(应用程序在前台);“false”表示无焦点时使用(例如notification栏拉下或对话框显示)。

注意:记住一点,statelist中第一个匹配当前状态的item会被使用。因此,如果第一个item没有任何状态特性的话,那么它将每次都被使用,这也是为什么默认的值必须总是在最后(如下面的例子所示)。

 

examples:

xml文件保存在res/color/button_text.xml

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

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

<item android:state_pressed="true"

android:color="#ffff0000"/> <!-- pressed -->

<item android:state_focused="true"

android:color="#ff0000ff"/> <!-- focused -->

<item android:color="#ff000000"/> <!-- default -->

</selector>

 

这个layout xml会应用colorstatelist到一个view上:

<button

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/button_text"

android:textcolor="@color/button_text" />

 

       在android上开发项目,如果仅仅是采用默认的样式可能不是很美观,需要编写响应的样式来使界面美观,在iphone上常用的圆角ListView的实现。

        本人实现的原理如下:

 通过重写ListView中拦截触摸的事件方式,在生成ListView时候根据不同行采用不同的样式。如第一个行,最末一行,和中建行。(特殊的情况下只有一行的时候,四个角均为圆角考虑)。

实现如下:

 

 

最后一行为时候,下方两个角为圆角样式如下:

app_list_corner_round_bottom.xml

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <gradient android:startColor="#BFEEFF"   
  4.         android:endColor="#40B9FF"   
  5.         android:angle="270"/>  
  6.     <corners android:bottomLeftRadius="6dip"  
  7.         android:bottomRightRadius="6dip" />  
  8. </shape>   

 

 

第一行为圆角且仅仅为一条记录使用样式:

app_list_corner_round.xml

 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <gradient android:startColor="#BFEEFF"   
  4.         android:endColor="#40B9FF"   
  5.         android:angle="270"/>  
  6.     <corners android:topLeftRadius="6dip"  
  7.         android:topRightRadius="6dip"  
  8.         android:bottomLeftRadius="6dip"  
  9.         android:bottomRightRadius="6dip"/>  
  10. </shape>   

 

第一行为圆角且有多条记录使用样式:

app_list_corner_round_top.xml

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <gradient android:startColor="#BFEEFF"   
  4.         android:endColor="#40B9FF"   
  5.         android:angle="270"/>  
  6.     <corners android:topLeftRadius="6dip"  
  7.         android:topRightRadius="6dip"/>  
  8. </shape>   

 

多行记录非第一行和最末一行使用的样式:

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <gradient android:startColor="#BFEEFF"   
  4.         android:endColor="#40B9FF"   
  5.         android:angle="270"/>  
  6. </shape>   

 

重写的ListView

Java代码  收藏代码
  1. package com.easyway.listview.corner;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.view.MotionEvent;  
  6. import android.widget.AdapterView;  
  7. import android.widget.ListView;  
  8.   
  9. /** 
  10.  *  圆角ListView 
  11.  *      重写ListView的样式实现相关的样式 
  12.  *  app_list_corner_round_top.xml 
  13.  *  <?xml version="1.0" encoding="utf-8"?> 
  14. <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
  15.     <gradient android:startColor="#BFEEFF"  
  16.         android:endColor="#40B9FF"  
  17.         android:angle="270"/> 
  18.     <corners android:topLeftRadius="6dip" 
  19.         android:topRightRadius="6dip"/> 
  20. </shape>  
  21.  *   
  22.  *  android:shape 配置的是图形的形式,主要包括方形、圆形等,上边代码为方形。 
  23.  *  gradient节点主要配置起点颜色、终点颜色、中间点的坐标、中间点的颜色、渐变角度(90度为上下渐变,0为左右渐变), 
  24.  *  padding节点主要配置上下左右边距, 
  25.  *  corners节点配置四周园角的半径。  
  26.  *   
  27.  *  
  28.  * @Title:  
  29.  * @Description: 实现TODO 
  30.  * @Copyright:Copyright (c) 2011 
  31.  * @Company:易程科技股份有限公司 
  32.  * @Date:2012-7-16 
  33.  * @author  longgangbai 
  34.  * @version 1.0 
  35.  */  
  36. public class CornerListView extends ListView {  
  37.     public CornerListView(Context context) {  
  38.         super(context);  
  39.     }  
  40.   
  41.     public CornerListView(Context context, AttributeSet attrs, int defStyle) {  
  42.         super(context, attrs, defStyle);  
  43.     }  
  44.   
  45.     public CornerListView(Context context, AttributeSet attrs) {  
  46.         super(context, attrs);  
  47.     }  
  48.   
  49.     /** 
  50.      * 重写此方式实现不同行的样式不一样 
  51.      *  
  52.      */  
  53.     @Override  
  54.     public boolean onInterceptTouchEvent(MotionEvent ev) {  
  55.         switch (ev.getAction()) {  
  56.         //  
  57.         case MotionEvent.ACTION_DOWN:  
  58.                 int x = (int) ev.getX();  
  59.                 int y = (int) ev.getY();  
  60.                 //返回记录数据行数  
  61.                 int itemnum = pointToPosition(x, y);  
  62.   
  63.                 if (itemnum == AdapterView.INVALID_POSITION)  
  64.                         break;                   
  65.                 else{  
  66.                     if(itemnum==0){  
  67.                         if(itemnum==(getAdapter().getCount()-1)){                                      
  68.                             setSelector(R.drawable.app_list_corner_round); //仅仅一行记录的样式  
  69.                         }else{  
  70.                             setSelector(R.drawable.app_list_corner_round_top); //多行且第一行的样式  
  71.                         }  
  72.                     }else if(itemnum==(getAdapter().getCount()-1))  //最后一行的样式  
  73.                             setSelector(R.drawable.app_list_corner_round_bottom);  
  74.                     else{                              
  75.                         setSelector(R.drawable.app_list_corner_shape);  
  76.                     }  
  77.                 }  
  78.                 break;  
  79.         case MotionEvent.ACTION_UP:  
  80.                 break;  
  81.         }  
  82.           
  83.         return super.onInterceptTouchEvent(ev);  
  84.     }  
  85. }  

 

主要布局类main.xml

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:id="@+id/listview_layout"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <LinearLayout  
  9.         android:id="@+id/linearLayout1"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:orientation="vertical"  
  13.         android:paddingBottom="5dp"  
  14.         android:paddingLeft="25dp"  
  15.         android:paddingTop="15dp" >  
  16.   
  17.         <TextView  
  18.             android:id="@+id/menu_1"  
  19.             android:layout_width="wrap_content"  
  20.             android:layout_height="wrap_content"  
  21.             android:text="@string/setting"  
  22.             android:textColor="@color/gray" />  
  23.     </LinearLayout>  
  24.   
  25.     <com.easyway.listview.corner.CornerListView  
  26.         android:id="@+id/list1"  
  27.         android:layout_width="fill_parent"  
  28.         android:layout_height="wrap_content"  
  29.         android:layout_marginLeft="15dp"  
  30.         android:layout_marginRight="15dp"  
  31.         android:background="@drawable/shape_bg_listview"  
  32.         android:cacheColorHint="@null"/>  
  33.   
  34. </LinearLayout>  

 每行的布局:

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <RelativeLayout  
  8.         android:id="@+id/relativeLayout1"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:paddingBottom="10dip"  
  12.         android:paddingLeft="15dip"  
  13.         android:paddingRight="5dip"  
  14.         android:paddingTop="10dip" >  
  15.   
  16.         <TextView  
  17.             android:id="@+id/item_title"  
  18.             android:layout_width="wrap_content"  
  19.             android:layout_height="wrap_content"  
  20.             android:layout_alignParentLeft="true"  
  21.             android:layout_alignParentTop="true"  
  22.             android:textColor="@color/gray"  
  23.             android:textSize="15sp" />  
  24.   
  25.         <ImageView  
  26.             android:id="@+id/imageView1"  
  27.             android:layout_width="wrap_content"  
  28.             android:layout_height="wrap_content"  
  29.             android:layout_alignParentRight="true"  
  30.             android:layout_centerVertical="true"  
  31.             android:layout_marginRight="16dp"  
  32.             android:src="@drawable/right" />  
  33.           
  34.     </RelativeLayout>  
  35.   
  36. </LinearLayout>  

 

主要类:

Java代码  收藏代码
  1. package com.easyway.listview.corner;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5.   
  6. import android.app.Activity;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.widget.AdapterView;  
  10. import android.widget.AdapterView.OnItemClickListener;  
  11. import android.widget.LinearLayout;  
  12. import android.widget.SimpleAdapter;  
  13.   
  14. /** 
  15.  * Android实现圆角ListView示例 
  16.  *          实现圆角ListView原理主要采用添加相关的样式布局。 
  17.  *          通过重写ListView中特定的方法 
  18.  *              拦截触摸事件的方法。 
  19.  *              public boolean onInterceptTouchEvent(MotionEvent ev)  
  20.  *              根据横轴坐标计算行数,并实现 
  21.  *               
  22.  * @Title:  
  23.  * @Description: 实现TODO 
  24.  * @Copyright:Copyright (c) 2011 
  25.  * @Company:易程科技股份有限公司 
  26.  * @Date:2012-7-16 
  27.  * @author  longgangbai 
  28.  * @version 1.0 
  29.  */  
  30. public class RoundCornerActivity extends Activity {  
  31.     private CornerListView cornerListView = null;  
  32.     private ArrayList<HashMap<String, String>> maplist = null;  
  33.     private LinearLayout linearLayout;  
  34.     /** Called when the activity is first created. */  
  35.     @Override  
  36.     public void onCreate(Bundle savedInstanceState) {  
  37.         super.onCreate(savedInstanceState);  
  38.         //设置布局  
  39.         setContentView(R.layout.main);  
  40.         //获取布局对象  
  41.         linearLayout=(LinearLayout)findViewById(R.id.listview_layout);  
  42.         //设置壁纸为背景图片  
  43.         linearLayout.setBackgroundDrawable(getWallpaper());  
  44.         //获取初始化数据  
  45.         maplist=getData();  
  46.         //创建一个适配器对象  
  47.         SimpleAdapter adapter1 = new SimpleAdapter(this, maplist,  
  48.                 R.layout.simple_list_item_1, new String[] { "item" },  
  49.                 new int[] { R.id.item_title });  
  50.         //创建ListView对象  
  51.         cornerListView = (CornerListView) findViewById(R.id.list1);  
  52.         //设置适配器  
  53.         cornerListView.setAdapter(adapter1);  
  54.         initListener();  
  55.     }  
  56.   
  57.     private void initListener() {  
  58.         //添加响应时间  
  59.         cornerListView.setOnItemClickListener(new OnItemClickListener() {  
  60.   
  61.             @Override  
  62.             public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  
  63.                     long arg3) {  
  64.                 if (arg2 == 0) {  
  65.                     System.out.println("0");  
  66.                 }else{  
  67.                     System.out.println("1");  
  68.                 }  
  69.             }  
  70.         });  
  71.     }  
  72.   
  73.     /** 
  74.      * 模拟数据 
  75.      * @return 
  76.      */  
  77.     public ArrayList<HashMap<String, String>> getData() {  
  78.   
  79.         maplist = new ArrayList<HashMap<String, String>>();  
  80.         HashMap<String, String> map1 = new HashMap<String, String>();  
  81.         HashMap<String, String> map2 = new HashMap<String, String>();  
  82.         HashMap<String, String> map3 = new HashMap<String, String>();  
  83.         HashMap<String, String> map4 = new HashMap<String, String>();  
  84.         map1.put("item""公交");  
  85.         map2.put("item""火车");  
  86.         map3.put("item""地铁");  
  87.         map4.put("item""航空");  
  88.         maplist.add(map1);  
  89.         maplist.add(map2);  
  90.         maplist.add(map3);  
  91.         maplist.add(map4);  
  92.         return maplist;  
  93.     }  
  94.   
  95. }  
0 0
原创粉丝点击