android 从零,Android从零开始(十三)

知识点:TabHost

效果图:

072d29365b0009f5d08681587bf7a14b.png

工程目录结构:

c3a896d5f21a7850933376eab1236dc7.png

步骤一、编写首页(核心代码如下):

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">

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical"

>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:scrollbars="none"

>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:id="@android:id/tabs"

>

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:id="@android:id/tabcontent"

>

步骤二、编写每个标签要显示的页面

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="lab1"

android:textSize="40sp"

/>

步骤三、主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

tabHost.setCurrentTab(++index);

}

}else if(e1.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

package="com.veryedu.tabhost"

android:versionCode="1"

android:versionName="1.0" >

android:minSdkVersion="11"

android:targetSdkVersion="17" />

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

android:name="com.veryedu.tabhost.MainActivity"

android:label="@string/app_name" >

源码下载请点这里:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值