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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。