显示多个图像的网格视图?安卓

时间:2013-07-04 06:19:18

标签: android

我想在Galaxy Tab上显示包含多个图像的网格视图...

我是开发新手,我真的很感激你的帮助。

的Vivek

1 个答案:

答案 0 :(得分:0)

public class MainActivity extends Activity {

  GridView gv1;
  GridViewAdapter madapter;
   int arr[]={R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
   String arr1[]={"ashu","hello","yes","no"};

@Override 
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   gv1=(GridView)findViewById(R.id.gridView1);

madapter=new GridViewAdapter();

gv1.setAdapter(madapter);
}

适配器类:

class GridViewAdapter extends BaseAdapter{

@Override 
 public int getCount() {
    // TODO Auto-generated method stub
    return arr1.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 0; }

@Override public View getView(int position, View convertView,
ViewGroup parent) {
    LayoutInflater infla=getLayoutInflater();

    View v=infla.inflate(R.layout.main1,null);
    TextView tv1=(TextView)v.findViewById(R.id.textView1);

    ImageView iv1=(ImageView)v.findViewById(R.id.imageView1);
    tv1.setText(arr1[position]);
    iv1.setBackgroundResource(arr[position]);
    return v; }}
}

<强> activity_main.xml中

    <GridView  
    android:id="@+id/gridView1"   android:layout_width="match_parent"   
     android:layout_height="match_parent"
    android:padding="10dp"
    android:verticalSpacing="10dp"

    android:horizontalSpacing="10dp"
    android:numColumns="auto_fit"
    android:columnWidth="60dp"
    android:stretchMode="columnWidth"

    android:gravity="center"
    /> </RelativeLayout>       

<强> main1.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/ic_launcher" />
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/imageView1"
        android:text="TextView" /> </RelativeLayout>