從零開始學android<TabHost標籤組件.二十九.>,androidtabhost

來源:互聯網
上載者:User

從零開始學android<TabHost標籤組件.二十九.>,androidtabhost
TabHost主要特點是可以在一個視窗中顯示多組標籤欄的內容,在Android系統之中每個標籤欄就稱為一個Tab,而包含這多個標籤欄的容器就將其稱為TabHost,TabHost類的繼承結構如下所示:java.lang.Object   ↳ android.view.View     ↳ android.view.ViewGroup       ↳ android.widget.FrameLayout         ↳ android.widget.TabHost 
常用方法如下所示

1 public TabHost(Context context) 構造 建立TabHost類對象
2 public void addTab(TabHost.TabSpec tabSpec) 普通 增加一個Tab
3 public TabHost.TabSpec newTabSpec(String tag) 普通 建立一個TabHost.TabSpec對象
4 public View getCurrentView() 普通 取得當前的View對象
5 public void setup() 普通 建立TabHost對象
6 public void setCurrentTab(int index) 普通 設定當前顯示的Tab編號
7 public void setCurrentTabByTag(String tag) 普通 設定當前顯示的Tab名稱
8 public FrameLayout getTabContentView() 普通 返回標籤容器
9 public void setOnTabChangedListener(TabHost.OnTabChangeListener l) 普通 設定標籤改變時觸發

兩種方式實現TabHost
方式一:直接讓一個Activity程式繼承TabActivity類;方式二:利用findViewById()方法取得TagHost組件,並進行若干配置;
第一種方式讓一個類繼承tabActivity
XMl檔案 配置需要在一個xml檔案中嵌套使用布局 來達到不同Tab中顯示不同的內容
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <LinearLayout        android:layout_marginTop="50dp"        android:id="@+id/login"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical" >        <TableRow            android:id="@+id/tableRow1"            android:layout_width="match_parent"            android:layout_height="wrap_content" >            <TextView                android:id="@+id/textView1"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="帳號" />            <EditText                android:id="@+id/editText1"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:ems="10"                android:textSize="25sp" />        </TableRow>        <TableRow            android:id="@+id/tableRow2"            android:layout_width="match_parent"            android:layout_height="wrap_content" >            <TextView                android:id="@+id/textView2"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="密碼" />            <EditText                android:id="@+id/editText2"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:ems="10"                android:textSize="25sp" />        </TableRow>        <TableRow            android:id="@+id/tableRow3"            android:layout_width="match_parent"            android:layout_height="wrap_content" >            <Button                android:id="@+id/button1"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_weight="1"                android:text="取消" />            <Button                android:id="@+id/button2"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_weight="1"                android:text="登入" />        </TableRow>    </LinearLayout>    <LinearLayout        android:layout_marginTop="50dp"        android:id="@+id/image"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical" >        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@drawable/a2" />    </LinearLayout>    <LinearLayout        android:layout_marginTop="50dp"        android:id="@+id/timer"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical" >        <TimePicker            android:layout_width="match_parent"            android:layout_height="wrap_content" />    </LinearLayout> <LinearLayout        android:layout_marginTop="50dp"        android:id="@+id/timer1"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical" >        <TimePicker            android:layout_width="match_parent"            android:layout_height="wrap_content" />    </LinearLayout>     <LinearLayout        android:layout_marginTop="50dp"        android:id="@+id/timer2"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical" >        <TimePicker            android:layout_width="match_parent"            android:layout_height="wrap_content" />    </LinearLayout></LinearLayout>


JAVA檔案設定

