Android中GridView(网格视图)效果源码(十五)
2015-11-06 18:29
525 查看
GridView(网格视图)是按照行列的方式来显示内容的,一般用于显示图片,图片等内容,比如实现九宫格图,用GridView是首选,也是最简单的。
GirdView的一些属性: android:numColumns="auto_fit" --------列数设置为自动 android:columnWidth="90dp",----------每列的宽度,也就是Item的宽度 android:stretchMode="columnWidth"------缩放与列宽大小同步 android:verticalSpacing="10dp"----------垂直边距 android:horizontalSpacing="10dp"-------水平边距
package com.itarchy.gridview; import android.app.Activity; import android.os.Bundle; import android.widget.GridView; /** * * @E-mail: itarchy@163.com * @version 创建时间:2015-11-6下午6:17:50 * @Des: */ public class MainActivity extends Activity { private GridView gridView; private MainAdapter mygvAdapter; private Integer[] mThumbIds = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取控件 gridView = (GridView) this.findViewById(R.id.gridview); initDataSource(); initAdapter(); } // 1 初始化数据源 private void initDataSource() { mThumbIds = new Integer[] { 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 }; } private void initAdapter() { mygvAdapter = new MainAdapter(this, mThumbIds); gridView.setAdapter(mygvAdapter); } }
<?xml version="1.0" encoding="utf-8"?> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridview" android:background="#ffbbbbbb" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:columnWidth="90dip" android:numColumns="auto_fit" android:stretchMode="columnWidth"/>
package com.itarchy.gridview; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; public class MainAdapter extends BaseAdapter { private Context mContext; private Integer[] data; public MainAdapter(Context context, Integer[] data) { this.mContext = context; this.data = data; } @Override public int getCount() { return data.length; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder mViewHolder; if (convertView == null) { convertView = View.inflate(mContext, R.layout.gv_list_item, null); // 创建一个绑定对象 mViewHolder = new ViewHolder(); // 初始化控件 mViewHolder.img = (ImageView) convertView.findViewById(R.id.img); // 绑定 convertView.setTag(mViewHolder); } else { // 从绑定对戏里重新获取一个View mViewHolder = (ViewHolder) convertView.getTag(); } // 赋值 mViewHolder.img.setBackgroundResource(data[position]); return convertView; } static class ViewHolder { ImageView img; } }
<RelativeLayout 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"> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true"/> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itarchy.gridview" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.itarchy.gridview.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories