生活随笔
收集整理的這篇文章主要介紹了
TabSpec与TabHost
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Android游戲開發系統控件-TabSpec與TabHost
2012/5/12 星期六
5.12 汶川地震四周年,四年了,時間飛快,再大的苦難都屬于過去,現在只著眼于眼前,把握現在,能讓自己過得開心不后悔就行了。加油吧!!!
今天學習了另一個比較特殊的控件:TabSpec(分頁),TabHost(分頁的集合)
TabHost相當于瀏覽器中分頁的集合,而TabSpec則相當于瀏覽器中的每個分頁;在Android中,每一個TabSpec分頁可以是一個組件,也可以是一個布局,然后將每個分頁裝入TabHost中,TabHost即可將其中的每個分頁一并顯示出來。
創建項目:TabProject
向項目資源中添加了兩張圖片資源:bg.png與bg2.png.
作者:wwj
功能:實現在布局中進行頁面切換
項目運行結果截圖:
??????????
?
?
?
?
修改代碼:
=>>布局文件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;?????? ????private?TabHost?tableHost;???????? ?????? ????@Override?? ????public?void?onCreate(Bundle?savedInstanceState)?{?? ????????super.onCreate(savedInstanceState);?? ????????tableHost?=?this.getTabHost();?? ?????????? ????????LayoutInflater.from(this).inflate(R.layout.main,?tableHost.getTabContentView());?? ????????ts1?=?tableHost.newTabSpec("tabOne");?? ????????ts1.setIndicator("Tab1");?? ????????ts1.setContent(R.id.btn1);?? ????????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);?? ????????tableHost.addTab(ts1);?? ????????tableHost.addTab(ts2);?? ????????tableHost.addTab(ts3);?? ????????tableHost.setOnTabChangedListener(this);?? ????????}?? ????????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
? ? ? ? ? ?? ?? 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的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。