android TabHost 例子

來源:互聯網
上載者:User

在android中實現選項卡的效果可以通過兩種方式:在布局檔案中引用TabHost,在Activity中通過Id擷取TabHost的執行個體,或者直接繼承TabActivity,直接擷取TabHost的執行個體。

程式主要代碼如下:
1、布局檔案tab.xml,在布局檔案中需要注意的是:如果TabHost標籤中引用TabWidget標籤,則必須設定其android:id="@android:id/tabs",而FrameLayout標籤的id必須設定為:android:id="@android:id/tabcontent",否則會出現null 指標異常。

    <?xml version="1.0" encoding="utf-8"?>
    <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/tabhost"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent">
                    <!-- set the tab title attributes and set the title

align the bottom -->
                    <TabWidget
                            android:id="@android:id/tabs"
                            android:layout_alignParentBottom="true"  //這

條語句的作用是把TabWidget設定到螢幕的下方,預設在螢幕上方。
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content">
                    </TabWidget>
                    <!-- set the tab body attributes -->
                    <FrameLayout
                            android:id="@android:id/tabcontent"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">
                            <LinearLayout
                                    android:id="@+id/tab1"
                                    android:orientation="vertical"
                                    android:layout_width="fill_parent"
                                    android:layout_height="fill_parent"
                                    android:paddingTop="20dip">
                                    <ImageView
                                            

android:layout_width="wrap_content"
                                            

android:layout_height="wrap_content"
                                            

android:layout_gravity="center"
                                            android:src="@drawable/home"/>
                            </LinearLayout>
                            <LinearLayout
                                    android:id="@+id/tab2"
                                    android:orientation="vertical"
                                    android:layout_width="fill_parent"
                                    android:layout_height="fill_parent"
                                    android:paddingTop="20dip">
                                    <ImageView
                                            

android:layout_width="wrap_content"
                                            

android:layout_height="wrap_content"
                                            

android:layout_gravity="center"
                                            

android:src="@drawable/garbage"/>
                                    </LinearLayout>
                            <LinearLayout
                                    android:id="@+id/tab3"
                                    android:orientation="vertical"
                                    android:layout_width="fill_parent"
                                    android:layout_height="fill_parent"
                                    android:paddingTop="20dip">
                                    <ImageView
                                            

android:layout_width="wrap_content"
                                            

android:layout_height="wrap_content"
                                            

android:layout_gravity="center"
                                            android:src="@drawable/help"/>
                            </LinearLayout>
                    </FrameLayout>
            </RelativeLayout>
    </TabHost>

Activity檔案 TabDemo.java

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Gravity;
    import android.widget.TabHost;
    import android.widget.Toast;
    import android.widget.TabHost.OnTabChangeListener;

    import com.kf.samples5.R;

    public class TabDemo extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab);
    TabHost host = (TabHost)findViewById(R.id.tabhost);
    host.setup();

    TabHost.TabSpec homeSpec = host.newTabSpec("Home");        //This

param will be used as tabId.
    homeSpec.setIndicator(null,         //This param will diplay as title.
    getResources().getDrawable(R.drawable.home_normal));
    homeSpec.setContent(R.id.tab1);
    host.addTab(homeSpec);

    TabHost.TabSpec garbageSpec = host.newTabSpec("Garbage");
    garbageSpec.setIndicator(null, getResources().getDrawable

(R.drawable.garbage_normal));
    garbageSpec.setContent(R.id.tab2);
    host.addTab(garbageSpec);

    TabHost.TabSpec maybeSpec = host.newTabSpec("Help");
    maybeSpec.setIndicator(null, getResources().getDrawable

(R.drawable.help_normal));
    maybeSpec.setContent(R.id.tab3);
    host.addTab(maybeSpec);

    host.setOnTabChangedListener(new OnTabChangeListener() {

    @Override
    public void onTabChanged(String tabId) {
    // TODO Auto-generated method stub
    Toast toast = Toast.makeText(TabDemo.this, tabId, Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 50);
    toast.show();
    }
    });

    //         host.setCurrentTabByTag("Home");
    Toast toast = Toast.makeText(TabDemo.this, "Home",

Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 50);
    toast.show();
    }
    }

聯繫我們

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