实现效果:
项目结构:
activity_main.xml中直接拖入tabhost控件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.zhh.android.ShiZhongLinearLayout
android:id="@+id/shizhong"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/shizhong_tv_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp"
android:gravity="center"
/>
</com.zhh.android.ShiZhongLinearLayout>
<LinearLayout
android:id="@+id/naozhong"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="@+id/miaobiao"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="@+id/jishiqi"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
MainActivity中添加控件:
package com.zhh.android;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TabHost;
public class MainActivity extends Activity {
private TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//tabHost实例化,并初始化赋值
tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
tabHost.addTab(tabHost.newTabSpec("shizhong").setIndicator("时钟")
.setContent(com.zhh.android.R.id.shizhong));
tabHost.addTab(tabHost.newTabSpec("naozhong").setIndicator("闹钟")
.setContent(com.zhh.android.R.id.naozhong));
tabHost.addTab(tabHost.newTabSpec("miaobiao").setIndicator("秒表")
.setContent(com.zhh.android.R.id.miaobiao));
tabHost.addTab(tabHost.newTabSpec("jishiqi").setIndicator("计时器")
.setContent(com.zhh.android.R.id.jishiqi));
}
}
ShiZhongLinearLayout中:
package com.zhh.android;
import java.util.Calendar;
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* 3实现显示Android时钟功能
*
*
*/
public class ShiZhongLinearLayout extends LinearLayout {
/**
* 必须重写三个构造方法
*/
public ShiZhongLinearLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public ShiZhongLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ShiZhongLinearLayout(Context context) {
super(context);
}
private TextView shizhong_tv_content;
/**
*
* 初始化方法,相当于activity中的onCreate
*/
@Override
protected void onFinishInflate() {
shizhong_tv_content = (TextView) findViewById(com.zhh.android.R.id.shizhong_tv_content);
shizhong_tv_content.setText("Hello...");
handler.sendEmptyMessage(0);//发送空消息
}
/**
* 显示,并刷新时间
*/
private void refreshTime() {
//显示时间
Calendar c = Calendar.getInstance();
shizhong_tv_content.setText(String.format("%d:%d:%d", c.get(Calendar.HOUR_OF_DAY),
c.get(Calendar.MINUTE), c.get(Calendar.SECOND)));
}
/**
* Handler接收消息
*/
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
refreshTime();
//判断视图可见
if (getVisibility() == View.VISIBLE) {
handler.sendEmptyMessageDelayed(0, 1000);//延时1秒发送,自己给自己发消息(在looper循环中发送)
}
};
};
/**
*
* 可见状态发生改变
*/
protected void onVisibilityChanged(View changedView, int visibility) {
//判断可见
if (getVisibility() == View.VISIBLE) {
handler.sendEmptyMessage(0);
} else {
handler.removeMessages(0);//移除所有的额消息
}
};
}
源码下载:
http://download.csdn.net/detail/zhaihaohao1/8780751
tabhost的相关文章:
http://blog.csdn.net/deng_hui_long/article/details/12253299?utm_source=tuicool