【问题标题】:How to assign activities as tab content?如何将活动分配为选项卡内容?
【发布时间】:2012-01-26 22:34:21
【问题描述】:

我在互联网上找到了这个自定义的 TabWidget 布局。问题是,我不知道如何将内容插入选项卡。有人可以帮忙吗?假设我有活动 1、活动 2 和活动 3。代码如下:

public class MainActivity extends Activity {
private TabHost mTabHost;

private void setupTabHost() {
    mTabHost = (TabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup();
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    setupTabHost();
    mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);

    setupTab(new TextView(this), "EASY");
    setupTab(new TextView(this), "MEDIUM");
    setupTab(new TextView(this), "INTENSE");
}

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

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

}

private static View createTabView(final Context context, final String text) {
    View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
    TextView tv = (TextView) view.findViewById(R.id.tabsText);
    tv.setText(text);
    return view;
}
}

【问题讨论】:

    标签: android android-activity tabs android-listview tabwidget


    【解决方案1】:

    您通过制作自己的文件、在清单中声明它们并将它们的 onCreate 放入setContentView(R.layout.activitylayout); 来将内容放入活动中。如果内容是静态的,则在适当的布局中完整描述。

    好的,明白了。然后返回 view 这里 public View createTabContent(String tag) {return view;} 你想放在那里的布局。一些 listViewOfMyPictures 或其他东西。

    【讨论】:

    • 嗨,谢谢你的回答。我确实了解如何将活动设置为内容。我浏览了 android 开发网站上的教程。但这是一个自定义标签小部件,其代码与教程中的代码不同。你能准确地告诉我我是如何在我的代码中为 3 个不同的活动编写“setContent”部分的吗?
    • 我将在此处作为视图返回 public View createTabContent(String tag) {return view;} 您想要放置在那里的布局。你没试过吗?
    • 不正确。你应该说你看到了什么错误,它是如何工作的。没有人会在这里做你的工作。要求这个是非常不礼貌的。我们都有自己的工作要做。
    【解决方案2】:

    为特定的 Activity 创建一个意图,并通过“setContent”将意图添加到 tabSpec,而不是您当前创建 TabContentFactory 的位置 - 就像这样(复制/粘贴自 Android 开发指南中的 Hello Tabwidget 教程) :

    Intent intent;  // Reusable Intent for each tab
    
    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, ArtistsActivity.class);
    
    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("artists").setIndicator("Artists",
                      res.getDrawable(R.drawable.ic_tab_artists))
                  .setContent(intent);
    tabHost.addTab(spec);
    

    【讨论】:

      【解决方案3】:

      尝试查看TabHost.TabSpec 的文档,尤其是setContent(Intent intent)

      创建诸如Intent tabIntentA = new Intent(this, SomeActivity.class); 之类的意图并将它们传递给setContent(...) 方法。

      编辑:

      查看Tab Layout tutorial 的第 6 部分 - 特别是这段代码...

      Intent intent;  // Reusable Intent for each tab
      
      // Create an Intent to launch an Activity for the tab (to be reused)
      intent = new Intent().setClass(this, ArtistsActivity.class);
      
      // Initialize a TabSpec for each tab and add it to the TabHost
      spec = tabHost.newTabSpec("artists").setIndicator("Artists",
                          res.getDrawable(R.drawable.ic_tab_artists))
                          .setContent(intent);
      tabHost.addTab(spec);
      

      无论如何,您最好还是遵循该教程而不是您在问题中发布的代码。

      【讨论】:

      • 你能看看我的代码并告诉我我必须在哪里输入这些吗?
      • 无法让它工作。你能复制我的代码,粘贴你的代码并在这里发布吗?如果可行,我会接受你的回答.. -_-
      • 问题是教程没有提供自定义tabwidget指示器的方法。使用我提供的代码,我成功地将可绘制对象用作 tabwidgets 指示器。如果有任何方法可以在常规教程代码中使用自定义选项卡指示器,我会从一开始就使用。
      • @borislemke:所以您是在认真地说您不能将我链接的教程中的示例与您在问题中发布的代码结合起来?如果是这种情况,那么您在编写任何代码时都会遇到问题。没有人在这里为您编写代码 - 我们都必须为自己思考并学习如何解决问题。您的问题标题询问如何将活动用作选项卡内容 - 我已经解释过了。如果您不想接受我的回答或其他任何人的回答,那也没关系。无论哪种方式,学习如何试验东西以了解它是如何工作的。
      【解决方案4】:

      我也遇到了同样的问题,通过修改下面给出的代码解决了。

      setupTab1(new TextView(this), "MEDIUM");
      setupTab2(new TextView(this), "INTENSE");
      
      
      private void setupTab1(final View view, final String tag) {
      
      View tabview = createTabView(mTabHost.getContext(), string);
      
      Intent intent1 = new Intent().setClass(this, DummyActivity1.class);
      TabSpec tab1 = mTabHost
          .newTabSpec("TAB1")
          .setIndicator(tabview)
          .setContent(intent1);
      
      mTabHost.addTab(tab1);
      

      }

      private void setupTab2(final View view, final String tag) {
      View tabview = createTabView(mTabHost.getContext(), string);
      
      Intent intent2 = new Intent().setClass(this, DummyActivity2.class);
      TabSpec tab2 = mTabHost
          .newTabSpec("TAB2")
          .setIndicator(tabview)
          .setContent(intent2);
      
      mTabHost.addTab(tab2);
      

      }

      以前我们为所有 Tabview 设置了 setupTab() 方法。现在我们有不同活动的不同 setupTab() 方法。

      它对我有用..!希望这对您有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多