androidtabhost缓存_Android学习笔记(一):TabHost存放多个Activity
TabHost是android應(yīng)用開發(fā)中非常常用的組件,他能起到類似web開發(fā)中菜單導(dǎo)航的效果。
基本概念:
TabHost:TabHost就像一個容器,里面可以存放多個Tab。
tabHost.addTab(tabSpec);//此方法用于將tab添加到tabHost。
TabSpec:就是Tab,這個類沒有對外提供構(gòu)造函數(shù)(不能new),我們需要通過tabHost.newTabSpec("TS_HOME")來實例化TabSpec,參數(shù)用于識別和區(qū)分多個Tab,就像每個人都會有一個名字。通過TabSpec我們可以設(shè)置Tab的圖標(biāo)、Tab上顯示的文字,還有Tab的內(nèi)容。
tabSpec.setIndicator("主頁", getResources().getDrawable(R.drawable.tab_home));//此方法用于設(shè)置Tab的文字和圖標(biāo)。
tabSpec.setContent(new Intent(this,HomeActivity.class));//此方法用于設(shè)置Tab的內(nèi)容,此方法有多種參數(shù)形式,本文主要講Tab的內(nèi)容為Activity。
程序?qū)嵗?#xff1a;
效果圖
代碼:
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class TabHost1Activity extends TabActivity {
TabHost tb;
/**Called when the activity is first created.*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tb = this.getTabHost();
TabSpec tsHome = tb.newTabSpec("TS_HOME")
.setIndicator("主頁", getResources().getDrawable(R.drawable.tab_home))
.setContent(new Intent(this,HomeActivity.class));
tb.addTab(tsHome);
TabSpec tsGroupOn = tb.newTabSpec("TS_GROUPON")
.setIndicator("團(tuán)購信息", getResources().getDrawable(R.drawable.tab_groupon))
.setContent(new Intent(this,GroupOnActivity.class));
tb.addTab(tsGroupOn);
TabSpec tsUserInfo = tb.newTabSpec("TS_USERINFO")
.setIndicator("個人中心", getResources().getDrawable(R.drawable.tab_userinfo))
.setContent(new Intent(this,UserInfoActivity.class));
tb.addTab(tsUserInfo);
TabSpec tsMore = tb.newTabSpec("TS_MORE")
.setIndicator("更多", getResources().getDrawable(R.drawable.tab_more))
.setContent(new Intent(this,MoreActivity.class));
tb.addTab(tsMore);
}
}
簡簡單單幾句代碼TabHost存放多個Activity就實現(xiàn)了。
總結(jié)
以上是生活随笔為你收集整理的androidtabhost缓存_Android学习笔记(一):TabHost存放多个Activity的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python支持list类型吗_Pyth
- 下一篇: android sina oauth2.