创建使用TabHost和多个片段的Andr​​oid应用 [英] Creating an Android app using TabHost and multiple fragments

查看:137
本文介绍了创建使用TabHost和多个片段的Andr​​oid应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图建立一个基本的Andr​​oid应用程序,其中包含标签,点击它会加载一个新的片段时 - 每一个片段作为一个单独的XML文件。我想用TabHost,而不是活动栏标签。我一直在寻找通过网络和这里的帖子在计算器上很多教程,但无济于事。大多数问题和教程都在讨论任何片段或制表符,而不是两个问题。预先感谢您的建议。

I have been trying to create a basic Android app that contains tabs, which when clicked will load a new fragment - each fragment being a separate xml file. I would like to use TabHost and not the Activity Bar tabs. I have been looking through many tutorials on the web and posts here on stackoverflow, but to no avail. Most questions and tutorials are discussing either fragments OR tabs, but not both subjects. Thank you in advance for the suggestions.

我想尝试包含一个非常基本的应用程序:

I would like to try a very basic app that contains:


  • 一个片段输入一个数字 - XML file_1(第1片)

  • 一个片段显示在一个TextView数 - XML file_2(第2片)

推荐答案

首先,感谢丹尼斯为我安排足够接近正确的解决方案,我可以勉强维持一个工作项目。现在,这应该是接近的功能。我改了个名字是通用的,但希望如预期,一切都会工作,并足以帮助您了解发生了什么。

First off, thanks to deniz for getting me close enough to the right solution I could eke out a working project. Now, this should be closer to functional. I changed names to be generic, but hopefully everything will work as intended and be enough to help you understand what's happening.

TabHostActivity(你的主要活动):

TabHostActivity (your main Activity):

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;

public class TabHostActivity extends FragmentActivity implements TabHost.OnTabChangeListener
{
    private static final String TAG = "TabHostActivity";

    private TabHost tabHost;

    @Override
    protected void onCreate(final Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_tabhost);

        tabHost = (TabHost) findViewById(android.R.id.tabhost);
        tabHost.setOnTabChangedListener(this);
        tabHost.setCurrentTab(0);
        setupTabs();
    }

    private void setupTabs()
    {
        tabHost.setup();
        setupTab(new TextView(this), "tab1");
        setupTab(new TextView(this), "tab2");
        setupTab(new TextView(this), "tab3");
        setupTab(new TextView(this), "tab4");
    }

    private void setupTab(final View view, final String tag)
    {
        View tabview = createTabView(tabHost.getContext(), tag);

        TabHost.TabSpec setContent = tabHost.newTabSpec(tag)
                .setIndicator(tabview)
                .setContent(new TabHost.TabContentFactory()
                {
                    public View createTabContent(String tag)
                    {
                        return view;
                    }
                });
        tabHost.addTab(setContent);
    }

    /**
     * Return the view for the individual tabs, the tab view being set is identified by tabId.
     *
     * @param context
     * @param tabId
     * @return
     */
    private static View createTabView(final Context context, final String tabId)
    {
        int resourceId;
        if (tabId.equals("tab1"))
        {
            resourceId = R.layout.tab1;
        }
        else if (tabId.equals("tab2"))
        {
            resourceId = R.layout.tab2;
        }
        else if (tabId.equals("tab3"))
        {
            resourceId = R.layout.tab3;
        }
        else
        {
            resourceId = R.layout.tab4;
        }

        return LayoutInflater.from(context).inflate(resourceId, null);
    }

    @Override
    public void onTabChanged(String tabId)
    {
        Log.d(TAG, "onTabChanged(): tabId=" + tabId);

        if (tabId.equalsIgnoreCase("tab1"))
        {
            updateTab(android.R.id.tabcontent, new tab1(), tabId);
        }
        else if (tabId.equalsIgnoreCase("tab2"))
        {
            updateTab(android.R.id.tabcontent, new tab2(), tabId);
        }
        else if (tabId.equalsIgnoreCase("tab3"))
        {
            updateTab(android.R.id.tabcontent, new tab3(), tabId);
        }
        else
        {
            updateTab(android.R.id.tabcontent, new tab4(), tabId);
        }
    }

    public void updateTab(int placeholder, Fragment fragment, String tabId)
    {
        getSupportFragmentManager().beginTransaction()
                .replace(placeholder, fragment, tabId)
                .commit();
    }
}

activity_tabhost.xml:

activity_tabhost.xml:

  <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

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


            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1" >

                <FrameLayout
                    android:id="@+id/tab_1"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" />

                <FrameLayout
                    android:id="@+id/tab_2"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" />

                <FrameLayout
                    android:id="@+id/tab_3"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" />

                <FrameLayout
                    android:id="@+id/tab_4"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" />

            </FrameLayout >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

        </LinearLayout >

    </TabHost >

tab1.xml,tab2.xml,tab3.xml,tab4.xml:

tab1.xml, tab2.xml, tab3.xml, tab4.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</LinearLayout>

tab1.java,tab2.java,tab3.java,tab4.java:

tab1.java, tab2.java, tab3.java, tab4.java:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class tab1 extends Fragment
{
    private static final String TAG = "tab1";

    @Override
    public void onActivityCreated(final Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);

        Log.i(TAG, "onActivityCreated");
    }

    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState)
    {
        Log.i(TAG, "onCreateView");

        return inflater.inflate(R.layout.tab1, container, false);
    }
}

AndroidManifest.xml中:

AndroidManifest.xml:

<activity android:name="<YOUR_PACKAGE_NAME>.TabHostActivity" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

</activity>

这是使用API​​作为目标的V19使用android.support.v4.app的库,并以最小的V10引擎。

This was done using v19 of the API as target, and with a minimum of v10 using the android.support.v4.app library.

所以,再一次谢谢大家对德尼兹让我到这个地步,我已经花了10小时,试图东西,一切我发现已经过时。希望这可以帮助其他人谁是在地狱的我的前状态。您现在应该有一个工作状态,可以看到你可以设计标签,以及在那里你把片段的内容。

So again, thank you deniz for getting me to this point, I had spent 10 hours trying things and everything I found was out of date. Hopefully this can help other people who are in my ex-state of limbo. You should now have a working state and can see where you can design the tabs as well as where you put the contents of the Fragments.

PS - 是的,在我的泛化通过它并导致XML内容,并且被称为标签tab1.whatever,希望这不是太混乱,这是一个可以一点点的上下文推断。

PS - yes, in my generalization pass it did result in the xml content and the tabs being called "tab1.whatever", hopefully this isn't too confusing, which is which can be deduced with a little context.

这篇关于创建使用TabHost和多个片段的Andr​​oid应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