Android控件之GridView
GridView是Android中的数据显示控件,先体验一下它的效果
这是GridView只呈现图片的效果。源码下载/2012/0425/20120425103107648.rar
下面详细介绍一下GridView。
一、简介
在二维可滚动网格中呈现子项(Item),Item来自于与之相关的ListAdapter.
二、重要方法
getStretchMode():获取GridView的延伸模式。
onKeyDown(int keyCode, KeyEvent event):默认KeyEvent.Callback.onKeyMultiple()
三、具体应用
1.在布局文件中说明
<GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:columnWidth="60dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
2.程序使用
private GridView mGrid;
mGrid = (GridView) findViewById(R.id.grid);
3.定义适配器
public class AppsAdapter extends BaseAdapter {
public AppsAdapter() {
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i;
if (convertView == null) {
i = new ImageView(GridDemo.this);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new GridView.LayoutParams(50, 50));
} else {
i = (ImageView) convertView;
}
ResolveInfo info = mApps.get(position);
i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));
return i;
}
public final int getCount() {
return mApps.size();
}
public final Object getItem(int position) {
return mApps.get(position);
}
public final long getItemId(int position) {
return position;
}
}
4.应用适配器
mGrid.setAdapter(new AppsAdapter());
5.获取图片
private void loadApps() {
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mApps = getPackageManager().queryIntentActivities(mainIntent, 0);
}
摘自 永恒的记忆
补充:移动开发 , Android ,