TabHost不显示标签' Android中的自定义布局

时间:2014-09-12 06:14:16

标签: android tabs android-tabs

我从这里开始学习本教程:http://wptrafficanalyzer.in/blog/creating-navigation-tabs-using-tabhost-and-fragments-in-android/

本教程提供的源代码工作正常。我尝试自己实现,但标签只显示标签,图标不存在。

以下是FragmentActivity

...
tHost =  (TabHost) findViewById(android.R.id.tabhost);
tHost.setup();
/**
     * Defining Tab Change Listener event. This is invoked when tab is
     * changed
     */
    TabHost.OnTabChangeListener tabChangeListener = new TabHost.OnTabChangeListener() {

        @Override
        public void onTabChanged(String tabId) {
            android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
            ContactsTabFragment contactsTabFragment = (ContactsTabFragment) fm
                    .findFragmentByTag(CONTACTS_TAG);

            SelfieIconsFragment selfieIconsFragment = (SelfieIconsFragment) fm
                    .findFragmentByTag(SELFIEICONS_TAG);

            ChatsTabFragment chatsTabFragment = (ChatsTabFragment) fm
                    .findFragmentByTag(CHAT_TAG);

            android.support.v4.app.FragmentTransaction ft = fm
                    .beginTransaction();

            /** Detaches the contacts if exists */
            if (contactsTabFragment != null)
                ft.detach(contactsTabFragment);

            /** Detaches the selfie icons if exists */
            if (selfieIconsFragment != null)
                ft.detach(selfieIconsFragment);

            /** Detaches the chat if exists */
            if (selfieIconsFragment != null)
                ft.detach(selfieIconsFragment);

            /** If current tab is contacts */
            if (tabId.equalsIgnoreCase(CONTACTS_TAG)) {

                if (contactsTabFragment == null) {
                    /**
                     * Create contactsFragment and add to
                     * fragmentTransaction
                     */
                    ft.add(R.id.realtabcontent, new ContactsTabFragment(),
                            CONTACTS_TAG);
                } else {
                    /**
                     * Bring to the front, if already exists in the
                     * fragmentTransaction
                     */
                    ft.attach(contactsTabFragment);
                }

            } else if (tabId.equalsIgnoreCase(SELFIEICONS_TAG)) {
                /** If current tab is selfie icons */
                if (selfieIconsFragment == null) {
                    /** Create selfie icons and add to fragmentTransaction */
                    ft.add(R.id.realtabcontent, new SelfieIconsFragment(),
                            SELFIEICONS_TAG);
                } else {
                    /**
                     * Bring to the front, if already exists in the
                     * fragmentTransaction
                     */
                    ft.attach(selfieIconsFragment);
                }
            } else if (tabId.equalsIgnoreCase(CHAT_TAG)) {
                /** If current tab is selfie icons */
                if (chatsTabFragment == null) {
                    /** Create AppleFragment and add to fragmentTransaction */
                    ft.add(R.id.realtabcontent, new ChatsTabFragment(),
                            CHAT_TAG);
                } else {
                    /**
                     * Bring to the front, if already exists in the
                     * fragmentTransaction
                     */
                    ft.attach(chatsTabFragment);
                }
            }
            ft.commit();
        }
    };

    /** Setting tabchangelistener for the tab */
    tHost.setOnTabChangedListener(tabChangeListener);

    /** Defining tab builder for Contacts tab */
    TabHost.TabSpec tSpecContacts = tHost.newTabSpec(CONTACTS_TAG);
    tSpecContacts.setIndicator("Contacts",getResources().getDrawable(R.drawable.android));        
    tSpecContacts.setContent(new DummyTabContent(getBaseContext()));        
    tHost.addTab(tSpecContacts);

    /** Defining tab builder for Selfie tab */
    TabHost.TabSpec tSpecSelfieIcons = tHost.newTabSpec(SELFIEICONS_TAG);
    tSpecSelfieIcons.setIndicator("Selfie Icon",getResources().getDrawable(R.drawable.android));        
    tSpecSelfieIcons.setContent(new DummyTabContent(getBaseContext()));
    tHost.addTab(tSpecSelfieIcons);

    /** Defining tab builder for Chat tab */
    TabHost.TabSpec tSpecChat = tHost.newTabSpec(CHAT_TAG);
    tSpecChat.setIndicator("Chat",getResources().getDrawable(R.drawable.android));        
    tSpecChat.setContent(new DummyTabContent(getBaseContext()));
    tHost.addTab(tSpecChat);

}

布局与示例完全相同。

<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">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"/>

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="0"/>

    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

</LinearLayout>

我尝试调试但仍无法找到问题。图标有什么问题?

P.S。 DummyTabContent.java也与示例相同。

0 个答案:

没有答案