ListActivity
ListActivity的使用
ListActivity类中集成了一个ListView控件。
通过继承ListActivity类可方便地使用ListView控件
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 public class 类名extends ListActivity { 2 protected void onCreate(Bundle savedInstanceState) { 3 super.onCreate (savedInstanceState); 4 //获取资源文件中的数组 5 String [] data=getResources().getStringArray(R.array.city); 6 ArrayAdapter<String> adapter=new ArrayAdapter<String> (this, 7 android.R.layout.simple_list_item_multiple_choice,data); //样式为复选框 8 setListAdapter (adapter); 9 ListView listView=getListView (); //获取ListActivity自带的ListView对象 10 //设置ListView的选择行为:复选 11 listView.setChoiceMode (ListView.CHOICE_MODE_MULTIPLE); 12 } 13 }
网格列表GridView(按多行多列显示信息)
方法 |
属性 |
描述 |
public GridView(Context context) |
|
创建GridView对象 |
public void setStretchMode(int stretchMode) |
android:stretchMode |
缩放模式 |
public void setVerticalSpacing(int verticalSpacing) |
android: verticalSpacing |
设置垂直间距 |
public void setHorizontalSpacing(int horizontalSpacing) |
android: horizontalSpacing |
设置水平间距 |
public void setNumColumns(int numColumns) |
android: numColumns |
设置每列显示的数据量,若设置为auto——fit则表示自动设置 |
public void setSelection(int position) |
|
设置默认选中项 |
public void setGravity(int gravity) |
android: gravity |
设置对齐模式,由Grivity类指定 |
public void setAdapter(ListAdapter adapter) |
|
设置图片集 |
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
<LinearLayout android: layout_width="fill_parent" android: layout_height="fill_parent"> <ImageView android:id="@+id/Img" android: layout_width="90dp" android: layout_height="100dp" android:scaleType="fitXY" //设置所有图片大小一致 android:padding="1px"/> //设置四周边距为1像素 </LinearLayout>
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
<LinearLayout android: layout_width="fill_parent" android: layout_height="fill_parent"> <GridView android:id="@+id/gv" android: layout_width="fill_parent" android: layout_height="fill_parent" android:numColumns="3" //设置所有图片大小一致 android:stretchMode="columnWidth"/> //设置所有图片大小一致 </LinearLayout>
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 GridView gv = (GridView) findViewById(R.id.gv); //找到控件 2 Field[] fields = R.drawable.class.getDeclaredFields(); //读到png结尾的图片 3 List<Map<String, Integer>> data = new ArrayList<Map<String, Integer>>(); 4 for (Field field : fields) { 5 Map<String, Integer> map = new HashMap<String, Integer>(); 6 try { 7 map.put("img", field.getInt(R.drawable.class)); 8 data.add(map); 9 } catch (IllegalArgumentException e) { 10 e.printStackTrace(); 11 } catch (IllegalAccessException e) { 12 e.printStackTrace(); 13 } 14 } 15 SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.gridview0, 16 new String[] { "img" },new int[] { R.id.GImg }); //创建适配器 17 gv.setAdapter(adapter); //添加GridView对象 18 19 gv.setOnItemClickListener(new OnItemClickListener() { 20 public void onItemClick(AdapterView<?> adp, View view, int position, long arg) { 21 Map<String, Integer> map= (Map<String, Integer>)adp.getItemAtPosition(position); 22 int imgId=map.get("img"); 23 ImageView iv=new ImageView(GridView0.this); 24 iv.setScaleType(ImageView.ScaleType.CENTER); 25 iv.setImageResource(imgId); 26 final Dialog da=new AlertDialog.Builder(GridView0.this).setTitle("展示图片").setView(iv).create(); 27 new Thread(){ //设置线程,让对话框在5秒后关闭 28 public void run(){ 29 try { 30 Thread.sleep(5000); 31 } catch (InterruptedException e) { 32 e.printStackTrace(); 33 }finally{ 34 da.dismiss(); 35 } 36 } 37 }.start(); 38 da.show(); 39 } 40 });