- 浏览: 61665 次
- 性别:
- 来自: 湖南
最新评论
-
Neacy_Zz:
有些Android也可以显示的出来。
Android 将一张CMYK模式的图片转换成RGB模式的图片 -
hold_on:
AsyncTask 2.3 和 4.0稍有不同
Android从零开始(十五)
知识点:TabHost
效果图:
工程目录结构:
步骤一、编写首页(核心代码如下):
步骤二、编写每个标签要显示的页面
步骤三、主Activity代码编写:
步骤四、其他Activity的编写:
步骤五、注册Activity
源码下载请点这里:
效果图:
工程目录结构:
步骤一、编写首页(核心代码如下):
<TabHost 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" tools:context=".MainActivity" android:id="@android:id/tabhost"> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <HorizontalScrollView android:layout_width="fill_parent" android:layout_height="wrap_content" android:scrollbars="none" > <TabWidget android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@android:id/tabs" ></TabWidget> </HorizontalScrollView> <FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@android:id/tabcontent" ></FrameLayout> </LinearLayout> </TabHost>
步骤二、编写每个标签要显示的页面
<?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:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="lab1" android:textSize="40sp" /> </LinearLayout>
步骤三、主Activity代码编写:
package com.veryedu.tabhost; import android.os.Bundle; import android.app.Activity; import android.app.TabActivity; import android.content.Intent; import android.view.GestureDetector; import android.view.GestureDetector.OnGestureListener; import android.view.Menu; import android.view.MotionEvent; import android.widget.TabHost; import android.widget.TabHost.TabSpec; import android.widget.TabWidget; public class MainActivity extends TabActivity implements OnGestureListener { GestureDetector gestureDetector; static TabHost tabHost; static int index = 0; int count; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tabHost = getTabHost(); // 添加选项卡的标题和内容 TabSpec spec1 = tabHost.newTabSpec("lab1"); spec1.setIndicator("选项卡一"); spec1.setContent(new Intent(this, Tab1Activity.class)); tabHost.addTab(spec1); TabSpec spec2 = tabHost.newTabSpec("lab2"); spec2.setIndicator("选项卡二"); spec2.setContent(new Intent(this, Tab2Activity.class)); tabHost.addTab(spec2); TabSpec spec3 = tabHost.newTabSpec("lab3"); spec3.setIndicator("选项卡三"); spec3.setContent(new Intent(this, Tab3Activity.class)); tabHost.addTab(spec3); TabSpec spec4 = tabHost.newTabSpec("lab4"); spec4.setIndicator("选项卡四"); spec4.setContent(new Intent(this, Tab4Activity.class)); tabHost.addTab(spec4); TabSpec spec5 = tabHost.newTabSpec("lab5"); spec5.setIndicator("选项卡五"); spec5.setContent(new Intent(this, Tab5Activity.class)); tabHost.addTab(spec5); TabSpec spec6 = tabHost.newTabSpec("lab6"); spec6.setIndicator("选项卡六"); spec6.setContent(new Intent(this, Tab6Activity.class)); tabHost.addTab(spec6); //获取控件数量 TabWidget tabWidget=(TabWidget)findViewById(android.R.id.tabs); count=tabWidget.getChildCount(); gestureDetector=new GestureDetector(this); } @Override public boolean onTouchEvent(MotionEvent event) { gestureDetector.onTouchEvent(event); return super.onTouchEvent(event); } @Override public boolean onDown(MotionEvent e) { // TODO Auto-generated method stub return false; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { if(e1.getX()>e2.getX()){ //向左滑动,选项卡前进到下一个 if(index<count){ tabHost.setCurrentTab(++index); } }else if(e1.getX()<e2.getX()){ //向右滑动,选项卡后退到前一个 if(index>0){ tabHost.setCurrentTab(--index); } } return false; } @Override public void onLongPress(MotionEvent e) { // TODO Auto-generated method stub } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // TODO Auto-generated method stub return false; } @Override public void onShowPress(MotionEvent e) { // TODO Auto-generated method stub } @Override public boolean onSingleTapUp(MotionEvent e) { // TODO Auto-generated method stub return false; } }
步骤四、其他Activity的编写:
package com.veryedu.tabhost; import android.app.Activity; import android.os.Bundle; public class Tab1Activity extends Activity { @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); MainActivity.index=0; } @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.lab1); } }
步骤五、注册Activity
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.veryedu.tabhost" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.veryedu.tabhost.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> <activity android:name=".Tab1Activity"></activity> <activity android:name=".Tab2Activity"></activity> <activity android:name=".Tab3Activity"></activity> <activity android:name=".Tab4Activity"></activity> <activity android:name=".Tab5Activity"></activity> <activity android:name=".Tab6Activity"></activity> </application> </manifest>
源码下载请点这里:
发表评论
-
Android自定义控件--3(刷新动画)
2014-02-26 11:14 945一个乐动力的刷新动画 package com.example. ... -
Android自定义控件--2(数字签名)
2014-02-26 11:08 918客户信息交互用到的一个数字签名,保存的是bmp图片(我那会在P ... -
Android自定义控件--1(TextView跑马灯效果)
2014-02-26 10:57 1554N久没有跟新了,最近 ... -
Android 将一张CMYK模式的图片转换成RGB模式的图片
2013-06-25 17:32 2213Android系统是不能够显示打印模式(CMYK)的图片的。所 ... -
Android TextToSpeech语音播放文本
2013-06-25 17:17 1850步骤一、初始化 package com.example.spe ... -
Android连接电脑eclipse无法识别=解决办法
2013-05-23 09:25 21391、方法一:“我的电脑”-->“属性”-->“设备 ... -
Android BaseAdapter如何获得每一项并添加按钮事件
2013-05-21 15:11 3733package com.hangcheng; impor ... -
Android的VideoView如何循环播放
2013-05-15 14:01 4831package com.hangcheng; impor ... -
Android如何获取asset目录下所有文件的路径
2013-05-15 09:48 7690package com.hangcheng.util; ... -
Android--WebView+HTML+Javascript
2013-04-09 17:17 1708知识点:android的WebView控件加载HTML并通过j ... -
VideoView重绘
2013-04-01 23:39 1327知识点:Android中的VideoView控件默认情况下是不 ... -
Android从零开始—多媒体(二十二)
2013-03-06 21:43 1030知识点:音乐播放、录音、播放视频、录像、拍照 步骤一、主界面 ... -
Android从零开始—监听广播BroadcastReceiver(二十一)
2013-03-05 10:17 2081知识点:电话拒接+短息 ... -
Android从零开始—电话+短信(二十)
2013-03-05 10:03 1137知识点:系统电话号码和短信的获取(最好用真机,模拟器上木有电话 ... -
Android从零开始(十九)
2013-02-27 10:35 959知识点:back键的处理(有时候我们可能会不小心按了回退键,但 ... -
Android从零开始(十八)
2013-02-27 10:24 836知识点:菜单(menu) 步骤一、创建菜单项(核心代码如下) ... -
Android从零开始(十七)
2013-02-27 10:09 941知识点:抽屉(SlidingDrawer) 先看一下抽屉打开 ... -
Android从零开始(十六)
2013-02-26 19:43 840知识点:闪屏(通常在 ... -
Android从零开始(十五)
2013-02-26 18:32 1085知识点:异步任务AsyncTa ... -
Android从零开始(十四)
2013-02-26 17:16 833知识点:TabHost选项卡之 ...
相关推荐
《Android开发从零开始视频教程》下载地址,常期可用! 文件大小:2.3GB 文件格式:MP4 视频内容: [Android开发从零开始].1.Android开发环境搭建.mp4 30.6MB [Android开发从零开始].2.模拟器的使用.mp4 20.8MB ...
从零开始 Android 编程
android从零开始 若水 源代码 43课时 全有
从零开始android游戏编程 包括源码
NULL 博文链接:https://1136051009.iteye.com/blog/1777096
NULL 博文链接:https://1136051009.iteye.com/blog/1815762
基于经典的坦克大战游戏,学习中不失乐趣。 通过整个游戏开发过程从零开始学习Android开发
Android开发从零开始课件源代码
Android开发从零开始源码,很好的资源,对初学者很有用!
从零开始学Android 编程 [曾宏远,崔荔蒙 编著] 2012年版.rar,适用于对安卓手机编程开发有兴趣的初学者。
《从零开始Android游戏编程》(第二版)PDF文档,Android游戏开发入门教程。
[Android开发从零开始].1.Android开发环境搭建
最详细"从零开始搭建架构实施Android项目指导文档
Android刷新时数字从零开始刷新
从零开始android游戏编程
从零开始学Android编程源代码 02-10章的源代码,欢迎下载
从零开始学Android编程
对于没有基础的java和android也可以从零开始。
[Android开发从零开始].9.Activity高级学习