GridView是Android中的數據顯示控件,先體驗一下它的效果android
這是GridView只呈現圖片的效果。源碼下載佈局
下面詳細介紹一下GridView。this
1、簡介spa
在二維可滾動網格中呈現子項(Item),Item來自於與之相關的ListAdapter.xml
2、重要方法blog
getStretchMode():獲取GridView的延伸模式。圖片
onKeyDown(int keyCode, KeyEvent event):默認KeyEvent.Callback.onKeyMultiple()ip
3、具體應用ci
1.在佈局文件中說明get
<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); }