我正在尝试制作网格视图,因为这对更多设备来说更好。我已经尝试在界面构建器中添加我的按钮(作为 ImageViews,因为我已将它们添加为 ImageButtons,但尺寸不好),结果出现在此屏幕中:http: //i.imgur.com/pTJnKOv .png
任何人都可以通过在 GridView 中进行此操作来帮助我,完全相同但在所有设备上都兼容。
提前致谢 ;)!
这是图像适配器:
public class ImageAdapter extends BaseAdapter {
private Context context;
private final String[] buttonValues;
public ImageAdapter(Context context, String[] buttonValues) {
this.context = context;
this.buttonValues = buttonValues;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from mobile.xml
gridView = inflater.inflate(R.layout.mobile, null);
// set image based on selected text
ImageView imageView = (ImageView) gridView
.findViewById(R.id.grid_item_image);
String button = buttonValues[position];
if (button.equals("homework")) {
imageView.setImageResource(R.drawable.homework);
} else if (button.equals("schedule")) {
imageView.setImageResource(R.drawable.schedulebut);
} else if (button.equals("planner")) {
imageView.setImageResource(R.drawable.plannerbut);
} else {
imageView.setImageResource(R.drawable.settingsbut);
}
} else {
gridView = (View) convertView;
}
return gridView;
}
@Override
public int getCount() {
return buttonValues.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
}
这是我在 onCreate 中的方法:
static final String[] MOBILE_OS = new String[] { "Homework", "Schedule",
"Planner", "Settings" };
private void setGridView() {
gridView = (GridView) findViewById(R.id.gridView1);
gridView.setAdapter(new ImageAdapter(this, MOBILE_OS));
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(getApplicationContext(), "LOL!", Toast.LENGTH_SHORT).show();
}
});
}