Android學習筆記之控制項GridView

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   ar   color   os   sp   

1.xml布局檔案

 

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="match_parent" 4     android:layout_height="match_parent" 5     android:orientation="vertical" > 6      7     <GridView  8         android:id="@+id/girdView1" 9         android:numColumns="2"10         android:verticalSpacing="10dp"11         android:horizontalSpacing="10dp"12         android:layout_width="wrap_content"13         android:layout_height="wrap_content">14     </GridView>15     16    <!--17     android:numColumns    //列數18     android:verticalSpacing  //兩行之間的距離19     android:horizontalSpacing  //兩列之間的距離20     -->21 22 </LinearLayout>

 

item.xml

 

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:orientation="vertical" 6     android:gravity="center" > 7      8     <ImageView  9         android:id="@+id/imageView1"10         android:layout_width="60dp"11         android:layout_height="60dp"12         android:src="@drawable/a"/>13     14     <TextView 15         android:id="@+id/textView1"16         android:layout_marginTop="5dp"17         android:layout_width="wrap_content"18         android:layout_height="wrap_content"19         android:textSize="15sp"20         android:text="阿森納"/>21     22 23 </LinearLayout>

 

2.java代碼

 

 1 public class MainActivity extends ActionBarActivity implements OnItemClickListener { 2      3     private GridView gv1; 4     private List<Map<String,Object>> dataList; 5     private int[] pic = {R.drawable.a,R.drawable.l,R.drawable.m,R.drawable.q}; 6     private String[] name = {"阿森納","利物浦","曼聯","切爾西"}; 7     private SimpleAdapter sa; 8  9     @Override10     protected void onCreate(Bundle savedInstanceState) {11         super.onCreate(savedInstanceState);12         setContentView(R.layout.main);13         14         gv1 = (GridView) findViewById(R.id.girdView1);15         //初始化16         dataList = new ArrayList<Map<String,Object>>();17         18         //1.準備資料來源19         //2.建立適配器(SimpleAdapter)20         //3.GridView載入適配器21         //4.GridView配置時間監聽器(onItemClickListener)22         23          sa = new SimpleAdapter(this, getData(),R.layout.item ,new String[]{"pic","name"}, new int[]{R.id.imageView1,R.id.textView1});24         gv1.setAdapter(sa);25         gv1.setOnItemClickListener(this);26         27        28         29     }30         private List<Map<String,Object>> getData() {31                  for(int i =0;i<pic.length;i++){32                     Map<String,Object> map = new HashMap<String, Object>();33                    map.put("pic", pic[i]);34                    map.put("name", name[i]);35                    dataList.add(map);36         }37                 return dataList;38         }39         @Override40         public void onItemClick(AdapterView<?> parent, View view, int position,41                 long id) {42             // TODO Auto-generated method stub43             Toast.makeText(this, "我是"+name[position], Toast.LENGTH_SHORT).show();44         }  45 }

 

Android學習筆記之控制項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.