安卓listview默認佈局總結

在使用ListView的時候,常常用到Android自帶的list佈局,即simple_list_item_1、simple_list_item_2、simple_list_item_checked等。初次用起來,難免有點雲裏霧裏。下面,就這幾種list佈局,做一些簡單介紹:

注:適配器選用SimpleAdapter

 

main.xml 如下:

       

  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,直接上代碼:

      

  1. <span style="font-size:18px;">public class ListViewDemo extends ListActivity {  
  2.     private List<Map<String, String>> data = new ArrayList<Map<String,String>>();  
  3.       
  4.     @Override  
  5.     public void onCreate(Bundle savedInstanceState) {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.main);  
  8.         Map<String, String> map1 = new HashMap<String, String>();  
  9.         map1.put("姓名""風晴雪");                 
  10.         data.add(map1);  
  11.         Map<String, String> map2 = new HashMap<String, String>();  
  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.     }</span>  

 

上圖:

 


 二、simple_list_item_2、two_line_list_item(雙行顯示)

        兩種佈局很相似,都有兩個TextView:android.R.id.text1和android.R.id.text2,不同之處在於,前者兩行字是不一樣大小的,而後者

中兩行字體一樣大小,這裏使用前者作爲示例,兩者的用法一樣。先看simple_list_item_2.xml 佈局文件:

       

  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相同,看代碼:

       

  1. public class ListViewDemo extends ListActivity {  
  2.       
  3.     private List<Map<String, String>> data = new ArrayList<Map<String,String>>();  
  4.       
  5.     @Override  
  6.     public void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.main);  
  9.         Map<String, String> map1 = new HashMap<String, String>();  
  10.         map1.put("姓名""風晴雪");  
  11.         map1.put("性別""女的");  
  12.         data.add(map1);  
  13.         Map<String, String> map2 = new HashMap<String, String>();  
  14.         map2.put("姓名""慳臾");  
  15.         map2.put("性別""公的");  
  16.         data.add(map2);  
  17.         Map<String, String> map3 = new HashMap<String, String>();  
  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();

       

  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

發佈了22 篇原創文章 · 獲贊 3 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章