一步一步写News App(一)
一、 新建一个安卓工程,安卓版本全部选2.3.3
二、第一步,添加一个tabhost控件
在MainActivity中声明TabHost tabHost; 然后新建一个private void initViews()函数,在函数中新建tabHost = getTabHost();提示出错,没有getTabHost()方法,将MainActivity extends Activity 修改成MainActivity extends TabActivity 就可以了。
然后在initViews()中添加每个标签页的内容。代码如下:
private void initViews(){
tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("news").setIndicator("News").setContent(new Intent(this, TabNewsActivity.class)));
tabHost.addTab(tabHost.newTabSpec("mood").setIndicator("Mood").setContent(new Intent(this, TabMoodActivity.class)));
tabHost.addTab(tabHost.newTabSpec("user").setIndicator("User").setContent(new Intent(this, TabUserActivity.class)));
tabHost.addTab(tabHost.newTabSpec("find").setIndicator("Find").setContent(new Intent(this, TabFindActivity.class)));
tabHost.addTab(tabHost.newTabSpec("setting").setIndicator("Setting").setContent(new Intent(this, TabSettingActivity.class)));
}
Intent用来实现Activity之间的跳转,new Intent(this, XXActivity.class)表示从当前(this)Activity跳转到XXActivity.class。
这样写完会提示后面5个参数页面不存在,然后就开始先把这5个Activity的框架写好,分别新建5个类,名称跟上面设计的一样。如图:

每个Activity文件先弄个最简单的,继承自Activity类,像这样:
package com.yhy.news; import android.app.Activity;
import android.os.Bundle; public class TabFindActivity extends Activity{ @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.find);
}
}
注意就是一要extends Activity, 二要重写onCreate函数,三要在onCreate函数里加上setContentView(R.layout.find)。其他的分别setContentView如下:
R.layout.mood, R.layout.setting, R.layout.user, R.layout.news。当然这些layout文件都需要我们自己新建,内容可以就放一下LinearLayout,以后再扩展,
新建好后的layout如下图:

MainActivity的布局文件activity_main.xml文件中填入以下内容:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"> <TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="1.0" >
</FrameLayout> <TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" /> <RelativeLayout
android:id="@+id/layout_bottom"
android:layout_width="fill_parent"
android:layout_height="wrap_content" > <RadioGroup
android:id="@+id/radiogroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@drawable/bottombg"
android:gravity="center_vertical"
android:orientation="horizontal" > <RadioButton
android:id="@+id/radio_news"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/tab_selector_news"
android:button="@null"
android:layout_weight="1"
android:checked="true" /> <RadioButton
android:id="@+id/radio_mood"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/tab_selector_mood"
android:layout_weight="1"
android:button="@null" /> <RadioButton
android:id="@+id/radio_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/tab_selector_user"
android:layout_weight="1"
android:button="@null" /> <RadioButton
android:id="@+id/radio_find"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/tab_selector_find"
android:layout_weight="1"
android:button="@null" /> <RadioButton
android:id="@+id/radio_setting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/tab_selector_setting"
android:layout_weight="1"
android:button="@null" />
</RadioGroup>
</RelativeLayout>
</LinearLayout>
</TabHost> </RelativeLayout>
该布局文件是一个标准的tabhost的布局文件,tabwidget标签中定义下方的几个选项卡,用5个radiobutton定义,每个按钮的背景设置为不同的xml文件,每个xml文件里定义着选中和未选中时的图片文件, 如 tab_selector_setting.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/current_setting_tab" android:state_checked="true"/>
<item android:drawable="@drawable/back_setting_tab" android:state_checked="false"/> </selector>
其中, current_setting_tab图片如下:

back_setting_tab图片如下:

其他图片资源:











都是png格式的。这样就可以在选中和未选中时显示不同的样子。
资源文件结构如下:

(drawable文件夹是自己新建的)。
准备好以上内容后,运行程序,会报错,看logcat中会有错误提示,说manifest中没有注册Activity。
于是在AndroidManifest.xml文件中将上面添加的5个Activity注册一下,在与MainActivity平行的地方添加如下内容:
<activity
android:name="com.yhy.news.TabNewsActivity"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar" >
</activity> <activity
android:name="com.yhy.news.TabFindActivity"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar" >
</activity> <activity
android:name="com.yhy.news.TabMoodActivity"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar" >
</activity> <activity
android:name="com.yhy.news.TabSettingActivity"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar" >
</activity> <activity
android:name="com.yhy.news.TabUserActivity"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar" >
</activity>
其中,name里面的是Activity完整包路径名。
经过上面的准备后终于可以运行起来了!效果如图:

