Android開發系列(四) TabView 索引標籤控制項的使用

來源:互聯網
上載者:User

 最近學習Android開發的時候,發現網上對於設計選項卡的教程很亂,因此結合Mars老師的視頻,在這裡做一下總結以備參考。 這裡建立三個Activity,一個是TabActivity ,另外兩個分別是兩個選項卡對應的Activity。 第一步  建立三個Activity並在AndroidManifest檔案中進行註冊; 複製代碼        <activity             android:name="com.example.android_tabname.MainActivity">            <intent-filter >                <action android:name="android.intent.action.MAIN"/>                <category android:name="android.intent.category.LAUNCHER"/>            </intent-filter>        </activity>        <activity             android:name="com.example.android_tabname.TabOne">                    </activity>        <activity             android:name="com.example.android_tabname.TabTwo">                    </activity>複製代碼     然後設定MainActivity 的布局: 複製代碼<?xml version="1.0" encoding="utf-8"?><TabHost xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@android:id/tabhost"              android:layout_width="match_parent"    android:layout_height="match_parent" >    <!-- 最外層是一個TabHost控制項,對應了MianActivity,注意這裡的id需要用系統內建的id -->        <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"/>        <FrameLayout             android:id="@android:id/tabcontent"            android:layout_width="match_parent"            android:layout_height="match_parent"/>             </LinearLayout>     </TabHost>複製代碼  然後是TabOne和TabTwo的布局:二者是一樣的,所以只貼了一個 複製代碼<?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" >        <TextView         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:text="This is TabTwo"        android:textSize="50dip"/>     </LinearLayout>複製代碼 第二步   在MainActivity中建立TabHost和TabHost.TabSpec對象,然後調用setIndicator()  和setContent()  方法,最後再調用TabHost的addTab()方法,將選項卡添加到索引標籤控制項中,程式如下: 複製代碼package com.example.android_tabname;import android.app.TabActivity;import android.content.Intent;import android.os.Bundle;import android.widget.*; public class MainActivity extends TabActivity{     TabHost tabHost=null;      //選項卡控制器    TabHost.TabSpec tabSpecA,tabSpecB=null;   //選項卡,這裡選項卡最好不用混用,有幾個選項卡就設定幾個對象        @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        //獲得TabHost執行個體;        tabHost=getTabHost();           //獲得TabHost.TabSpec對象執行個體;        tabSpecA=tabHost.newTabSpec("One");        //為TabSpec對象設定指標        tabSpecA.setIndicator("TabA",getResources().getDrawable(android.R.drawable.ic_media_play));        //為選項卡設定內容,這裡需要建立一個intent對象        Intent intentA=new Intent();        intentA.setClass(this, TabOne.class);        tabSpecA.setContent(intentA);                //然後建立第二個選項卡:        tabSpecB=tabHost.newTabSpec("Two");        tabSpecB.setIndicator("TabB",getResources().getDrawable(android.R.drawable.ic_media_next));        Intent intentB=new Intent();        intentB.setClass(this, TabTwo.class);        tabSpecB.setContent(intentB);                //最後一步,把兩個選項卡TabSpec添加到索引標籤控制項TabHost中        tabHost.addTab(tabSpecA);        tabHost.addTab(tabSpecB);                                     }             }複製代碼 另外兩個Activity只是設定了一下布局檔案,後續可以根據不同需要進行擴充。代碼如下: 複製代碼package com.example.android_tabname;import android.app.Activity;import android.os.Bundle;  public class TabOne extends Activity{     @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        setContentView(R.layout.layout_tabone);    }         }複製代碼  複製代碼package com.example.android_tabname; import android.app.Activity;import android.os.Bundle; public class TabTwo extends Activity{     @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        setContentView(R.layout.layout_tabtwo);    }     }

聯繫我們

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