shape和selector的结合使用-超炫效果
1.Shape 简介 作用:XML中定义的几何形状 位置:res/drawable/文件的名称.xml 使用的方法: Java代码中: R.drawable.文件的名称 XML中: Android:background="@drawable/文件的名称" 属性: <shape> Android:shape=["rectangle" | "oval" | "line" | "ring"] 其中rectagle矩形,oval椭圆,line水平直线,ring环形 <shape>中子节点的常用属性: <gradient> 渐变 Android:startColor 起始颜色 Android:endColor 结束颜色 Android:angle 渐变角度,0从上到下,90表示从左到右,数值为45的整数倍默认为0; Android:type 渐变的样式 liner线性渐变 radial环形渐变 sweep <solid > 填充 Android:color 填充的颜色 <stroke > 描边 Android:width 描边的宽度 Android:color 描边的颜色 Android:dashWidth 表示'-'横线的宽度 Android:dashGap 表示'-'横线之间的距离 <corners > 圆角 Android:radius 圆角的半径 值越大角越圆 Android:topRightRadius 右上圆角半径 Android:bottomLeftRadius 右下圆角角半径 Android:topLeftRadius 左上圆角半径 Android:bottomRightRadius 左下圆角半径
一个列表的实例:
//drawable/selector.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <selector xmlns:Android="http://schemas.android.com/apk/res/android" > 3 4 <item Android:state_selected="true"> 5 <shape> 6 <gradient Android:angle="270" Android:endColor="#99BD4C" 7 Android:startColor="#A5D245" /> 8 <size Android:height="60dp" Android:width="320dp" /> 9 <corners Android:radius="8dp" /> 10 </shape> 11 </item> 12 <item Android:state_pressed="true"> 13 <shape> 14 <gradient Android:angle="270" Android:endColor="#99BD4C" 15 Android:startColor="#A5D245"/> 16 <size Android:height="60dp" Android:width="320dp" /> 17 <corners Android:radius="8dp" /> 18 </shape> 19 </item> 20 <item> 21 <shape> 22 <gradient Android:angle="270" Android:endColor="#A8C3B0" 23 Android:startColor="#C6CFCE" /> 24 <size Android:height="60dp" Android:width="320dp" /> 25 <corners Android:radius="8dp" /> 26 </shape> 27 </item> 28 29 </selector>
//layout/list_item.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android" 3 Android:orientation="horizontal" 4 Android:layout_width="fill_parent" 5 Android:layout_height="wrap_content" 6 Android:background="@drawable/selector" 7 > 8 <ImageView 9 Android:id="@+id/img" 10 Android:layout_width="wrap_content" 11 Android:layout_height="wrap_content" 12 Android:layout_gravity="center_vertical" 13 Android:layout_marginLeft="20dp" 14 /> 15 <TextView 16 Android:text="data" 17 Android:id="@+id/title" 18 Android:layout_width="fill_parent" 19 Android:layout_height="wrap_content" 20 Android:gravity="center_vertical" 21 Android:layout_marginLeft="20dp" 22 Android:layout_marginTop="20dp" 23 Android:textSize="14sp" 24 Android:textStyle="bold" 25 Android:textColor="@color/black" 26 > 27 </TextView> 28 </LinearLayout>
//values/colors.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3 <color name="white">#FFFFFFFF</color> 4 <color name="transparency">#00000000</color> 5 <color name="title_bg">#1C86EE</color> 6 <color name="end_color">#A0cfef83</color> 7 <color name="black">#464646</color> 8 </resources>
//layout/main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:Android="http://schemas.android.com/apk/res/android" 4 Android:orientation="vertical" 5 Android:layout_width="fill_parent" 6 Android:layout_height="wrap_content" 7 Android:background="#253853" 8 > 9 <ListView 10 Android:id="@+id/list" 11 Android:layout_width="match_parent" 12 Android:layout_height="match_parent" 13 Android:cacheColorHint="#00000000" 14 Android:divider="#2A4562" 15 Android:dividerHeight="3px" 16 Android:listSelector="#264365" 17 Android:drawSelectorOnTop="false" 18 > 19 </ListView> 20 </LinearLayout>
//src/MyListViewActivity.java
1 package com.mylistview; 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.view.View.OnClickListener; 10 import android.widget.AdapterView; 11 import android.widget.AdapterView.OnItemClickListener; 12 import android.widget.ListView; 13 import android.widget.SimpleAdapter; 14 15 public class MyListViewActivity extends Activity { 16 17 ListView list; 18 String data[] = new String[]{ 19 "China","UK","USA","Japan","German","Canada","ET","Narotu" 20 }; 21 ArrayList<HashMap<String, Object>> mDlist; 22 SimpleAdapter adapter; 23 24 @Override 25 public void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.main); 28 29 list =(ListView) findViewById(R.id.list); 30 adapter = new SimpleAdapter(this, getData(), R.layout.list_item, 31 new String[]{"title","img"}, new int[]{R.id.title,R.id.img}); 32 list.setAdapter(adapter); 33 34 list.setOnItemClickListener(new OnItemClickListener(){ 35 36 @Override 37 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 38 long arg3) { 39 // TODO Auto-generated method stub 40 mDlist.remove( arg2 ); 41 adapter.notifyDataSetChanged(); 42 } 43 44 }); 45 46 } 47 48 private ArrayList<HashMap<String, Object>> getData() { 49 mDlist = new ArrayList<HashMap<String,Object>>(); 50 for(int i =0;i<data.length;i++){ 51 HashMap<String, Object>map = new HashMap<String, Object>(); 52 map.put("title", data[i]); 53 map.put("img", R.drawable.gowalla_hdpi); 54 mDlist.add(map); 55 } 56 return mDlist; 57 } 58 }
效果图
引用自:http://www.cnblogs.com/top5/archive/2012/05/10/2494763.html,部分修改,点击某项时,删除该项,仅做自己收藏学习使用。
posted on 2012-05-11 21:30 cmlanche-明 阅读(407) 评论(0) 编辑 收藏 举报