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

源码下载请点这里:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要从零开始写Android的SystemUI,需要具备一些基本的知识和技能。首先,你需要熟悉Java编程语言和Android开发的基础知识。了解Android的Activity、Service、BroadcastReceiver等组件的用法和生命周期。 接下来,你需要理解SystemUI是Android系统的一部分,是用于管理和显示系统状态栏、导航栏等界面元素的应用程序。它需要与底层系统进行交互,并实现对其进行控制和更新。你可以参考Android源代码中的SystemUI模块,了解其结构和基本原理。 在编写SystemUI之前,可以先定义你的项目目标和需求。你可以思考你想实现的功能和界面样式。然后,创建一个新的Android项目,并启动一个新的进程用于运行SystemUI。你可以在AndroidManifest.xml文件中注册你的SystemUI服务组件。 接下来,你可以开始编写SystemUI的代码。你可以创建一个主Activity来加载和管理系统状态栏和导航栏的布局。可以使用Android提供的View类来创建和显示界面元素。你还可以利用Android系统提供的API来获取和更新系统状态信息。 在编写SystemUI时,你需要考虑到系统UI的响应性和性能。你可以使用线程和异步任务来处理后台操作,以避免阻塞UI线程。此外,你还可以使用Android的Notification和Broadcast机制与其他应用程序进行通信。 最后,你可以进行调试和测试你的SystemUI应用程序。你可以使用Android设备或模拟器来运行你的应用,并逐步验证功能的正确性和稳定性。 总结而言,从零开始编写Android的SystemUI需要对Android开发有一定的了解和经验。通过学习Android的基础知识、阅读源代码、分析需求和目标,你可以逐步构建一个可靠和功能丰富的SystemUI应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值