手机版
你好,游客 登录 注册
背景:
阅读新闻

Android游戏开发系统控件-ListView

[日期:2012-05-13] 来源:Linux社区  作者:wwj_748 [字体:大 中 小]

=>>ListViewProject_2运行效果:

=>>修改布局文件main.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:layout_height="fill_parent"  
  5.     android:orientation="horizontal" >  
  6. <ImageView  
  7.     android:layout_width="wrap_content"  
  8.     android:layout_height="wrap_content"  
  9.     android:id="@+id/iv"  
  10.     />  
  11.     <LinearLayout   
  12.         android:orientation="vertical"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         >  
  16.             <TextView   
  17.                 android:layout_width="wrap_content"  
  18.                 android:layout_height="wrap_content"  
  19.                 android:textSize="20sp"  
  20.                 android:id="@+id/bigtv"  
  21.                 />  
  22.             <TextView  
  23.                 android:layout_width="wrap_content"  
  24.                 android:layout_height="wrap_content"  
  25.                 android:textSize="10sp"  
  26.                 android:id="@+id/smalltv"  
  27.                 />"  
  28.     </LinearLayout>  
  29. <Button  
  30.     android:layout_width="wrap_content"  
  31.     android:layout_height="wrap_content"  
  32.     android:text="@string/btn"  
  33.     android:id="@+id/bn"  
  34.     />  
  35. <CheckBox  
  36.     android:layout_width="wrap_content"  
  37.     android:layout_height="wrap_content"  
  38.     android:id="@+id/cb"  
  39.     />  
  40. </LinearLayout>  

=>>string,xml

  1.   
  2. <?xml version="1.0" encoding="utf-8"?>  
  3. <resources>  
  4.   
  5.     <string name="hello">Hello World, ListViewProject_2Activity!</string>  
  6.     <string name="app_name">ListViewProject_2</string>  
  7.     <string name="btn">Button</string>  
  8.   
  9. </resources>  

=>>ListViewProject_2Activity.Java

  1. package com.ListView;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import android.app.Activity;  
  9. import android.os.Bundle;  
  10. import android.widget.ListView;  
  11. import android.widget.SimpleAdapter;  
  12.   
  13. public class ListViewProject_2Activity extends Activity {  
  14.     private SimpleAdapter sp;//声明适配器对象   
  15.     private ListView listView;//声明列表视图对象   
  16.     private List<Map<String,Object>>list;//声明列表容器   
  17.     /** Called when the activity is first created. */  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         //实例化列表容器   
  22.         list = new ArrayList<Map<String,Object>>();  
  23.         listView = new ListView(this);//实例化列表视图   
  24.         //实例化一个列表数据容器   
  25.         Map<String,Object> map = new HashMap<String,Object>();  
  26.         //往列表容器中添加数据   
  27.         map.put("item1_imageivew", R.drawable.ic_launcher);  
  28.         map.put("item1_bigtv", "BIGTV");  
  29.         map.put("item1_smalltv", "SMALLTV");  
  30.         //将列表数据添加到列表容器中   
  31.         list.add(map);  
  32.         //实例适配器   
  33.         sp = new SimpleAdapter(this,list,R.layout.main,new String[]{  
  34.                 "item1_imageivew","item1_bigtv","item1_smalltv"},new  
  35.                 int[]{R.id.iv,R.id.bigtv,R.id.smalltv});  
  36.         //为列表视图设置适配器(将数据映射到列表视图中)   
  37.         listView.setAdapter(sp);  
  38.         //显示列表视图   
  39.         this.setContentView(listView);  
  40.     }  
  41. }  
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款