1.准备好文字资源,修改values里的strings.xml文件。
<span style="font-size:18px;"><string name="title_search">搜搜</string>
<string name="title_study">学习</string>
<string name="title_game">游戏</string>
<string name="title_save">收藏</string>
<string name="title_help">帮助</string></span>
2.定义一个实体类Category。
<span style="font-size:18px;">package com.edu.happyidiom.entity;
public class Category {
private String name;
private int imageId;
public Category(String name, int imageId) {
super();
this.name = name;
this.imageId = imageId;
}
public String getName() {
return name;
}
public int getImageId() {
return imageId;
}
}</span>
3.新建一个activity_study.xml文件,添加一个ListView控件。
<span style="font-size:18px;"><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:background="@drawable/bg_ling"
tools:context=".StudyActivity" >
<ListView
android:id="@+id/lvCategories"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutAnimation="@anim/anim_layout_listview"
android:listSelector="#00000000"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" >
</ListView>
</RelativeLayout></span>
4.为ListView的子项添加一个我们自定义的布局category_item.xml。
<span style="font-size:18px;"><?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="horizontal"
android:padding="10dp"
>
<ImageView
android:id="@+id/category_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/category_animal"
/>
<TextView
android:id="@+id/category_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/animal"
android:gravity="center"
android:textAppearance="?@android:attr/textAppearanceLarge"
/>
</LinearLayout></span>
5.创建一个适配器,将Category_item.xml和ListView适配。
<span style="font-size:18px;">package com.edu.happyidiom.adapter;
import java.util.List;
import com.edu.happyidiom.entity.Category;
import com.example.happyidiom.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CategoryAdapter extends ArrayAdapter<Category>{
private int resourceld;
public CategoryAdapter(Context context, int resource,
List<Category> objects) {
super(context, resource, objects);
// TODO Auto-generated constructor stub
resourceld=resource;
}
public View getView(int position,View convertView,ViewGroup parent){
Category category=getItem(position);
View view;
ViewHolder viewHolder;
if(convertView==null){
view=LayoutInflater.from(getContext()).inflate(resourceld, null);
viewHolder=new ViewHolder();
viewHolder.categoryImage=(ImageView) view.findViewById(R.id.category_image);
viewHolder.categoryName=(TextView) view.findViewById(R.id.category_name);
view.setTag(viewHolder);
}else{
view=convertView;
viewHolder=(ViewHolder) view.getTag();
}
</span><div style="text-align: center;"><span style="font-family: Arial, Helvetica, sans-serif;">}</span></div> viewHolder.categoryImage.setImageResource(category.getImageId());
viewHolder.categoryName.setText(category.getName());
return view;
}
class ViewHolder{
ImageView categoryImage;
TextView categoryName;
}
6.编写StudyActivity类继承自Activity。
<span style="font-size:18px;">package com.example.happyidiom;
import java.util.ArrayList;
import java.util.List;
import com.edu.happyidiom.adapter.CategoryAdapter;
import com.edu.happyidiom.entity.Category;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
public class StudyActivity extends Activity {
private List<Category> categoryList;
private String[] category_names;
private int[] category_images;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_study);
initCategory();
CategoryAdapter adapter=new CategoryAdapter(this, R.layout.category_item, categoryList);
ListView listView=(ListView) findViewById(R.id.lvCategories);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position,
long id) {
// TODO Auto-generated method stub
switch(position){
case 0:
Intent intent=new Intent(StudyActivity.this,StudyAnimalActivity.class);
startActivity(intent);
break;
case 1:
Intent intent1=new Intent(StudyActivity.this,StudyNatureActivity.class);
startActivity(intent1);
break;
default:
break;
}
}
});
}
private void initCategory() {
// TODO Auto-generated method stub
categoryList=new ArrayList<Category>();
Resources resources=getResources();
category_names=resources.getStringArray(R.array.category);
category_images=new int[]{R.drawable.category_animal,
R.drawable.category_nature,R.drawable.category_human,
R.drawable.category_season,R.drawable.category_number,
R.drawable.category_fable,R.drawable.category_other
};
for(int i=0;i<category_names.length;i++){
categoryList.add(new Category(category_names[i], category_images[i]));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.study, menu);
return true;
}
}</span>
下面将StudyActivity类变成入口类,运行该程序,就会出现以下的界面