Trung tâm Tin học – ĐH KHTN Tạo Menu dạng GridView 1/ Các bạn tạo Project như sau: Project name: GridViewMenu Build Target: Google APIs Application name: GridViewMenu Package name: com.sample. GridViewMenu Create Activity: GridViewMenu 2/ Các bạn chỉnh sửa nội dung trong file main.xml như sau: <?xml version="1.0" encoding="utf-8"?> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/myGrid" 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" /> 3/ Các bạn code file GridViewMenu.java như sau: package com.sample.GridViewMenu; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; import android.widget.Toast; public class GridViewMenu extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { Lập trình Android – http://laptrinhdidong.vn Page 1 Trung tâm Tin học – ĐH KHTN super.onCreate(savedInstanceState); setContentView(R.layout.main); GridView g = (GridView) findViewById(R.id.myGrid); g.setAdapter(new ImageAdapter(this)); g.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { Toast.makeText(GridViewMenu.this, "" + position, Toast.LENGTH_SHORT).show(); } }); } public class ImageAdapter extends BaseAdapter { public ImageAdapter(Context c) { mContext = c; } public int getCount() { return mThumbIds.length; } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { imageView = new ImageView(mContext); imageView.setLayoutParams(new GridView.LayoutParams(45, 45)); imageView.setAdjustViewBounds(false); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setPadding(8, 8, 8, 8); } else { imageView = (ImageView) convertView; } imageView.setImageResource(mThumbIds[position]); return imageView; } private Context mContext; private Integer[] mThumbIds = { R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon, Lập trình Android – http://laptrinhdidong.vn Page 2 Trung tâm Tin học – ĐH KHTN R.drawable.icon, R.drawable.icon, }; } } Cuối cùng debug ứng dụng : Mọi ý kiến đóng góp các bạn vui lòng post bài lên forum của trang web www.laptrinhdidong.vn . Rất mong nhận được ý kiến đóng góp của các bạn. Mình sẽ cập nhật trang web thường xuyên Lập trình Android – http://laptrinhdidong.vn Page 3 . – ĐH KHTN Tạo Menu dạng GridView 1/ Các bạn tạo Project như sau: Project name: GridViewMenu Build Target: Google APIs Application name: GridViewMenu Package name: com.sample. GridViewMenu Create. xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/myGrid" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" android:verticalSpacing="10dp" . android:verticalSpacing="10dp" android:horizontalSpacing="10dp" android:numColumns="auto_fit" android:columnWidth="60dp" android:stretchMode="columnWidth" android:gravity="center" /> 3/