一步一步写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个最大的数,这个问题是网上流传的 ...
随机推荐
- ELK 二进制安装并收集nginx日志
对于日志来说,最常见的需求就是收集.存储.查询.展示,开源社区正好有相对应的开源项目:logstash(收集).elasticsearch(存储+搜索).kibana(展示),我们将这三个组合起来的技 ...
- Redis Sentinel 高可用服务架构搭建
https://www.cnblogs.com/xishuai/p/redis-sentinel.html
- linux下使用SVN上传项目
linux下使用SVN上传项目 摘自:https://blog.csdn.net/puppet_/article/details/78259591 2017年10月17日 13:51:33 puppe ...
- LeetCode_121. Best Time to Buy and Sell Stock
121. Best Time to Buy and Sell Stock Easy Say you have an array for which the ith element is the pri ...
- 小程序下载canvas生成图片
save_share_img:function(img){ var that = this; let { result } = that.data; getData.getData( "sa ...
- 从成员函数指针生成可调用对象:function<>、mem_fn()和bind()
我们知道,普通函数指针是一个可调用对象,但是成员函数指针不是可调用对象.因此,如果我们想在一个保存string的vector中找到第一个空string,不能这样写: vector<string& ...
- iOS的多线程技术
iOS的三种多线程技术 1.NSThread 每个NSThread对象对应一个线程,量级较轻(真正的多线程) 2.以下两点是苹果专门开发的“并发”技术,使得程序员可以不再去关心线程的具体使用问题 ØN ...
- 计数器+打卡+习惯+目标APP推荐
目录 一.计数器类APP推荐 1.1. Thing Counter - Google Play 上的应用 1.2. Counter - Apps on Google Play 1.3. Counter ...
- [转载]Oracle中动态SQL详解
1.静态SQLSQL与动态SQL Oracle编译PL/SQL程序块分为两个种:其一为前期联编(early binding),即SQL语句在程序编译期间就已经确定,大多数的编译情况属于这种类型:另外一 ...
- 阿里云RocketMQ的消费者简单实现
业务场景之类的请看另一篇生产者的实现: package com.ttt.eee; import com.aliyun.openservices.ons.api.Action; import com.a ...