18

我看过 Stack Overflow 上的帖子和其他网站上的教程,但我不明白如何使用TabHost. 有人可以向我解释一下,或者给我发一个教程的链接吗?

4

2 回答 2

71

概念 TabHost

在此处输入图像描述

  1. 在 ManiActivity 扩展 TabActivity

    public class MainActivity extends TabActivity {
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        //setContentView(R.layout.activity_main);
    
        TabHost mTabHost = getTabHost();
    
        mTabHost.addTab(mTabHost.newTabSpec("first").setIndicator("First").setContent(new Intent(this  ,FirstActivity.class )));
        mTabHost.addTab(mTabHost.newTabSpec("second").setIndicator("Second").setContent(new Intent(this , SecondActivity.class )));
        mTabHost.setCurrentTab(0);
    
    
    }
    }
    
  • 在这个活动中不要使用布局 "activity_main.xml" 。

  • 标签主机 mTab​​Host = getTabHost(); 是创建主选项卡。

  • mTabHost.newTabSpec("first") 是创建 tabspec id "first"。

  • setIndicator("First") 在标题选项卡中创建文本“First”。

  • setContent(new Intent(this ,FirstActivity.class)) 是使用来自 FirstActivity.class (FirstActivity.java) 的内容

  • mTabHost.addTab(....) 是将 spectab 添加到主选项卡

  • mTabHost.setCurrentTab(0) 是起始页时的默认选项卡。

第一活动.java

public class FirstActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView( R.layout.first_layout );
}

}

SecondActivity.java

public class SecondActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView( R.layout.second_layout );
}
}
  • “R.layout.first_layout”是来自 first_layout.xml 的内容

  • “R.layout.second_layout”是来自 second_layout.xml 的内容

在 AndroidManifest.xml 中,在示例 xml 中添加活动名称“.FirstActivity”和“.SecondActivity”。

在此处输入图像描述

结束!!!!!

在此处输入图像描述

于 2012-07-24T09:17:29.180 回答
6

首先 whileTabHost没有被弃用,TabActivity另一方面由于FragmentAPI 而被弃用。

TabHost 有两种使用方式;通过使用 FragmentFragmentTabHost并使用TabHost.TabContentFactory.

1.通过片段使用FragmentTabHost

此示例代码向您展示如何在 Activity 中使用 TabHost。

FragmentTabHostActivity.java

public class FragmentTabHostActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_tab_host_activity);

        FragmentTabHost fragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        fragmentTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
        fragmentTabHost.addTab(getTabSpec1(fragmentTabHost), Tab1Fragment.class, null);
        fragmentTabHost.addTab(getTabSpec2(fragmentTabHost), Tab2Fragment.class, null);
    }

    private TabHost.TabSpec getTabSpec1(FragmentTabHost tabHost) {
        return tabHost.newTabSpec("First Tab")
            .setIndicator("Tab1");
    }

    private TabHost.TabSpec getTabSpec2(FragmentTabHost tabHost) {
        return tabHost.newTabSpec("Second Tab")
            .setIndicator("Tab 2");
    }
}

fragment_tab_host_activity.xml

<android.support.v4.app.FragmentTabHost
    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: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="wrap_content"
            android:layout_height="wrap_content"/>

    </LinearLayout>

</android.support.v4.app.FragmentTabHost>

实际上,通过使用 Fragment,您可以在 Fragment 中使用 Tab(Android 文档)。

2.使用TabHost.ContentFactory

TabHostActivity.java

public class TabHostActivity extends AppCompatActivity implements TabHost.TabContentFactory {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
        tabHost.setup();

        tabHost.addTab(getTabSpec1(tabHost));
        tabHost.addTab(getTabSpec2(tabHost));
    }

    private TabHost.TabSpec getTabSpec1(TabHost tabHost) {
        return tabHost.newTabSpec("First Tab")
            .setIndicator("Tab1")
            .setContent(this);
    }

    private TabHost.TabSpec getTabSpec2(TabHost tabHost) {
        return tabHost.newTabSpec("Second Tab")
            .setIndicator("Tab 2")
            .setContent(this);
    }

    @Override
    public View createTabContent(String tag) {
        return LayoutInflater.from(this).inflate(R.layout.activity_tab_1, null);
    }
}

activity_main.xml

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

    <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="wrap_content"
            android:layout_height="wrap_content"/>

    </LinearLayout>

</TabHost>

不过,我个人推荐使用最新的Material Design 风格TabLayout

于 2017-05-13T04:21:34.693 回答