当前位置: 首页 > news >正文

圆角背景的ListView


先定义一张圆角的图片shape_bg_listview.xml
Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"    
  3.        android:shape="rectangle"  
  4.        >  
  5.        <gradient     
  6.        android:startColor="@android:color/white"     
  7.        android:endColor="#FFCCCCCC"     
  8.        android:angle="180"    
  9.        />  
  10.        <stroke android:width="0px"    
  11.        android:color="@android:color/white"    
  12.        />    
  13.        <solid android:color="@android:color/white"    
  14.        />    
  15.        <corners    
  16.        android:bottomRightRadius="20px"    
  17.        android:bottomLeftRadius="20px"    
  18.        android:topLeftRadius="20px"    
  19.        android:topRightRadius="20px"    
  20.        />  
  21. </shape>   

然后在ListView中引用它,注意android:listSelector为了去掉选中效果:
Xml代码 复制代码  收藏代码
  1. <ListView android:id="@+id/list"    
  2.         android:layout_width="fill_parent"    
  3.         android:layout_height="wrap_content"  
  4.         android:layout_margin="20.0dip"  
  5.         android:cacheColorHint="@null"    
  6.                 android:listSelector="@drawable/shape_bg_listview"  
  7.         android:background="@drawable/shape_bg_listview"  
  8.         />  

代码:
Java代码 复制代码  收藏代码
  1. public class App extends Activity {   
  2.     private ListView mListView = null;   
  3.        
  4.     /** Called when the activity is first created. */  
  5.     @Override  
  6.     public void onCreate(Bundle savedInstanceState) {   
  7.         super.onCreate(savedInstanceState);   
  8.         setContentView(R.layout.main);   
  9.         ArrayList<String> list =new ArrayList<String>();   
  10.         list.add("1");   
  11.         list.add("2");   
  12.         list.add("3");   
  13.         mListView =  (ListView)findViewById(R.id.list);   
  14.         mListView.setAdapter(new AppAdapter(this,list));   
  15.     }   
  16.        
  17.     class AppAdapter extends BaseAdapter{   
  18.   
  19.         Context context;   
  20.         ArrayList<String> list;   
  21.         AppAdapter(Context context,ArrayList<String> list){   
  22.             this.context=context;   
  23.             this.list=list;   
  24.         }   
  25.         @Override  
  26.         public int getCount() {   
  27.             // TODO Auto-generated method stub   
  28.             return list.size();   
  29.         }   
  30.   
  31.         @Override  
  32.         public Object getItem(int position) {   
  33.             // TODO Auto-generated method stub   
  34.             return list.get(position);   
  35.         }   
  36.   
  37.         @Override  
  38.         public long getItemId(int position) {   
  39.             // TODO Auto-generated method stub   
  40.             return position;   
  41.         }   
  42.   
  43.         @Override  
  44.         public View getView(int position, View convertView, ViewGroup parent) {   
  45.             // TODO Auto-generated method stub   
  46.             TextView tv;   
  47.             if(convertView==null){   
  48.                 convertView=LayoutInflater.from(context).inflate(R.layout.simple_item_2, null);   
  49.             }   
  50.             tv=(TextView)convertView.findViewById(R.id.name);   
  51.             tv.setText(list.get(position));   
  52.             return convertView;   
  53.         }   
  54.            
  55.     }   
  56. }  
public class App extends Activity {
	private ListView mListView = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ArrayList<String> list =new ArrayList<String>();
        list.add("1");
        list.add("2");
        list.add("3");
        mListView =  (ListView)findViewById(R.id.list);
        mListView.setAdapter(new AppAdapter(this,list));
    }
    
    class AppAdapter extends BaseAdapter{

    	Context context;
    	ArrayList<String> list;
    	AppAdapter(Context context,ArrayList<String> list){
    		this.context=context;
    		this.list=list;
    	}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			TextView tv;
			if(convertView==null){
				convertView=LayoutInflater.from(context).inflate(R.layout.simple_item_2, null);
			}
			tv=(TextView)convertView.findViewById(R.id.name);
			tv.setText(list.get(position));
			return convertView;
		}
    	
    }
}


