Android 控件之GridView

GridView是Android中的数据显示控件,先体验一下它的效果

Android 控件之GridView

这是GridView只呈现图片的效果。源码下载

下面详细介绍一下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);
}

更多相关文章

  1. Android新控件MotionLayout介绍(二)
  2. Android5.0水波纹效果适配4.X
  3. 自定义ProgressDialog(无遮罩效果)
  4. android下使用Fragment实现左侧3级菜单+动画效果
  5. Android 遍历界面控件
  6. Android控件之TabHost探究
  7. 转:android 图片叠加效果——两种方法
  8. Android——文本类控件

随机推荐

  1. Android警告错误搜集
  2. android修改进程名
  3. Android: PowerManager.WakeLock
  4. Android(安卓)Component 简介
  5. android电话拦截及短信过滤
  6. Android7.0中文文档(API)-- RemoteViews
  7. android单元测试
  8. Android(安卓)Support Library 23.2
  9. Android(安卓)面试题及答案(英文)
  10. android的ExpandableListView