Home>Article>Backend Development> Android UI control series: GridView (grid layout)

Android UI control series: GridView (grid layout)

黄舟
黄舟 Original
2017-01-19 09:58:18 1125browse

GridView displays content in rows and columns. It is generally suitable for displaying icons, pictures and other content. It is mainly used to set Adapter

Here is mainly the basic BaseAdapter class, and rewriting the methods in it is mainly rewriting The getView method sets the display format of the image

The example code is as follows

GridViewTest.java

/* * @author hualang */ package org.hualang.grid; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; public class GridViewTest extends Activity { /** Called when the activity is first created. */ private GridView gv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //通过findViewById方法获得GridView对象 gv=(GridView)findViewById(R.id.GridView01); //设置GridView的行数 gv.setNumColumns(4); gv.setAdapter(new MyAdapter(this)); } //自定义适配器 class MyAdapter extends BaseAdapter { //图片id数组 private Integer[] imgs={ R.drawable.img01, R.drawable.img02, R.drawable.img03, R.drawable.img04, R.drawable.img05, R.drawable.img06, R.drawable.img07, R.drawable.img08, R.drawable.img01, R.drawable.img02, R.drawable.img03, R.drawable.img04, R.drawable.img05, R.drawable.img06, R.drawable.img07, R.drawable.img08 }; //上下文对象 Context context; //构造方法 MyAdapter(Context context) { this.context=context; } //获得数量 public int getCount() { return imgs.length; } //获得当前选项 public Object getItem(int item) { return item; } //获得当前选项id public long getItemId(int id) { return id; } //创建View方法 public View getView(int position,View convertView,ViewGroup parent) { ImageView imageView; if(convertView==null) { //实例化ImageView对象 imageView=new ImageView(context); //设置ImageView对象布局 imageView.setLayoutParams(new GridView.LayoutParams(45,45)); //设置边界对齐 imageView.setAdjustViewBounds(false); //设置刻度类型 imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); //设置间距 imageView.setPadding(8,8,8,8); }else { imageView=(ImageView)convertView; } //为ImageView设置图片资源 imageView.setImageResource(imgs[position]); return imageView; } } }

main.xml

   

The running results are as follows

Android UI control series: GridView (grid layout)

The above is the content of the Android UI control series: GridView (grid layout). For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn