【问题标题】:How to add an activity to tab?如何将活动添加到选项卡?
【发布时间】:2012-02-09 09:48:09
【问题描述】:

我已经开始在 Tablayout 中自定义选项卡,所以我按照本教程进行操作 http://joshclemm.com/blog/?p=136 效果很好。但问题是我无法找到如何在本教程中为每个选项卡添加活动,我尝试过但没有找到解决方案。

一般我们使用

    TabSpec spec = tabHost.newTabSpec("Photos");
    spec.setIndicator("Photos");

    Intent photos = new Intent(this, Photos.class);
    spec.setContent(photos);

但不知道在这种情况下如何添加活动。

谁能帮我解决这个问题?

谢谢。

【问题讨论】:

    标签: android tabs customization


    【解决方案1】:

    【讨论】:

      【解决方案2】:
          Resources res = getResources(); // Resource object to get Drawables
              TabHost tabHost = getTabHost(); // The activity TabHost
              TabHost.TabSpec spec; // Resusable TabSpec for each tab
              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, DemoActivity1.class);
              // Initialize a TabSpec for each tab and add it to the TabHost
              spec = tabHost.newTabSpec("todaystake").setIndicator("Todays Take",
                      res.getDrawable(R.drawable.icontodaystake)).setContent(intent);
              tabHost.addTab(spec);
      
              // Do the same for the other tabs
              intent = new Intent().setClass(this, DemoActivity2.class);
              spec = tabHost.newTabSpec("whatscasting").setIndicator(
                      "What's Casting", res.getDrawable(R.drawable.iconwhatscasting))
                      .setContent(intent);
              tabHost.addTab(spec);
      tabHost.setCurrentTab(0);
      

      您的 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="fill_parent"
          android:layout_height="fill_parent">
          <RelativeLayout android:layout_width="fill_parent"
              android:layout_height="fill_parent">
              <FrameLayout android:id="@android:id/tabcontent"
                  android:layout_width="fill_parent" android:layout_height="fill_parent"
                  android:layout_above="@android:id/tabs" />
              <TabWidget android:id="@android:id/tabs"
                  android:layout_alignParentBottom="true" android:layout_width="fill_parent"
                  android:layout_height="wrap_content" />
          </RelativeLayout>
      </TabHost>
      

      【讨论】:

      • 在你的主类中添加这个,你在 xml 中添加你的标签主机
      • 感谢您的帮助,但请检查我在问题中提到的链接并提供答案。
      • 以上代码将创建标签页,每个标签页包含一个不同的活动类,它不用于创建自定义标签页
      • 我想自定义标签,所以我使用了该链接提供的源代码
      • 给我你的邮件ID,我会给你一个你需要的定制代码
      【解决方案3】:

      你在正确的轨道上 - Photos.class 只需要成为一个活动。

      参见例如this TabActivity 我是如何与 Zwitscher 合作的

      【讨论】:

      • 谢谢。你看到那个教程中的代码了吗,你能告诉我在那个教程代码的哪里写,我很困惑......@Heiko Rupp
      【解决方案4】:

      您只需将单词 photos 替换为您的名称活动类的其他名称。 但请记住,您必须在 androidmanifest 文件中注册您的活动。

      【讨论】:

      • 示例我创建了一个名为 test.class 的活动然后我打开 AndroidManifest,在 AndroidManifest.xml 选项卡中,我将添加一些如下代码行: 意图过滤器>
      猜你喜欢
      • 2019-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多