乐学成语2
2016-06-07 20:22
369 查看
9.TabHost控件的应用,美化activity_main.xml的主界面效果
10在res的values目录的string.xml文件下定义所需字符串
<pre class="html" name="code"><?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">HappyIdiom</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="title_activity_main">MainActivity</string>
<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>
<string name="animal">动物类</string>
<string-array name="category">
<item>动物类</item>
<item>自然类</item>
<item>人物类</item>
<item>季节类</item>
<item>数字类</item>
<item>寓言类</item>
<item>其他类</item>
</string-array>
<string name="title_activity_study">StudyActivity</string>
<string name="title_activity_study_animal">StudyAnimalActivity</string>
</resources>
11.编写活动MainActivity
16.activity包下新建StudyActivity
10在res的values目录的string.xml文件下定义所需字符串
<pre class="html" name="code"><?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">HappyIdiom</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="title_activity_main">MainActivity</string>
<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>
<string name="animal">动物类</string>
<string-array name="category">
<item>动物类</item>
<item>自然类</item>
<item>人物类</item>
<item>季节类</item>
<item>数字类</item>
<item>寓言类</item>
<item>其他类</item>
</string-array>
<string name="title_activity_study">StudyActivity</string>
<string name="title_activity_study_animal">StudyAnimalActivity</string>
</resources>
11.编写活动MainActivity
</pre><pre class="java" name="code"><pre class="java" name="code"><pre class="java" name="code">package cn.edu.bztc.happyidiom; import android.os.Bundle; import android.app.Activity; import android.app.TabActivity; import android.content.Intent; import android.view.Menu; import android.view.Window; import android.widget.TabHost; public class MainActivity extends TabActivity{ private TabHost tabHost; @Override protected void onCreate(Bundle savedInstanceState) { 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) { // TODO Auto-generated method stub tabHost.addTab(tabHost.newTabSpec(tag).setIndicator(getString(title_introduction), getResources().getDrawable(title_icon)).setContent(new Intent(this,ActivityClass))); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
12.对配置文件进行修改
<pre class="html" name="code"> <activity android:name="cn.edu.bztc.happyidiom.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
<span style="font-size:24px;">13.作为ListView适配器的适配类型,在entity包下新建Category</span>
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 void setName(String name) { this.name = name; } public int getImageId() { return imageId; } public void setImageId(int imageId) { this.imageId = imageId; } }
<span style="font-size:24px;">14.在layout下新建activity——study.xml文件,添加一个ListView控件,并在目录下新建category_item.xml LinearLayout布局下添加ImageView(修改id为category_image)和TextView(修改id为category_name)控件</span>
<span style="font-size:24px;">15.创建适配器</span>
<pre class="html" name="code">public class CategoryAdapter extends ArrayAdapter<Category>{ private int resourceId; public CategoryAdapter(Context context, int resource, List<Category> objects) { super(context, resource, objects); // TODO Auto-generated constructor stub resourceId = 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(resourceId,null); viewHolder=new ViewHolder(); viewHolder.categoryName=(TextView)view.findViewById(R.id.category_name); viewHolder.categoryImage=(ImageView) view.findViewById(R.id.category_image); view.setTag(viewHolder); }else { view=convertView; viewHolder=(ViewHolder)view.getTag(); } viewHolder.categoryImage.setImageResource(category.getImageId()); viewHolder.categoryName.setText(category.getName()); return view; } class ViewHolder{ ImageView categoryImage; TextView categoryName; } }
16.activity包下新建StudyActivity
<pre class="html" name="code">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); initCategories(); 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 Category category=categoryList.get(position); Toast.makeText(StudyActivity.this, category.getName(), Toast.LENGTH_LONG).show(); } }); } private void initCategories() { // TODO Auto-generated method stub } }
相关文章推荐
- 机房收费系统 问题(一)——机房运行准备工作
- redis-集群安装(2)变两重冗余为三重
- spring+springMVC集成(annotation方式)
- JQuery开发7-网页中禁用鼠标右键单击和文本选择
- 第九周项目一深复制体验(2)
- AngularJS 用promises和$q处理异步调用
- Decorator装饰者设计模式
- HDU 2191 多重背包转01背包
- KMP
- 给你一个长度为N的链表。N很大,但你不知道N有多大。你的任务是从这N个元素中随机取出k个元素
- C++ 笔记4:指针函数
- XML.可扩展标记语言.就知道你不知道英文全称
- volley(5) 参数total_remain:totalqty, data:[{ bar_status:XX , bar_code: "XX",bar_remain:XX, bar_whcode:"XX" , bar_prodcode:"XX",bar_id:XX,bar_location: "XX", pr_detail: "XX" , bar_batchcode:method:POST
- 求割边模板(tarjan)
- CentOS 6.5 64位下安装Redis
- 二维数组传递参数问题
- 浅解Linux的启动流程
- C#获取当前行号
- 构造函数
- 13周 阅读程序(2)