最近刚学习Andrioid 开发,昨日看到大牛级网友hellgv写的关于《Android入门第八篇之GridView(九宫图)》之后想到做一个模仿手机菜单界面的布局,于是有了今天下面的入门级作品,这个作品与大牛级网友hellgv写的作品最大的区别就是在菜单图片下面添加了不同文字,希望大家能多提建议,看怎么实现布局更加精美,共同勉励,共同进步,废话少说,先看效果图:
public class AndroidTest2_3_3 extends Activity { private static final String TAG = "AndroidTest2_3_3"; private Bitmap bit; private String textArray1[] = {"婺源", "丹霞山", "周庄"}; private String textArray2[] = {"油菜花盛开的地方", "红绿之间,雄壮与温柔", "梦中的水乡"}; /** Called when the activity is first created。 */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R。layout。main); String imagepath = Environment。getExternalStorageDirectory() + "/abc。jpg"; bit = BitmapFactory。decodeFile(imagepath); GridView gv=(GridView)this。findViewById(R。id。GridView01); Log。d(TAG, "+++++++++++1"); ImageAdapter ia = new ImageAdapter(this); gv。setAdapter(ia);//为GridView设置数据适配器 } /** * GridView适配器类,创建单元格view * @author ameyume * */ public class ImageAdapter extends BaseAdapter { private Context mContext; public ImageAdapter(Context context) { Log。d(TAG, "ImageAdapter 1"); mContext = context; } public int getCount() { return textArray1。length; } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { Log。d(TAG, "000000000000000"); View view = View。inflate(AndroidTest2_3_3。this, R。layout。grid_row, null); RelativeLayout rl = (RelativeLayout)view。findViewById(R。id。rlGridRow); ImageView image = (ImageView)rl。findViewById(R。id。imageView01); image。setImageBitmap(bit); TextView tv1 = (TextView)rl。findViewById(R。id。txtLink1); tv1。setText(textArray1[position]);
TextView tv2 = (TextView)rl。findViewById(R。id。txtLink2); tv2。setText(textArray2[position]); return rl; }
package cn.com.blog; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; import android.widget.TextView; public class MenuPanelActivity extends Activity { private GridView gridView; private int[] mImageIds = { R.drawable.calculator, R.drawable.camera, R.drawable.compass, R.drawable.ebook, R.drawable.email, R.drawable.games, R.drawable.map, R.drawable.message, R.drawable.multimedia, R.drawable.music, R.drawable.phone, R.drawable.radio, R.drawable.video, }; private int[] TitleTexts={ R.string.calculator, R.string.camera, R.string.compass, R.string.ebook, R.string.email, R.string.games, R.string.map, R.string.message, R.string.multimedia, R.string.music, R.string.phone, R.string.radio, R.string.video, };
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); gridView =(GridView)this.findViewById(R.id.GridViewId); gridView.setAdapter(new gridViewAdapter(mImageIds,TitleTexts)); }
public class gridViewAdapter extends BaseAdapter { private View[] itemViews; public gridViewAdapter(int[] mImageIds, int[] TitleTexts) { itemViews = new View[mImageIds.length]; for (int i = 0; i < itemViews.length; i++) { itemViews = makeItemView(mImageIds,TitleTexts); } } public int getCount() { return itemViews.length; } public View getItem(int position) { return itemViews[position]; }
public long getItemId(int position) { return position; } private View makeItemView(int strmImageIds, int strTitleTexts) { LayoutInflater inflater = (LayoutInflater) MenuPanelActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View itemView = inflater.inflate(R.layout.menuitem, null); TextView title = (TextView) itemView.findViewById(R.id.TextItemId); title.setText(strTitleTexts); ImageView image = (ImageView) itemView.findViewById(R.id.ImageItemId); image.setImageResource(strmImageIds); image.setScaleType(ImageView.ScaleType.FIT_CENTER); return itemView; } public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) return itemViews[position]; return convertView; } } }
main.xml代码如下:
<GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/GridViewId" android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="auto_fit" android:verticalSpacing="10dip" android:horizontalSpacing="10dip" android:paddingRight="5dip"
android:columnWidth="90dip" android:stretchMode="columnWidth" android:gravity="center" />
menuitem.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="wrap_content"> <ImageView android:layout_height="wrap_content" android:id="@+id/ImageItemId" android:layout_width="wrap_content" android:layout_centerHorizontal="true"> </ImageView> <TextView android:layout_width="wrap_content" android:layout_below="@+id/ImageItemId" android:layout_height="wrap_content" android:text="TextView" android:layout_centerHorizontal="true" android:id="@+id/TextItemId"> </TextView> </RelativeLayout>
strings.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">MenuPanel</string>
<string name="calculator">calculator</string>
<string name="camera">camera</string>
<string name="compass">compass</string>
<string name="ebook">ebook</string>
<string name="email">email</string>
<string name="games">games</string>
<string name="map">map</string>
<string name="message">message</string>
<string name="multimedia">multimedia</string>
<string name="music">music</string>
<string name="phone">phone</string>
<string name="radio">radio</string>
<string name="video">video</string>
</resources>
}
}