您好,登錄后才能下訂單哦!
Android中怎么利用Gridview實現一個圖片列表,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
1.創建一個新的項目:gridview.
2,打開 res/layout/main.xml文件,插入一下代碼:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <GridView android:id="@+id/gridview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:columnWidth="90dp" android:numColumns="auto_fit" android:verticalSpacing="10dp" android:horizontalSpacing="10dp" android:stretchMode="columnWidth" android:gravity="center" /> </LinearLayout>
做好以后就可以在屏幕上顯示一個grid view
3.打開gridview.java
首先要導入包:
package cn.gridview; import android.widget.GridView; import android.widget.Toast; import android.app.Activity; import android.os.Bundle; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView; import android.view.View; 在onCreate添加方法: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); GridView gridview=(GridView)findViewById(R.id.gridview);//找到main.xml中定義gridview 的id gridview.setAdapter(new ImageAdapter(this));//調用ImageAdapter.java gridview.setOnItemClickListener(new OnItemClickListener(){//監聽事件 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(gridview.this, ""+position,Toast.LENGTH_SHORT).show();//顯示信息; } }); } ImageAdapter.java package cn.gridview; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.content.Context; import android.widget.ImageView; import android.widget.GridView; public class ImageAdapter extends BaseAdapter { private Context mContext; public ImageAdapter(Context c) { mContext=c; } @Override public int getCount() { // TODO Auto-generated method stub return mThumbIds.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ImageView imageview; if(convertView==null) { imageview=new ImageView(mContext); imageview.setLayoutParams(new GridView.LayoutParams(85, 85)); imageview.setScaleType(ImageView.ScaleType.CENTER_CROP); imageview.setPadding(8,8,8,8); } else { imageview=(ImageView) convertView; } imageview.setImageResource(mThumbIds[position]); return imageview; } private Integer[] mThumbIds={//顯示的圖片數組 R.drawable.sample_2,R.drawable.sample_3, R.drawable.sample_4,R.drawable.sample_5, R.drawable.sample_6,R.drawable.sample_7, R.drawable.sample_0,R.drawable.sample_1, R.drawable.sample_2,R.drawable.sample_3, R.drawable.sample_4,R.drawable.sample_5, R.drawable.sample_6,R.drawable.sample_7, R.drawable.sample_0,R.drawable.sample_1, R.drawable.sample_2,R.drawable.sample_3, R.drawable.sample_4,R.drawable.sample_5, R.drawable.sample_6,R.drawable.sample_7, }; }
關于Android中怎么利用Gridview實現一個圖片列表問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。