![](https://i-blog.csdnimg.cn/blog_migrate/215133010d4b2b0c9bc42e06f4ea2702.png)
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.test17.MainActivity" >
<GridView
android:id="@+id/gv_gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="4">
</GridView>
<GridView
android:id="@+id/gv_gridView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="4">
</GridView>
</RelativeLayout>
package com.example.test17;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private GridView gridView;
private GridView gridView2;
private String[] address={"北京","天津","上海","深圳","成都"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView=(GridView) findViewById(R.id.gv_gridView);
gridView2=(GridView) findViewById(R.id.gv_gridView2);
//创建适配器
ArrayAdapter<String> adapter=new ArrayAdapter<String>(
this,
R.layout.grid_item,
R.id.tv_textView,
address);
gridView.setAdapter(adapter);
gridView2.setAdapter(new MyAdapter());
}
class MyAdapter extends BaseAdapter{
//设置数据源
private int[] images={
R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e
};
private String[] names={
"name1","name2","name3","name4","name5"
};
@Override
public int getCount() {
// TODO Auto-generated method stub
return images.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
ViewHolder holder;
if(convertView==null){
holder=new ViewHolder();
view=LayoutInflater.from(MainActivity.this).inflate(R.layout.grid2_item, null);
holder.image=(ImageView) view.findViewById(R.id.iv_image2);
holder.tv=(TextView) view.findViewById(R.id.tv_textView2);
view.setTag(holder);
}else{
view=convertView;
holder=(ViewHolder) view.getTag();
}
ImageView image=holder.image;
image.setImageResource(images[position]);
TextView tv=holder.tv;
tv.setText(names[position]);
return view;
}
}
class ViewHolder{
ImageView image;
TextView tv;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/iv_image2"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_launcher"/>
<TextView
android:id="@+id/tv_textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="name1"
/>
</LinearLayout>