android學習筆記十——TabHost

來源:互聯網
上載者:User

標籤:

TabHost——標籤頁

==》

TabHost,可以在視窗放置多個標籤頁,每個標籤頁相當於獲得了一個與外部容器相同大小的組件擺放地區。

通過此種方式可以實現在一個容器放置更多組件(EG:通話記錄實現方式)。

TabHost僅僅是一個簡單的容器,其提供了如下兩個方法來建立選項卡、添加選項卡

==》

  newTabSpec(String tag):建立選項卡

  addTab(TabHost.TabSpec tabspec):添加選項卡

使用TabHost的一般步驟:

  1.在介面布局中定義TabHost組件,並為該組件定義選項卡的內容;

  2.Activity繼承TabActivity;

  3.調用TabActivity的getTabHost()方法擷取TabHost對象;

  4.通過TabHost對象的方法建立或添加選項卡;

注意:TabHost還提供了一些方法擷取當前選項卡,擷取當前View的方法;可通過TabHost.OnTabChangeListener監聽器——監控TabHost當前頁簽的改變。

    TabWidget : 該組件就是TabHost標籤頁中上部 或者 下部的按鈕, 可以點擊按鈕切換選項卡;

    TabSpec : 代表了選項卡介面, 添加一個TabSpec即可添加到TabHost中;

執行個體如下:

布局檔案==><TabHost xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@android:id/tabhost"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".Main" >    <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="wrap_content" >            <LinearLayout                android:id="@+id/tab1"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:orientation="vertical" >                <TextView                    android:layout_width="match_parent"                    android:layout_height="match_parent"                    android:text="110" />            </LinearLayout>            <LinearLayout                android:id="@+id/tab2"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:orientation="vertical" >                <TextView                    android:layout_width="match_parent"                    android:layout_height="match_parent"                    android:text="220" />            </LinearLayout>                         <LinearLayout                android:id="@+id/tab3"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:orientation="vertical" >                <TextView                    android:layout_width="match_parent"                    android:layout_height="match_parent"                    android:text="330" />            </LinearLayout>        </FrameLayout>    </LinearLayout></TabHost>代碼實現==》package com.example.mytabhost;import android.annotation.SuppressLint;import android.app.TabActivity;import android.os.Bundle;import android.view.LayoutInflater;import android.view.Menu;import android.widget.TabHost;import android.widget.TabHost.OnTabChangeListener;import android.widget.TabHost.TabSpec;import android.widget.Toast;@SuppressLint("ShowToast")@SuppressWarnings("deprecation")public class MainActivity extends TabActivity{@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);final TabHost tabHost = this.getTabHost();TabSpec tab1 = tabHost.newTabSpec("tab1").setIndicator("tab1").setContent(R.id.tab1);tabHost.addTab(tab1);// 以上代碼等價於tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("tab2").setContent(R.id.tab2));tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab3").setContent(R.id.tab3));tabHost.setOnTabChangedListener(new OnTabChangeListener(){@Overridepublic void onTabChanged(String tabId){if (tabId.equals("tab1")){ // 第一個標籤Toast.makeText(MainActivity.this, "選擇了001", 3000).show();}if (tabId.equals("tab2")){ // 第二個標籤Toast.makeText(MainActivity.this, "選擇了002", 3000).show();}if (tabId.equals("tab3")){ // 第三個標籤Toast.makeText(MainActivity.this, "選擇了003", 3000).show();}}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu){getMenuInflater().inflate(R.menu.main, menu);return true;}}

實現效果如:

 

android學習筆記十——TabHost

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.