Android乐学成语之界面实现
来源:互联网 发布:windows7服务优化 编辑:程序博客网 时间:2024/04/30 12:57
主界面的设计采用选项卡组件,在res的drawable-hdpi目录下拷入需要的图片素材准备好文字资源,修改values中的strings.xml文件如下:
<?xml version="1.0" encoding="utf-8"?><resources><string-array name="category"> <item>动物类</item> <item>自然类</item> <item>人物类</item> <item>季节类</item> <item>数字类</item> <item>寓言类</item> <item>其它类</item> </string-array>
接着定义一个实体类,作为ListView适配器的适配类型,在entity新建类Category,代码如下所示:
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;}}
Category类中只有两个字段,name代表种类的名字,imageId代表类别对应图片的资源id,在layout下新建activity_study.xml文件,主要添加了一个ListView控件,代码如下:
<?xml version="1.0" encoding="utf-8"?><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:layout_alignParentLeft="true" android:layout_alignParentTop="true"> </ListView></RelativeLayout>
然后需要为ListView的子项指定一个我们自定义的布局,在layout目录下新建category_item.xml,代码如下所示:
<?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:padding="10dp" android:orientation="horizontal" > <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="@array/category" android:gravity="center" android:textAppearance="?android:attr/textAppearanceLarge"/></LinearLayout>在这个布局中,我们定义了一个ImageView用于显示类别的图片,又定义了一个TextView用于显示类别的名称。接下来需要在应用的包下创建adapter包,在该包下定义个自定义的适配器,这个适配器继承成ArrayAdapter,并将泛型指定为Category类,新建类CategoryAdapter,代码如下所示:
public class CategoryAdapter extends ArrayAdapter<Category>{private int resourceId;public CategoryAdapter(Context context, int resource, List<Category> objects) {super(context, resource, objects);resourceId = resource;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {Category category = getItem(position);//获取当前项的Category实例View view = LayoutInflater.from(getContext()).inflate(resourceId, null);ImageView categoryImage = (ImageView) view.findViewById(R.id.category_image);TextView categoryName = (TextView) view.findViewById(R.id.category_name);categoryImage.setImageResource(category.getImageId());categoryName.setText(category.getName());return view;}}
CategoryAdapter重写了父类的一组构造函数,用于将上下文、ListView子项布局的id和数据都传递进去。另外又重写了getView()方法,这个方法在每个子项被滚动到屏幕内的时候会被调用。在getView方法中,首先通过getItem()方法得到当前项的Category实例,然后使用LayoutInflater来为这个子项加载我们传入的布局,接着调用View的findViewById()方法分别获取到ImageView和TextView的实例,并分别调用它们的setImageResource()和setText()方法来设置显示的图片和文字,最后将布局返回,这样我们自定义的适配器就完成了。
下面在activity包下新建StudyActivity继承自Activity,代码如下所示:
public class StudyActivity extends Activity {private List<Category> categoryList;private String[] category_names;private int[] category_images;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_study);initCategories();// 初始化类别CategoryAdapter adapter = new CategoryAdapter(this,R.layout.category_item, categoryList);ListView listView = (ListView) findViewById(R.id.lvCategories);listView.setAdapter(adapter);}private void initCategories() {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]));}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.study, menu);return true;}}
可以看到,这里添加了一个initCategories()方法,用于初始化所有的类别数据。在Fruit类的构造函数中将类别的名字和对应的图片id传入,然后把创建好的对象添加到类别列表中。接着我们在onCreate()方法中创建了CategoryAdapter对象,并将CategoryAdapter作为适配器传递给了ListView。这样定制ListView界面的任务就完成了。
在运行程序之前,首先修改AndroidManifest.xml文件将StudyActivity变成入口类,现在重新运行程序,效果如图所示:
现在学习界面还是独立的,我们要将其与之前建立的主界面连接起来。
修改MainActivity,代码如下:
public class MainActivity extends TabActivity {private TabHost tabHost;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);// 取消标题栏setContentView(R.layout.activity_main);tabHost = getTabHost();addTab("study", R.string.title_study, R.drawable.study, StudyActivity.class);addTab("search", R.string.title_search, R.drawable.search, StudyActivity.class);addTab("game", R.string.title_game, R.drawable.game, StudyActivity.class);addTab("save", R.string.title_save, R.drawable.save, StudyActivity.class);addTab("help", R.string.title_help, R.drawable.help, StudyActivity.class);}private void addTab(String tag, int title_introduction, int title_icon,Class ActivityClass) {tabHost.addTab(tabHost.newTabSpec(tag).setIndicator(getString(title_introduction),getResources().getDrawable(title_icon)).setContent(new Intent(this,ActivityClass)));}public boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main, menu);return true;}}
通过Intent,将选项卡和对应的StudyActivity关联起来了,运行结果如图所示:
错误总结
1.如果大家在非黑色背景下使用ListView控件时,Android默认可能在滚动ListView时这个列表控件的背景突然变成黑色。这样可能导致程序的黑色的背景和主程序的主题既不协调
我一开始是在AndroidManifest.xml中使用
android:theme="@android:style/Theme.NoTitleBar"
之后仔细查了查意思是:背景主题的没有标题栏的样式,默认如果没有设置的话,显示黑背景
解决这样的问题:
那就换成android:theme="@android:style/Theme.Translucent.NoTitleBar
2.出现以下错误:这是不能实例化
java.lang.RuntimeException:Unable to instantiateactivity ComponentInfo
不能实例化activity有如下二种情况:
1.没有在Manifest.xml 清单中注册该activity,或者在创建完activity后,修改了包名或者activity的类名,而配置清单中没有修改,造成不能实例化
2.自己新建一个包,而配置的时候,使用默认包等
- Android乐学成语之界面实现
- 乐学成语实现之二显示主界面
- Android之乐学成语
- Android乐学成语的实现分析
- Android---乐学成语
- 乐学成语之splash
- 乐学成语之小游戏
- andriod之乐学成语
- 乐学成语之数据库
- 乐学成语3-界面美化
- 3.乐学成语----------显示主界面
- 学习Andriod之乐学成语的实现
- 乐学成语实现之四:显示所有动物类成语的列表
- 乐学成语实现之五:显示每条成语的详细信息
- Android开发教程(十三)乐学成语代码实现
- 学习Android开发-乐学成语之TabHost控件
- Android SQLite乐学成语项目之数据库存储
- 个人完成案列之乐学成语(显示主界面)
- 第十六周项目1——小玩文件
- 第十四周上机实践项目1(3):阅读程序
- 第14周程序阅读(2)
- 16周阅读程序(2)
- 在线程中使用HttpClicent获取网络数据(get和post两种方式)
- Android乐学成语之界面实现
- Fragment生命周期
- 16周阅读程序(3)
- 将SVN仓库里面的一个文件夹的所有内容复制到另一个文件夹下
- C++(基础班)上机复习
- Activity的生命周期
- 项目45-洗牌
- 第15周 程序阅读二
- 第十 十一周项目4-教师兼干部类