【问题标题】:How do I use tabHost for Android如何在 Android 上使用 tabHost
【发布时间】:2012-07-22 06:42:37
【问题描述】:

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

【问题讨论】:

  • tabHosts 与 Fragment 之间真的存在关系吗?
  • @AndyHarris TabHost 未被弃用。 TabActivity 是。请停止散布谣言。

标签: java android xml android-tabhost


【解决方案1】:

  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”。

  • Tabhost 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) 是起始页时的默认选项卡。

FirstActivity.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”。

完成!!!!!!

【讨论】:

  • 标签没有出现...为什么?
  • getTabHost() 对我不起作用。它不会作为资源导入。
  • @Eenvincible 你应该有你的活动扩展 TabActivity
  • 我们不应该为每个标签使用片段吗?
  • @SebastienFERRAND 是的,Fragment 总是更好的选择
【解决方案2】:

首先,TabHost 未被弃用,另一方面,TabActivityFragment API 而被弃用。

TabHost 有两种使用方式;通过FragmentTabHost 使用片段并使用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 docs)。

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 style TabLayout class

【讨论】:

    猜你喜欢
    • 2016-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多