Android之TabHost的使用(一)

來源:互聯網
上載者:User

在使用TabHost之前,我們先來簡單的看下它的原始碼。

主要變數有:

private TabWidget mTabWidget;

private FrameLayout mTabContent;

private List mTabSpecs = new ArrayList(2);

也就是說我們的tabhost必須有這三個東西,所以我們的.xml檔案就會有規定:繼續查看原始碼:

public void setup() {
        mTabWidget = (TabWidget) findViewById(com.android.internal.R.id.tabs);
        if (mTabWidget == null) {
            throw new RuntimeException(
                    "Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'");
}

mTabContent = (FrameLayout) findViewById(com.android.internal.R.id.tabcontent);

if (mTabContent == null) {
            throw new RuntimeException(
                    "Your TabHost must have a FrameLayout whose id attribute is "
                            + "'android.R.id.tabcontent'");
}

也就是說我們的.xml檔案需要TabWidget和FrameLayout標籤。

接下來構建我們自己的tab執行個體:
      有兩種方式可以實現:
      一種是繼承TabActivity 類,可以使用android的自己內部定義好的.xml資源檔作容器檔案。也就是在我們的代碼中使用getTabHost(); , 而相應的後台源碼是這樣的:
this.setContentView(com.android.internal.R.layout.tab_content);
       在系統的資源檔中可以看見這個layout
      有了容器,然後我們就需要我們為每個tab分配內容

步驟:

1、在布局檔案中使用FrameLayout列出Tab組件和Tab組件內容(動態載入的內容可以在後台完成)

2、Activity需要繼承TabActivity

3、調用TabActivity的getTabHost()即可得到TabHost對象(即盛放Tab的容器物件)

4、通過TabHost建立Tab選項卡

main.xml檔案內容:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView android:id="@+id/tab_demo_tv1"    
        android:layout_width="fill_parent"     
        android:layout_height="fill_parent"     
        android:text="android,哈哈!!!!"    
    />    
<TextView android:id="@+id/tab_demo_tv2"    
        android:layout_width="fill_parent"     
        android:layout_height="fill_parent"     
        android:text="這是一個tabhost執行個體"    
    />    
<ListView
    android:id="@+id/tab_demo_tv3"    
    android:layout_width="fill_parent"     
    android:layout_height="fill_parent"     
    />   
<Button
    android:id="@+id/tab_demo_tv4"    
    android:layout_width="fill_parent"     
    android:layout_height="wrap_content"     
    android:text="開關"
/>
</FrameLayout>

TabHostTest.java類,繼承TabActivity類:代碼如下,

import java.util.ArrayList;
import java.util.List;
import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TabHost;

public class TabHostTest extends TabActivity {
    private android.widget.TabHost tabHost;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //得到tab容器
        tabHost = this.getTabHost();
        //將FrameLayout綁定到tabHost上
        LayoutInflater.from(this).inflate(R.layout.main,tabHost.getTabContentView(), true);
        //靜態擷取內容
        tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab1", null).setContent(R.id.tab_demo_tv1));
        //靜態擷取內容
        tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Tab2", null).setContent(R.id.tab_demo_tv2));
        //動態擷取內容
        tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("Tab3", null).setContent(new TabHost.TabContentFactory() {
                    
                    @Override
                    public View createTabContent(String tag) {
                         ListView lv = (ListView) findViewById(R.id.tab_demo_tv3);
                            List<String> list = new ArrayList<String>();
                            list.add("tab3_01");
                            list.add("tab3_02");
                            list.add("tab3_03");
                            list.add("tab3_04");
                            ArrayAdapter<String> adapter = new ArrayAdapter<String>(TabHostTest.this, android.R.layout.simple_list_item_1, list);
                            lv.setAdapter(adapter);
                            return lv ;
                    }
                }));
        //靜態擷取內容
        tabHost.addTab(tabHost.newTabSpec("tab4").setIndicator("Tab4", null)
                .setContent(R.id.tab_demo_tv4));
        
        setContentView(tabHost);
    }
}

小結:

1、 在一個tabActivity裡面嵌套一個tabActivity,如果在子tabActivity裡面顯示AlertDialog、ProgressDialog的話,就會引發錯誤:android.view.WindowManager$BadTokenException:Unable to add window

解決方案:

可以把建立dialog時傳遞的參數xxx.this改成this.getParent(),其中的xxx為Activity。

http://my.chinaunix.net/space.php?uid=25370280&do=blog&id=122252

相關文章

聯繫我們

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