Android開(kāi)發(fā)中GridView用法示例
本文實(shí)例講述了Android開(kāi)發(fā)中GridView用法。分享給大家供大家參考,具體如下:
Android的GridView控件用于把一系列的空間組織成一個(gè)二維的網(wǎng)格顯示出來(lái),應(yīng)用的比較多的就是組合圖片顯示。下面我就詳細(xì)講一個(gè)例子。
首先寫(xiě)一個(gè)類繼承BaseAdapter
1. Java代碼
package com.yarin.android.Examples_04_19; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; public class ImageAdapter extends BaseAdapter { // 定義Context private Context mContext; // 定義整型數(shù)組 即圖片源 private Integer[] mImageIds = { R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5, R.drawable.img6, R.drawable.img7, R.drawable.img8, R.drawable.img1, }; public ImageAdapter(Context c) { mContext = c; } // 獲取圖片的個(gè)數(shù) public int getCount() { return mImageIds.length; } // 獲取圖片在庫(kù)中的位置 public Object getItem(int position) { return position; } // 獲取圖片ID public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { // 給ImageView設(shè)置資源 imageView = new ImageView(mContext); // 設(shè)置布局 圖片120×120顯示 imageView.setLayoutParams(new GridView.LayoutParams(85, 85)); // 設(shè)置顯示比例類型 imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); } else { imageView = (ImageView) convertView; } imageView.setImageResource(mImageIds[position]); return imageView; } }
2. Java代碼
package com.yarin.android.Examples_04_19; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.GridView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; public class Activity01 extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //取得GridView對(duì)象 GridView gridview = (GridView) findViewById(R.id.gridview); //添加元素給gridview gridview.setAdapter(new ImageAdapter(this)); // 設(shè)置Gallery的背景 gridview.setBackgroundResource(R.drawable.bg0); //事件監(jiān)聽(tīng) gridview.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { Toast.makeText(Activity01.this, "你選擇了" + (position + 1) + " 號(hào)圖片", Toast.LENGTH_SHORT).show(); } }); } }
3. XML代碼
<?xml version="1.0" encoding="utf-8"?> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:numColumns="auto_fit" android:verticalSpacing="10dp" android:horizontalSpacing="10dp" android:columnWidth="90dp" android:stretchMode="columnWidth" android:gravity="center" />
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》及《Android資源操作技巧匯總》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android中實(shí)現(xiàn)多行、水平滾動(dòng)的分頁(yè)的Gridview實(shí)例源碼
- android ListView和GridView拖拽移位實(shí)現(xiàn)代碼
- Android中RecyclerView布局代替GridView實(shí)現(xiàn)類似支付寶的界面
- Android GridView實(shí)現(xiàn)滾動(dòng)到指定位置的方法
- android中GridView的用法示例
- android GridView多選效果的實(shí)例代碼
- Android開(kāi)發(fā)之使用GridView展示圖片的方法
- Android GridView仿微信朋友圈顯示圖片
- Android實(shí)現(xiàn)九宮格(GridView中各項(xiàng)平分空間)的方法
- Android實(shí)現(xiàn)GridView中ImageView動(dòng)態(tài)變換的方法
- Android開(kāi)發(fā)之實(shí)現(xiàn)GridView支付寶九宮格
- Android控件之GridView用法實(shí)例分析
相關(guān)文章
Android Secret Code(輸入字符彈出手機(jī)信息)詳解
這篇文章主要介紹了Android Secret Code(輸入字符彈出手機(jī)信息)詳解的相關(guān)資料,需要的朋友可以參考下2016-11-11AndriodStudio使用listview實(shí)現(xiàn)簡(jiǎn)單圖書(shū)管理
這篇文章主要為大家詳細(xì)介紹了AndriodStudio使用listview實(shí)現(xiàn)簡(jiǎn)單圖書(shū)管理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03Android實(shí)現(xiàn)移動(dòng)小球和CircularReveal頁(yè)面切換動(dòng)畫(huà)實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于利用Android如何實(shí)現(xiàn)移動(dòng)的小球和CircularReveal頁(yè)面切換動(dòng)畫(huà)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-09-09Android點(diǎn)擊Button實(shí)現(xiàn)功能的幾種方法總結(jié)
當(dāng)Button有多個(gè)或者Button的使用次數(shù)很多時(shí),我們需要采用綁定監(jiān)聽(tīng)器的做法,其實(shí),綁定監(jiān)聽(tīng)器也有幾種方法,不過(guò),我在這里就不一一列舉了,畢竟那些方法在實(shí)際的應(yīng)用中也不常見(jiàn)2013-10-10Android XRecyclerView實(shí)現(xiàn)多條目加載
這篇文章主要為大家詳細(xì)介紹了Android XRecyclerView實(shí)現(xiàn)多條目加載效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Android指紋識(shí)別功能深入淺出分析到實(shí)戰(zhàn)(6.0以下系統(tǒng)解決方案)
指紋識(shí)別在現(xiàn)實(shí)應(yīng)用中已經(jīng)很多了,本篇文章主要介紹了Android指紋識(shí)別功能,具有一定的參考價(jià)值,有需要的可以了解一下。2016-11-11IDEA打包jar-解決找不到或無(wú)法加載主類 main的問(wèn)題
這篇文章主要介紹了IDEA打包jar-解決找不到或無(wú)法加載主類 main的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08