【Android】九宮格實現

來源:互聯網
上載者:User

標籤:

第一步,布局檔案


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".JokeTabHostActivity"    android:orientation="vertical"    >    <GridView         android:id="@+id/GridView"        android:listSelector="@android:color/transparent"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:columnWidth="90dp"        android:gravity="center"        android:numColumns="auto_fit"        android:layout_marginTop="10dp"        android:stretchMode="columnWidth">    </GridView></LinearLayout>

第二步,在你的activity  中調用初始化Grid

GridView gridview = (GridView) findViewById(R.id.GridView);ArrayList<HashMap<String, Object>> meumList = new ArrayList<HashMap<String, Object>>();for (int i = 1; i < 4; i++) {HashMap<String, Object> map = new HashMap<String, Object>();if (i == 1) {map.put("ItemImage", R.drawable.a1);map.put("ItemText", "文章精選");} else if (i == 2) {map.put("ItemImage", R.drawable.a2);map.put("ItemText", "幽默笑話");} else {map.put("ItemImage", R.drawable.a5);map.put("ItemText", "期待很多其它");}meumList.add(map);}SimpleAdapter saItem = new SimpleAdapter(this, meumList, // 資料來源R.layout.item, // xml實現new String[] { "ItemImage", "ItemText" }, // 相應map的Keynew int[] { R.id.ItemImage, R.id.ItemText }); // 相應R的Id// //加入Item到網格中gridview.setAdapter(saItem); // 加入點擊事件gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {@SuppressWarnings("static-access")public void onItemClick(AdapterView<?

> arg0, View arg1, int arg2,long arg3) {int index = arg2 + 1;// id是從0開始的,所以須要+1if (index == 1) {Intent intent = new Intent();intent.setClass(MainActivity.this,CloseTabHostActivity.class);startActivity(intent);}if (index == 2) { Intent intent = new Intent(); intent.setClass(MainActivity.this, XListViewActivity.class); startActivity(intent);//showInfo("正在努力碼代碼中。。。

");// finish();//停止當前的Activity,假設不寫,則按返回鍵會跳回原來的Activity}if (index == 3) {showInfo("期待你的建議,好建議請Q 649175826");}}});



著作權聲明:本文博主原創文章,部落格,未經同意不得轉載。

【Android】九宮格實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.