Android乐学成语之界面实现

主界面的设计采用选项卡组件,在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;
	}
	@Override
	public 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;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.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]));
		}
	}

	@Override
	public 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;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.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.自己新建一个包,而配置的时候,使用默认包等








  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值