package com.example.tabhost;import android.app.TabActivity;import android.os.Bundle;import android.view.LayoutInflater;import android.widget.TabHost;import android.widget.TabHost.TabSpec;public class MainActivity extends TabActivity {private TabHost tabHost;//初始化TabHost組件private int reslayout[] = { R.id.login, R.id.image, R.id.timer , R.id.timer1,R.id.timer2};//設定對應的額xml檔案private int images[]={R.drawable.cart,R.drawable.cloud,R.drawable.comment,R.drawable.gear,R.drawable.joystick};//設定顯示的標題檔案@SuppressWarnings("deprecation")@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);this.tabHost = super.getTabHost();//執行個體化TabHost組件// 取得LayoutInflater對象LayoutInflater.from(this).inflate(R.layout.linearlayout,//制定布局this.tabHost.getTabContentView(),//制定標籤增加的容器 true);for (int i = 0; i < reslayout.length; i++) {TabSpec myTab = tabHost.newTabSpec("tab" + i);// 定義TabSpecmyTab.setIndicator(null,getResources().getDrawable(images[i])) ;// 設定標籤myTab.setContent(this.reslayout[i]) ;// 設定顯示的組件this.tabHost.addTab(myTab) ;}}}

如下




使用設定檔設定。這種方法較為常見,可以講TabHost置於底部
但是布局檔案較為複雜,大家可以參照例子進行具體的學習
XML檔案
<?xml version="1.0" encoding="utf-8"?><TabHost xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/tabhost" android:orientation="vertical"android:layout_width="fill_parent" android:layout_height="fill_parent"><RelativeLayout android:orientation="vertical"android:layout_width="fill_parent" android:layout_height="fill_parent"><TabWidget android:id="@android:id/tabs"android:layout_width="fill_parent" android:layout_height="wrap_content"android:layout_alignParentBottom="true" /><FrameLayout android:id="@android:id/tabcontent"android:layout_width="fill_parent" android:layout_height="fill_parent">  <LinearLayout                android:id="@+id/login"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical" >        <TableRow            android:id="@+id/tableRow1"            android:layout_width="match_parent"            android:layout_height="wrap_content" >            <TextView                android:id="@+id/textView1"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="帳號" />            <EditText                android:id="@+id/editText1"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:ems="10"                android:textSize="25sp" />        </TableRow>        <TableRow            android:id="@+id/tableRow2"            android:layout_width="match_parent"            android:layout_height="wrap_content" >            <TextView                android:id="@+id/textView2"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="密碼" />            <EditText                android:id="@+id/editText2"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:ems="10"                android:textSize="25sp" />        </TableRow>        <TableRow            android:id="@+id/tableRow3"            android:layout_width="match_parent"            android:layout_height="wrap_content" >            <Button                android:id="@+id/button1"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_weight="1"                android:text="取消" />            <Button                android:id="@+id/button2"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_weight="1"                android:text="登入" />        </TableRow>    </LinearLayout>    <LinearLayout        android:id="@+id/image"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical" >        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@drawable/a2" />    </LinearLayout>    <LinearLayout        android:id="@+id/timer"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical" >        <TimePicker            android:layout_width="match_parent"            android:layout_height="wrap_content" />    </LinearLayout> <LinearLayout        android:id="@+id/timer1"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical" >        <TimePicker            android:layout_width="match_parent"            android:layout_height="wrap_content" />    </LinearLayout>     <LinearLayout        android:id="@+id/timer2"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical" >        <TimePicker            android:layout_width="match_parent"            android:layout_height="wrap_content" />    </LinearLayout></FrameLayout></RelativeLayout ></TabHost>

JAVA檔案配置
package com.example.tabhost;import android.os.Bundle;import android.app.Activity;import android.widget.TabHost;import android.widget.TabHost.TabSpec;public class MainActivity extends Activity {// 直接繼承Activityprivate TabHost myTabHost;// 定義TabHostprivate int[] layRes = { R.id.login, R.id.image , R.id.timer, R.id.timer1, R.id.timer2 };// 定義內嵌布局管理器IDprivate int images[]={R.drawable.cart,R.drawable.cloud,R.drawable.comment,R.drawable.gear,R.drawable.joystick};//標題圖片資料@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(R.layout.activity_main) ;// 調用預設布局管理器this.myTabHost = (TabHost) super.findViewById(R.id.tabhost); // 取得TabHost對象this.myTabHost.setup() ;// 建立TabHost對象for (int x = 0; x < this.layRes.length; x++) {// 迴圈取出所有布局標記TabSpec myTab = myTabHost.newTabSpec("tab" + x);// 定義TabSpecmyTab.setIndicator(null,getResources().getDrawable(images[x])) ;// 設定標籤文字myTab.setContent(this.layRes[x]) ;// 設定顯示的組件this.myTabHost.addTab(myTab) ;// 增加標籤}this.myTabHost.setCurrentTab(0) ;// 設定開始索引}}






使用TabHost組件設定Tab切換與intent的結合在開發中較常用到,是app開發架構的基礎
下節預報:Menu菜單


android TabHost label 透明 怎讓TabHost裡面的標籤變得透明,包括裡面的控制項

你是想讓tabhost更美觀吧?
可以用自訂的tabhost,我這邊有個Demo,你要我可以發給你
 
android裡實現一個tabhost顯示怎弄?

Tab標籤頁是介面設計時經常使用的介面控制項,可以實現多個分頁之間的快速切換,每個分頁可以顯示不同的內容,是Android系統內建的Tab標籤頁,點擊“呼出/接聽鍵”後出現,用於電話呼出和查看撥號記錄、連絡人.

Tab標籤頁的使用
先要設計所有的分頁的介面布局
在分頁設計完成後,使用代碼建立Tab標籤頁,並給每個分頁添加標識和標題
最後確定每個分頁所顯示的介面布局
每個分頁建立一個XML檔案,用以編輯和儲存分頁的介面布局,使用的方法與設計普通使用者介面沒有什麼區別

建立一個“TabDemo”程式,包含三個XML檔案,分別為tab1.xml、tab2.xml和tab3.xml,這3個檔案分別使用線性布局、相對布局和絕對布局樣本中的main.xml的代碼,並將布局的ID分別定義為layout01、layout02和layout03

關鍵代碼如下:
10. public void onCreate(Bundle savedInstanceState) {
11. super.onCreate(savedInstanceState);
12. TabHost tabHost = getTabHost();
13. LayoutInflater.from(this).inflate(R.layout.tab1, tabHost.getTabContentView(),true);
14. LayoutInflater.from(this).inflate(R.layout.tab2, tabHost.getTabContentView(),true);
15. LayoutInflater.from(this).inflate(R.layout.tab3, tabHost.getTabContentView(),true);
16. tabHost.addTab(tabHost.newTabSpec("TAB1")
17. .setIndicator("線性布局").setContent(R.id.layout01));
18. tabHost.addTab(tabHost.newTabSpec("TAB2")
19. .setIndicator("絕對布局").setContent(R.id.layout02));
20. tabHost.addTab(tabHost.newTabSpec("TAB3")
21. .setIndicator("相對布局").setContent(R.id.layout03));
22. }
23. }

第8行代碼的聲明TabDemo類繼承與TabActivity,與以往繼承Activity不同,TabActivity支援內嵌多個Activity或View
第12行代碼通過getTabHost()函數獲得了Tab標籤頁的容器,用以承載可以點擊的Tab標籤和分頁的介面布局。
第13行代碼通過LayoutInflater將tab1.xml檔案中的布局轉換為Tab標籤頁可以使用的View對象
第16行代碼使用addTab()函數添加了第1個分頁,tabHost.newTabSpec("TAB1")表明在第12行代碼中建立的tabHost上,添加一個標識為TAB1的Tab分頁
第17行代碼使用s......餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.