gridview一般的办法是 自定义一个模板布局,然后再通过把xml的layout文件加载进来
比如说对于这个例子的话可能就要写一个xml文件,里面分别有imagebutton 和 textview放在一起的layout
但是其实可以利用textview可以设置图片的特点来简单化,实际上我们在getview中返回一系列的textview即可了
public View getView(int position, View convertView, ViewGroup parent) {
TextView text = new TextView(context);
text.setGravity(Gravity.CENTER_HORIZONTAL);
text.setCompoundDrawablePadding(2);
// 重点,将该图片显示在TextView上方
text.setCompoundDrawablesWithIntrinsicBounds(null, context.getResources().getDrawable(R.drawable.ic_launcher), null, null);
text.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16f);
text.setText(String.valueOf(position));
return text;
}
这样就可以实现效果了,简单吧?哈哈
当然一些很复杂的还是通过xml来比较好
本文介绍了一种简化GridView布局的方法,通过在TextView中直接设置图片资源,避免了使用复杂的XML布局文件,这种方法适用于简单的显示需求。

3859

被折叠的 条评论
为什么被折叠?



