日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

TabSpec与TabHost

發(fā)布時間:2025/6/15 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 TabSpec与TabHost 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Android游戲開發(fā)系統(tǒng)控件-TabSpec與TabHost

2012/5/12 星期六

5.12 汶川地震四周年,四年了,時間飛快,再大的苦難都屬于過去,現(xiàn)在只著眼于眼前,把握現(xiàn)在,能讓自己過得開心不后悔就行了。加油吧!!!

今天學習了另一個比較特殊的控件:TabSpec(分頁),TabHost(分頁的集合)

TabHost相當于瀏覽器中分頁的集合,而TabSpec則相當于瀏覽器中的每個分頁;在Android中,每一個TabSpec分頁可以是一個組件,也可以是一個布局,然后將每個分頁裝入TabHost中,TabHost即可將其中的每個分頁一并顯示出來。

創(chuàng)建項目:TabProject

向項目資源中添加了兩張圖片資源:bg.png與bg2.png.

作者:wwj

功能:實現(xiàn)在布局中進行頁面切換

項目運行結果截圖:

??????????

?

?

?

?

修改代碼:

=>>布局文件main.xml

[html]?view plaincopy
  • <?xml?version="1.0"?encoding="utf-8"?>??
  • <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  • ????android:layout_width="fill_parent"??
  • ????android:layout_height="fill_parent"??
  • ????android:orientation="vertical"???
  • ????android:background="@drawable/bg2">??
  • ????<Button???
  • ????????android:layout_width="fill_parent"??
  • ????????android:layout_height="wrap_content"??
  • ????????android:text="@string/btn1"??
  • ????????android:id="@+id/btn1"??
  • ????????/>??
  • ????<EditText???
  • ????????android:layout_width="fill_parent"??
  • ????????android:layout_height="wrap_content"??
  • ????????android:text="@string/et1"??
  • ????????android:id="@+id/et1"??
  • ????????/>??
  • ????<LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  • ????????android:orientation="vertical"??
  • ????????android:layout_width="fill_parent"??
  • ????????android:layout_height="fill_parent"??
  • ????????android:id="@+id/mylayout"??
  • ????????android:background="@drawable/bg"??
  • ????????>??
  • ????????<Button??
  • ????????????android:layout_width="fill_parent"??
  • ????????????android:layout_height="wrap_content"??
  • ????????????android:text="@string/btn2"??
  • ????????????/>??
  • ????????<EditText??
  • ????????????android:layout_width="fill_parent"??
  • ????????????android:layout_height="wrap_content"??
  • ????????????android:text="@string/et2"??
  • ????????????/>??
  • ????</LinearLayout>??
  • ??
  • </LinearLayout>??
  • ?

    ?

    ?

    =>>string.xml

    [html]?view plaincopy
  • <?xml?version="1.0"?encoding="utf-8"?>??
  • <resources>??
  • ??
  • ????<string?name="hello">Hello?World,?TabProjectActivity!</string>??
  • ????<string?name="app_name">TabProject</string>??
  • ????<string?name="btn1">This?is?Tab1</string>??
  • ????<string?name="btn2">This?is?Tab3</string>??
  • ????<string?name="et1">This?is?Tab2</string>??
  • ????<string?name="et2">This?is?Tab3</string>??
  • ??
  • </resources>??

  • ?

    =>>TabProjectActivity.java

    [java]?view plaincopy
  • package?com.tabHost;??
  • ??
  • import?android.app.TabActivity;??
  • import?android.os.Bundle;??
  • import?android.view.LayoutInflater;??
  • import?android.widget.TabHost;??
  • import?android.widget.TabHost.OnTabChangeListener;??
  • import?android.widget.TabHost.TabSpec;??
  • import?android.widget.Toast;??
  • ??
  • public?class?TabProjectActivity?extends?TabActivity?implements?OnTabChangeListener{??
  • ????private?TabSpec?ts1,ts2,ts3;????//聲明3個分頁??
  • ????private?TabHost?tableHost;??????//分頁菜單(tab容器)??
  • ????/**?Called?when?the?activity?is?first?created.?*/??
  • ????@Override??
  • ????public?void?onCreate(Bundle?savedInstanceState)?{??
  • ????????super.onCreate(savedInstanceState);??
  • ????????tableHost?=?this.getTabHost();//實例(分頁)菜單??
  • ????????//利用LayoutInflater將布局與分頁菜單一起顯示??
  • ????????LayoutInflater.from(this).inflate(R.layout.main,?tableHost.getTabContentView());??
  • ????????ts1?=?tableHost.newTabSpec("tabOne");//實例化一個分頁??
  • ????????ts1.setIndicator("Tab1");//設置此頁顯示的標題??
  • ????????ts1.setContent(R.id.btn1);//設置此分頁的資源id??
  • ????????ts2?=?tableHost.newTabSpec("tabTwo");??
  • ????????//設置此分頁顯示的標題和圖標??
  • ????????ts2.setIndicator("Tab2",getResources().getDrawable(R.drawable.ic_launcher));??
  • ????????ts2.setContent(R.id.et1);??
  • ????????ts3?=?tableHost.newTabSpec("tavThree");??
  • ????????ts3.setIndicator("Tab3");??
  • ????????ts3.setContent(R.id.mylayout);//設置此分頁的布局id??
  • ????????tableHost.addTab(ts1);//菜單中添加ts1分頁??
  • ????????tableHost.addTab(ts2);??
  • ????????tableHost.addTab(ts3);??
  • ????????tableHost.setOnTabChangedListener(this);//這次監(jiān)聽器????????
  • ????????}??
  • ????????public?void?onTabChanged(String?tabId){??
  • ????????????if(tabId.equals("tabOne")){??
  • ????????????????Toast.makeText(this,?"分頁1",?Toast.LENGTH_LONG).show();??
  • ????????????}??
  • ????????????if(tabId.equals("tabTwo")){??
  • ????????????????Toast.makeText(this,?"分頁2",?Toast.LENGTH_LONG).show();??
  • ????????????}??
  • ????????????if(tabId.equals("tabThree")){??
  • ????????????????Toast.makeText(this,"分頁3",Toast.LENGTH_LONG).show();??
  • ????????????}??
  • ????????}??
  • ??????????
  • }??

  • ?

    自動添加的資源文件R.java

    [java]?view plaincopy
  • /*?AUTO-GENERATED?FILE.??DO?NOT?MODIFY.?
  • ?*?
  • ?*?This?class?was?automatically?generated?by?the?
  • ?*?aapt?tool?from?the?resource?data?it?found.??It?
  • ?*?should?not?be?modified?by?hand.?
  • ?*/??
  • ??
  • package?com.tabHost;??
  • ??
  • public?final?class?R?{??
  • ????public?static?final?class?attr?{??
  • ????}??
  • ????public?static?final?class?drawable?{??
  • ????????public?static?final?int?bg=0x7f020000;??
  • ????????public?static?final?int?bg2=0x7f020001;??
  • ????????public?static?final?int?ic_launcher=0x7f020002;??
  • ????}??
  • ????public?static?final?class?id?{??
  • ????????public?static?final?int?btn1=0x7f050000;??
  • ????????public?static?final?int?et1=0x7f050001;??
  • ????????public?static?final?int?mylayout=0x7f050002;??
  • ????}??
  • ????public?static?final?class?layout?{??
  • ????????public?static?final?int?main=0x7f030000;??
  • ????}??
  • ????public?static?final?class?string?{??
  • ????????public?static?final?int?app_name=0x7f040001;??
  • ????????public?static?final?int?btn1=0x7f040002;??
  • ????????public?static?final?int?btn2=0x7f040003;??
  • ????????public?static?final?int?et1=0x7f040004;??
  • ????????public?static?final?int?et2=0x7f040005;??
  • ????????public?static?final?int?hello=0x7f040000;??
  • ????}??
  • } ?
  • 總結

    以上是生活随笔為你收集整理的TabSpec与TabHost的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。