一步一步写News App(一)的更多相关文章
- Android 从硬件到应用程序:一步一步爬上去 6 -- 我写的APP测试框架层硬件服务(终点)
创Android Applicationproject:采用Eclipse的Android插入ADT创Androidproject,project名字Gpio,创建完成后,project文件夹pack ...
- .NET跨平台:在Mac上跟着错误信息一步一步手写ASP.NET 5程序
今天坐高铁时尝试了一种学习ASP.NET 5的笨方法,从空文件夹开始,根据运行dnx . kestrel命令的错误信息,一步一步写代码,直至将一个最简单的ASP.NET程序运行起来. 尝试的具体步骤如 ...
- 一步一步写平衡二叉树(AVL树)
平衡二叉树(Balanced Binary Tree)是二叉查找树的一个进化体,也是第一个引入平衡概念的二叉树.1962年,G.M. Adelson-Velsky 和 E.M. Landis发明了这棵 ...
- 《一步一步写嵌入式操作系统》读书笔记1—Skyeye介绍、安装和HelloWorld
2013-11-14 最近在看<一步一步写嵌入式操作系统>,感觉此书甚好,许多地方讲得很清楚.可操作性强,计划边读边实践边写笔记,希望能够逐步熟悉嵌入式操作系统底层的东西,最终剪裁出一套实 ...
- 一步一步写一个简单通用的makefile(三)
上一篇一步一步写一个简单通用的makefile(二) 里面的makefile 实现对通用的代码进行编译,这一章我将会对上一次的makefile 进行进一步的优化. 优化后的makefile: #Hel ...
- Python之美[从菜鸟到高手]--一步一步动手给Python写扩展(异常处理和引用计数)
我们将继续一步一步动手给Python写扩展,通过上一篇我们学习了如何写扩展,本篇将介绍一些高级话题,如异常,引用计数问题等.强烈建议先看上一篇,Python之美[从菜鸟到高手]--一步一步动手给Pyt ...
- 一步一步写算法(之prim算法 下)
原文:一步一步写算法(之prim算法 下) [ 声明:版权所有,欢迎转载,请勿用于商业用途. 联系信箱:feixiaoxing @163.com] 前两篇博客我们讨论了prim最小生成树的算法,熟悉 ...
- 一步一步写算法(之prim算法 中)
原文:一步一步写算法(之prim算法 中) [ 声明:版权所有,欢迎转载,请勿用于商业用途. 联系信箱:feixiaoxing @163.com] C)编写最小生成树,涉及创建.挑选和添加过程 MI ...
- 一步一步写算法(之prim算法 上)
原文:一步一步写算法(之prim算法 上) [ 声明:版权所有,欢迎转载,请勿用于商业用途. 联系信箱:feixiaoxing @163.com] 前面我们讨论了图的创建.添加.删除和保存等问题.今 ...
- 一步一步写算法(之挑选最大的n个数)
原文:一步一步写算法(之挑选最大的n个数) [ 声明:版权所有,欢迎转载,请勿用于商业用途. 联系信箱:feixiaoxing @163.com] 从一堆数据中挑选n个最大的数,这个问题是网上流传的 ...
随机推荐
- Linux中split大文件分割和cat合并文件
当需要将较大的数据上传到服务器,或从服务器下载较大的日志文件时,往往会因为网络或其它原因而导致传输中断而不得不重新传输.这种情况下,可以先将大文件分割成小文件后分批传输,传完后再合并文件. 1.分割 ...
- [IMX6DL] CPU频率调节模式以及降频方法
本文转自http://blog.csdn.net/kris_fei/article/details/51822435 Kernel branch: 3.0.35 CPU的频率调节模式:1. Perfo ...
- JMeter学习-参数化
JMeter也有像LR中的参数化,本篇就来介绍下JMeter的参数化如何去实现. 参数化:录制脚本中有登录操作,需要输入用户名和密码,假如系统不允许相同的用户名和密码同时登录,或者想更好的模拟多个用户 ...
- Intel AI Cloud 使用
1.申请AI Cloud A ‘training-ready’ hardware like Amazon® EC2, Intel® AI DevCloud, or a GPU-based system ...
- Ionic4.x 新增底部 tabs 页面
1.创建 tab4 模块 ionic g page tab4 2.修改根目录里 app-routing.module.ts 文件里面的路由配置,去掉默认增加的路由 3.tabs.router.modu ...
- android滑动标题栏渐变实现
import android.graphics.Color; import android.os.Build; import android.os.Bundle; import android.sup ...
- Mysql备份工具mysqldump和mysqlhotcopy
(1).Mysql备份类型 1)按照备份时对数据库的影响分为 Hot backup(热备):也叫在线备份.指在数据库运行中直接备份,对正在运行的数据库没有任何影响. Cold backup(冷备):也 ...
- 简介Python设计模式中的代理模式与模板方法模式编程
简介Python设计模式中的代理模式与模板方法模式编程 这篇文章主要介绍了Python设计模式中的代理模式与模板方法模式编程,文中举了两个简单的代码片段来说明,需要的朋友可以参考下 代理模式 Prox ...
- FastCGI模式编译安装LAMP+Xcache
PHP的工作模式:php在lamp环境下共有三种工作模式:CGI模式.apache模块.FastCGI模式.CGI模式下运行PHP,性能不是很好.(已淘汰)FastCGI的方式和apache模块的不同 ...
- Golang 开发框架 gin 项目时笔记
1.模板引入时报错: func main() { router := gin.Default() router.LoadHTMLGlob("templates/**/*") rou ...