在使用ListView的时候,常常用到Android自带的list布局,即simple_list_item_1、simple_list_item_2、simple_list_item_checked等。初次用起来,难免有点云里雾里。下面,就这几种list布局,做一些简单介绍:

注:适配器选用SimpleAdapter

 

main.xml 如下:

       

[html]  view plain copy print ?
  1. <span style="font-size:18px;"><?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:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.     <ListView   
  7.         android:id="@+id/android:list"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"/>  
  10.     <TextView   
  11.         android:id="@+id/android:empty"  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:text="对不起,没有数据显示"/>    
  15. LinearLayout>span>  


包含一个id="@+id/android:list"的ListView和id="@+id/android:empty"的TextView,当ListView没有数据显示时,TextView显示出来,同时ListView会被影藏(貌似在ListActivity中才有此效果,在Activity中必须自行设置);

 

一、simple_list_item_1(单行显示)

       此布局显示最为简单,其中只有一个TextView,id为:android.R.id.text1,直接上代码:

      

[java]  view plain copy print ?
  1. "font-size:18px;">public class ListViewDemo extends ListActivity {  
  2.     private List> data = new ArrayList>();  
  3.       
  4.     @Override  
  5.     public void onCreate(Bundle savedInstanceState) {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.main);  
  8.         Map map1 = new HashMap();  
  9.         map1.put("姓名""风晴雪");                 
  10.         data.add(map1);  
  11.         Map map2 = new HashMap();  
  12.         map2.put("姓名""悭臾");  
  13.         data.add(map2);  
  14.         setListAdapter(new SimpleAdapter(this,data,android.R.layout.simple_list_item_1,  
  15.                 new String[]{"姓名"},            //每行显示一个姓名  
  16.                 new int[]{android.R.id.text1}   //名字在text1上显示  
  17.         ));  
  18.     }  

 

上图:

 


 二、simple_list_item_2、two_line_list_item(双行显示)

        两种布局很相似,都有两个TextView:android.R.id.text1和android.R.id.text2,不同之处在于,前者两行字是不一样大小的,而后者

中两行字体一样大小,这里使用前者作为示例,两者的用法一样。先看simple_list_item_2.xml 布局文件:

       

[html]  view plain copy print ?
  1. <TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"   
  2.       android:paddingTop="2dip"  
  3.       android:paddingBottom="2dip"  
  4.       android:layout_width="match_parent"  
  5.       android:layout_height="wrap_content"  
  6.       android:minHeight="?android:attr/listPreferredItemHeight"  
  7.       android:mode="twoLine"  
  8.       >  
  9.       
  10.     <TextView android:id="@android:id/text1"  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="wrap_content"  
  13.              android:layout_marginLeft="6dip"  
  14.              android:layout_marginTop="6dip"  
  15.         android:textAppearance="?android:attr/textAppearanceLarge"  
  16.     />  
  17.           
  18.     <TextView android:id="@android:id/text2"  
  19.         android:layout_width="match_parent"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_below="@android:id/text1"  
  22.              android:layout_alignLeft="@android:id/text1"  
  23.         android:textAppearance="?android:attr/textAppearanceSmall"  
  24.     />  
  25. TwoLineListItem>  

simple_list_item_2用法跟simple_list_item_1相同,看代码:

       

[java]  view plain copy print ?
  1. public class ListViewDemo extends ListActivity {  
  2.       
  3.     private List> data = new ArrayList>();  
  4.       
  5.     @Override  
  6.     public void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.main);  
  9.         Map map1 = new HashMap();  
  10.         map1.put("姓名""风晴雪");  
  11.         map1.put("性别""女的");  
  12.         data.add(map1);  
  13.         Map map2 = new HashMap();  
  14.         map2.put("姓名""悭臾");  
  15.         map2.put("性别""公的");  
  16.         data.add(map2);  
  17.         Map map3 = new HashMap();  
  18.         map3.put("姓名""百里屠苏");  
  19.         map3.put("性别""男的");  
  20.         data.add(map3);  
  21.         setListAdapter(new SimpleAdapter(this,data,android.R.layout.simple_list_item_2,  
  22.                 new String[]{"姓名","性别"},            //每行显示一组姓名和性别  
  23.                 new int[]{android.R.id.text1,android.R.id.text2}   //名字在text1上显示,性别在text2上显示  
  24.         ));  
  25.     }  
  26. }  


上图:simple_list_item_2

  

   two_line_list_item 

 

 三、simple_list_item_single_choice、simple_list_item_multiple_choice、simple_list_item_checked(不同的呈现方式)

        这三种布局增加了选项,有单选和多选模式。常用方法为setChoiceMode(),getCheckedItemPositions(),getCheckedItemIds();

       

[java]  view plain copy print ?
  1. setListAdapter(new SimpleAdapter(this,data,android.R.layout.simple_list_item_multiple_choice,  
  2.         new String[]{"姓名"},            //每行显示一组姓名  
  3.         new int[]{android.R.id.text1}   //名字在text1上显示  
  4. ));  
  5. /*表明有选项,若不设置,缺省为none,则点击后没有反应 
  6.  * 选项模式有:CHOICE_MODE_SINGLE  单选--ListView中只能有一个item被选中 
  7.  * CHOICE_MODE_MULTIPLE  多选--允许选中多个item 
  8.  * CHOICE_MODE_NONE  缺省 
  9.  */  
  10. getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);  


上图:依次为simple_list_item_multiple_choice、simple_list_item_single_choice、simple_list_item_checked格式

转自:http://blog.csdn.net/ma12an/article/details/7762961

更多相关文章

  1. Android(安卓)向右滑动关闭页面
  2. Android(安卓)Material Design :LinearLayoutCompat添加分割线div
  3. ListView-BaseAdapter
  4. Android(安卓)四种阴影实现方式对比
  5. Android(安卓)sdk报错,unfortunately XXX has stopped的可能原因
  6. PreferenceActivity使用介绍
  7. 【Android(安卓)开发教程】重新布局
  8. android 组件隐蔽显示状态
  9. Android实现气泡布局/弹窗效果 气泡尖角方向及偏移量可控

随机推荐

  1. android设置wifi/bt默认开关状态
  2. Ubuntu 配置Android SDK NDK环境变量
  3. flutter 报错 x86
  4. Android AMR格式录音和播放,仿微信
  5. Android图形处理-Drawabble
  6. Android studio 028 获取GPS
  7. Android Drawable工具类,
  8. 删除安卓系统的应用
  9. Android 监听长时单击(OnLongClickListene
  10. Android Intent and Intent Filter