您可以使用自定义适配器(对于 listView):
public class CustomAdapter extends ArrayAdapter<Integer> {
Activity context;
ArrayList<Integer> objects;
public CustomAdapter(Activity context, ArrayList<Integer> objects) {
super(context, R.layout.row, objects);
this.context = context;
this.objects = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row, parent, false);
}
ImageView i = (ImageView) convertView.findViewById(R.id.icon);
i.setBackgroundResource(objects.get(position));
TextView t = (TextView) convertView.findViewById(R.id.title);
t.setText("title");
return convertView;
}
}
当然,您会将其设置为 gridView 的适配器。
编辑:当然,而不是ArrayAdapter<Integer>
你可以扩展ArrayAdapter<YourHolder>
.
class YourHolder {
public int ID;
public String title;
}
编辑 2:添加一段功能代码。
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<GridView
android:id="@+id/grid_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="3"
android:gravity="center_horizontal"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:padding="10dp"
android:background="#600000ff"
/>
</RelativeLayout>
grid_item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#6000ff00"
android:padding="4dp">
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="label"
android:layout_gravity="center_horizontal"/>
<FrameLayout
android:id="@+id/holder"
android:layout_width="100dp"
android:layout_height="100dp">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>
</FrameLayout>
</LinearLayout>
CustomAdapter.class
public class CustomAdapter extends ArrayAdapter<Integer> {
private Activity context;
private ArrayList<Integer> objects;
public CustomAdapter(Activity context, ArrayList<Integer> objects) {
super(context, R.layout.grid_item_layout, objects);
this.context = context;
this.objects = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
LayoutInflater i = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = (View) i.inflate(R.layout.grid_item_layout, parent, false);
}
TextView t = (TextView) convertView.findViewById(R.id.label);
ImageView i = (ImageView) convertView.findViewById(R.id.image);
t.setText("label " + position);
i.setImageResource(objects.get(position));
return convertView;
}
主要活动
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView g = (GridView) findViewById(R.id.grid_view);
CustomAdapter adapter = new CustomAdapter(this, getData());
g.setAdapter(adapter);
}
public ArrayList<Integer> getData(){
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(R.drawable.pic1);
// add 2 - 11
list.add(R.drawable.pic12);
return list;
}
}
最后的样子:
注意:根据我的经验,当gridView中有很多图片滚动变得缓慢和滞后,但这不是你的问题的主题,所以如果它也是一个问题,请参阅ListView中的图像延迟加载