安卓--網格視圖(GridView)執行個體

來源:互聯網
上載者:User

標籤:安卓   gridview   

main.xml代碼如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <GridView        android:id="@+id/myGridView"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:numColumns="3"        android:stretchMode="columnWidth" >    </GridView></LinearLayout>

grid_layout.xml代碼如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <ImageView        android:id="@+id/img"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:scaleType="center"        android:padding="3px"/></LinearLayout>

.java代碼 如下:

package org.lxh.demo;import java.lang.reflect.Field;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.os.Bundle;import android.widget.GridView;import android.widget.SimpleAdapter;public class Hello extends Activity {private List<Map<String, Integer>> list = new ArrayList<Map<String, Integer>>();private SimpleAdapter simpleAdapter = null;private GridView gridView = null;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); // 生命週期方法super.setContentView(R.layout.main); // 設定要使用的布局管理器this.initAdapter();this.gridView = (GridView) super.findViewById(R.id.myGridView);this.gridView.setAdapter(this.simpleAdapter);}private void initAdapter() {Field[] fields = R.drawable.class.getDeclaredFields();//反射機制for (int x = 0; x < fields.length; x++) {if (fields[x].getName().startsWith("png_")) {Map<String, Integer> map = new HashMap<String, Integer>();try {map.put("img", fields[x].getInt(R.drawable.class));} catch (IllegalArgumentException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}this.list.add(map);}this.simpleAdapter = new SimpleAdapter(this, this.list,R.layout.grid_layout, new String[] { "img" },new int[] { R.id.img });}}}

效果如下:


安卓--網格視圖(GridView)執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.