Android TabHost學習筆記

來源:互聯網
上載者:User

一、什麼是TabHost。
Android 裡面的TabHost就是選項卡,看(新浪微博介面):

至於選項卡有什麼好處或者用途,我想代碼哥們都知道吧,我就不多說了。

二、在Android裡面如何?TabHost
有兩種方式可以實現。

1、繼承TabActivity,然後用getTabHost()方法擷取TabHost,最後在布局檔案中定義各個Tab選項卡添加到TabHost中

2、不繼承TabActivity,然後在布局檔案中定義TabHost,最後講各個Tab選項卡添加到TabHost中

總結以上兩種方式為兩步:

①:擷取TabHost對象

②:把Tab添加到TabHost中。

我們先看第一種實現:

①:布局檔案:

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><!-- 第一個選項卡面板 --><LinearLayoutandroid:id="@+id/tab1"android:layout_width="fill_parent"android:layout_height="fill_parent"><!-- 面板中只有一個TextView--><TextView android:id="@+id/V1"android:layout_width="fill_parent"android:layout_height="fill_parent"android:text="Touch Android"/></LinearLayout><!-- 第二個選項卡面板 --><LinearLayoutandroid:id="@+id/tab2"android:layout_width="fill_parent"android:layout_height="fill_parent"><!-- 面板中只有一個TextView--><TextView android:id="@+id/V2"android:layout_width="fill_parent"android:layout_height="fill_parent"android:text="Touch Android"/></LinearLayout></LinearLayout>

②:Activity

package com.droidstouch.tabhost;import android.app.TabActivity;import android.os.Bundle;import android.view.LayoutInflater;import android.widget.TabHost;import android.widget.TabHost.TabSpec;/*** @author <a href="http://bbs.droidstouch.com">Touch Android</a>**/public class Demo2Activity extends TabActivity{protected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);// this.setContentView(R.layout.demo2); // 注意不要加上此行代碼//擷取到TabHost對象TabHost tabHost =this.getTabHost();//把我們的布局檔案添加到tabHost 的FrameLayout下面LayoutInflater.from(this).inflate(R.layout.demo2, tabHost.getTabContentView(), true);// 下面定義了兩個選項卡//擷取一個新的TabHost.TabSpec,並關聯到當前tab host//參數:所需的選項卡標籤TabSpec pSpec = tabHost.newTabSpec("parent");// 參數一:選項卡上的文字,參數二:選項卡的背景圖片pSpec.setIndicator("父類", this.getResources().getDrawable(R.drawable.f_root));//設定選項卡內容pSpec.setContent(R.id.tab1);TabSpec subSpec = tabHost.newTabSpec("sub");subSpec.setIndicator("子類", this.getResources().getDrawable(R.drawable.f_sub));subSpec.setContent(R.id.tab2);// 將選項卡添加到TabHost中tabHost.addTab(pSpec);tabHost.addTab(subSpec);}}

第二種方式
①:布局檔案

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><!-- 定義TabHost ,自訂的TabHost一定得包含TabWidget 和 FrameLayout,並且 TabWidget 的ID一定是@android:id/tabsFrameLayout 的Id一定是@android:id/tabcontent--><TabHost android:id="@+id/tabs"android:layout_width="fill_parent"android:layout_height="fill_parent"><LinearLayout android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><!-- 定義TabWidget,此控制項 必須和TabHost一起使用 --><TabWidget android:id="@android:id/tabs"android:layout_width="fill_parent"android:layout_height="wrap_content"/><!-- 定義FrameLayout--><FrameLayout android:id="@android:id/tabcontent"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextView android:id="@+id/txtV1"android:layout_width="fill_parent"android:layout_height="fill_parent"android:text="Touch Android"/><TextView android:id="@+id/txtV2"android:layout_width="fill_parent"android:layout_height="fill_parent"android:text="http://bbs.droidstouch.com"/></FrameLayout></LinearLayout></TabHost></LinearLayout>

②:Activity:

package com.droidstouch.tabhost;import android.app.Activity;import android.os.Bundle;import android.widget.TabHost;import android.widget.TabHost.TabSpec;public class Dome1Activity extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.demo1);//從布局檔案中 擷取到TabHostTabHost tabHost = (TabHost) this.findViewById(R.id.tabs);//安裝TabHosttabHost.setup();// 下面定義兩個選項卡//擷取一個新的TabHost.TabSpec,並關聯到當前tab host//參數:所需的選項卡標籤TabSpec pSpec = tabHost.newTabSpec("parent");pSpec.setIndicator("父類", this.getResources().getDrawable(R.drawable.f_root));pSpec.setContent(R.id.txtV1);TabSpec subSpec = tabHost.newTabSpec("sub");subSpec.setIndicator("子類", this.getResources().getDrawable(R.drawable.f_root));subSpec.setContent(R.id.txtV2);//添加選項卡到TabHost中tabHost.addTab(pSpec);tabHost.addTab(subSpec);}}

 

聯繫我們

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