simple_item_2.xml定义如下:
Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="wrap_content"  
  6.     >  
  7.     <TextView  
  8.         android:id="@+id/name"  
  9.         android:textSize="17.0sp"  
  10.         android:textColor="@android:color/black"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_marginLeft="20.0dip"  
  14.         android:layout_alignParentLeft="true"  
  15.         android:layout_centerVertical="true"  
  16.         >  
  17.     </TextView>  
  18.     <ImageView  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_marginRight="20.0dip"  
  22.         android:src="@drawable/arrow"  
  23.         android:layout_alignParentRight="true"  
  24.         android:layout_centerVertical="true"  
  25.         >  
  26.     </ImageView>  
  27. </RelativeLayout>  
from:http://gundumw100.iteye.com/blog/1085930

相关文章:

  • LayerDrawable层叠样式layer-list
  • android的edittext默认不获得焦点
  • Android中AsyncTask的用法实例
  • Eclipse中如何更改字体及字体大小
  • Android通过http协议POST传输方式
  • Android四种Activity的加载模式
  • 谷歌Android UI设计技巧:优秀UI设计准则
  • android UI优化
  • ListView性能优化之视图缓存
  • Android中ListView的addFooterView不显示的问题
  • iPhone入门学习——Objective-C学习文档
  • Android 开发中的网络下载策略 与 文件缓存策略
  • 关于代码重构的一些思考
  • Android使用VideoView播放网络视频
  • [Android 数据通信] android cmwap接入点
  • JS 中的深拷贝与浅拷贝
  • 实现windows 窗体的自己画,网上摘抄的,学习了
  • co.js - 让异步代码同步化
  • create-react-app做的留言板
  • GDB 调试 Mysql 实战(三)优先队列排序算法中的行记录长度统计是怎么来的(上)...
  • go语言学习初探(一)
  • JAVA_NIO系列——Channel和Buffer详解
  • Java精华积累:初学者都应该搞懂的问题
  • laravel with 查询列表限制条数
  • PAT A1092
  • Spring Boot MyBatis配置多种数据库
  • Vue.js 移动端适配之 vw 解决方案
  • 初识 beanstalkd
  • 搞机器学习要哪些技能
  • 给第三方使用接口的 URL 签名实现
  • 猴子数据域名防封接口降低小说被封的风险
  • 坑!为什么View.startAnimation不起作用?
  • 前端面试题总结
  • 使用前端开发工具包WijmoJS - 创建自定义DropDownTree控件(包含源代码)
  • 说说动画卡顿的解决方案
  • 最简单的无缝轮播
  • 06-01 点餐小程序前台界面搭建
  • 继 XDL 之后,阿里妈妈开源大规模分布式图表征学习框架 Euler ...
  • !!Dom4j 学习笔记
  • #gStore-weekly | gStore最新版本1.0之三角形计数函数的使用
  • #ifdef 的技巧用法
  • #LLM入门|Prompt#1.8_聊天机器人_Chatbot
  • #设计模式#4.6 Flyweight(享元) 对象结构型模式
  • #我与Java虚拟机的故事#连载16:打开Java世界大门的钥匙
  • (007)XHTML文档之标题——h1~h6
  • (02)vite环境变量配置
  • (12)目标检测_SSD基于pytorch搭建代码
  • (Oracle)SQL优化技巧(一):分页查询
  • (保姆级教程)Mysql中索引、触发器、存储过程、存储函数的概念、作用,以及如何使用索引、存储过程,代码操作演示
  • (附源码)springboot掌上博客系统 毕业设计063131
  • (附源码)计算机毕业设计ssm本地美食推荐平台
  • (一)pytest自动化测试框架之生成测试报告(mac系统)
  • (一)插入排序
  • (转)C#调用WebService 基础
  • .bat批处理出现中文乱码的情况