完成后的效果
详细请看下面代码,已写入相关代码
public class AppListActivity extends Activity {
private GridView GridViewapplist;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
loadApplist();
setContentView(R.layout.layout_applist);
GridViewapplist = (GridView) findViewById(R.id.gridviewApplist);
GridViewapplist.setAdapter(new AppAdapter());
}
private List<ResolveInfo> mApps;
/**
* 读取程序列表
* */
private void loadApplist() {
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
mApps = getPackageManager().queryIntentActivities(intent, 0);
}
private class AppAdapter extends BaseAdapter {
/**
* 返回资源ID数组长度
* */
@Override
public int getCount() {
// TODO Auto-generated method stub
return mApps.size();
}
/**
* 得到Item
* */
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mApps.get(position);
}
/**
* 获取Items的ID
* */
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView view;
if (convertView == null) {
view = new ImageView(AppListActivity.this);
view.setScaleType(ImageView.ScaleType.FIT_CENTER);
view.setLayoutParams(new GridView.LayoutParams(50, 50));
} else {
view = (ImageView) convertView;
}
// 获取程序的图标
ResolveInfo info = mApps.get(position);
view.setImageDrawable(info.activityInfo
.loadIcon(getPackageManager()));
return view;
}
}
}
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <GridView android:id="@+id/gridviewApplist" android:layout_width="fill_parent" android:layout_height="wrap_content" android:numColumns="4" > </GridView> </LinearLayout>