如何用图像显示网格视图?

时间:2013-07-03 03:59:05

标签: android

我正在尝试显示图像的网格视图。我已经尝试了以下代码,但我的应用程序在运行后被强制关闭。我哪里出错了? 我的代码:

AndroidGridLayoutActivity:

public class AndroidGridLayoutActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        GridView gv=(GridView)findViewById(R.id.grid_view);
        gv.setAdapter(new ImageAdapter(this));
    }
}

ImageAdapter.java:

public class ImageAdapter extends BaseAdapter {
                private Context mcontext;
                public Integer[] mThumbids={R.drawable.img9,R.drawable.img10,R.drawable.img11,R.drawable.img12,R.drawable.img13,R.drawable.img14};
                public ImageAdapter(Context c)
                {
                                mcontext=c;
                }

                @Override
                public int getCount() {
                                // TODO Auto-generated method stub
                                return mThumbids.length;
                }

                @Override
                public Object getItem(int position) {
                                // TODO Auto-generated method stub
                                return mThumbids[position];
                }

                @Override
                public long getItemId(int position) {
                                // TODO Auto-generated method stub
                                return 0;
                }

                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                                // TODO Auto-generated method stub
                                ImageView imageView=new ImageView(mcontext);
                                imageView.setImageResource(mThumbids[position]);
                                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                                imageView.setLayoutParams(new GridView.LayoutParams(70,70));
                                return imageView;
                }



}

main.xml中:

<GridView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/grid_view"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:numColumns="auto_fit"
  android:columnWidth="90dp"
  android:horizontalSpacing="10dp"
  android:verticalSpacing="10dp"
  android:stretchMode="columnWidth"/>

1 个答案:

答案 0 :(得分:0)

公共类MainActivity扩展了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);





}

类GridViewAdapter扩展了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) {
    // TODO Auto-generated method stub


    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>