android的grid: 很多ui界面都需要使用grid来显示二维数据,grid就像是html中的table用来显示一个表的行列值,grid也可以被用来显示图标,android系统启动后的菜单列出了所有应用就是用grid控制。 用grid显示数据:
这种用grid显示数据在b/s和c/s中比较常见,在手机由于受到屏幕大小限制用这种方式显示数据很不方便,所有一般grid用来 显示一些图标
具体代码如下:
package com.android777.demo.uicontroller;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;货运专家
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class GridActivity extends Activity {
private List<ResolveInfo> apps ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview);
GridView gridView = (GridView) findViewById(R.id.dataGrid);
//使用Intent查询出所有的app启动Activity信息
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
apps = getPackageManager().queryIntentActivities(mainIntent, 0);
gridView.setAdapter(new AppAdapter(this, apps));
}软件开发
class AppAdapter extends ArrayAdapter<ResolveInfo>{
Context mContext;
public AppAdapter(Context context,List<ResolveInfo> apps){
super(context,0,apps);
mContext = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView view ;
if(convertView == null){
view = new ImageView(mContext);
view.setLayoutParams(new GridView.LayoutParams(50, 50));
}else{
view = (ImageView) convertView;
}
ResolveInfo mResolveInfo = getItem(position);
view.setImageDrawable(mResolveInfo.activityInfo.loadIcon(getPackageManager()));
return view;
}
}
}