【问题标题】:Android TabHost - Activities within each tabAndroid TabHost - 每个选项卡中的活动
【发布时间】:2010-06-23 15:26:55
【问题描述】:

我正在尝试创建多个选项卡,每个选项卡都有不同的活动。唯一的缺点是我使用的是自定义布局文件,因此我的类扩展了 Activity 而不是 TabActivity。尝试运行时失败并建议调用TabHost.Setup(ActivityGroupManager agm)

任何人有如何实现这一点的想法/实际示例?

提前致谢

【问题讨论】:

  • "...每个都有不同的 Activity" - 你可能是指不同的“视图”。
  • 扩展 TabActivity 只需要用于承载选项卡的主 Activity。选项卡中的所有其他活动都可以简单地扩展 Activity,它们会运行得很好。

标签: android android-activity android-tabhost


【解决方案1】:

这是我的活动示例,也没有从 TabActivity 扩展:

protected TabHost tabs;

// ...

/**
 * Init tabs.
 */
private void initTabs() {
    tabs = (TabHost) findViewById(R.id.tabhost);
    tabs.setup();
    tabs.setBackgroundResource(R.drawable.bg_midgray);

    TabHost.TabSpec spec;

    // Location info
    txtTabInfo = new TextView(this);
    txtTabInfo.setText("INFO");
    txtTabInfo.setPadding(0, 0, 0, 0);
    txtTabInfo.setTextSize(14);
    txtTabInfo.setBackgroundResource(R.drawable.bg_tab_left_inactive_right_inactive);
    txtTabInfo.setTextColor(Color.DKGRAY);
    txtTabInfo.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.TOP);
    txtTabInfo.setHeight(39);
    spec = tabs.newTabSpec("tabInfo");
    spec.setContent(R.id.tabInfo);
    spec.setIndicator(txtTabInfo);
    tabs.addTab(spec);

    // Maps
    txtTabMap = new TextView(this);
    txtTabMap.setText("MAP");
    txtTabMap.setTextSize(14);
    txtTabMap.setPadding(0, 0, 0, 0);
    txtTabMap.setBackgroundResource(R.drawable.bg_tab_middle_inactive_right_active);
    txtTabMap.setTextColor(Color.DKGRAY);
    txtTabMap.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.TOP);
    txtTabMap.setHeight(39);
    spec = tabs.newTabSpec("tabMap");
    spec.setContent(R.id.tabMap);
    spec.setIndicator(txtTabMap);
    tabs.addTab(spec);

    tabs.setCurrentTab(0);

    tabs.setOnTabChangedListener(this);
}

// ...

【讨论】:

    【解决方案2】:

    首先,在主布局中定义一个frametab。

    <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:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dp">
        <tabwidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content">
          <framelayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dp">
          </framelayout>
        </tabwidget>
    </linearlayout>
    </tabhost>
    

    然后,创建一个从 TabActivity 扩展的活动

    Resources res = getResources();
    TabHost tabHost = getTabHost();
    TabHost.TabSpec spec;
    Intent intent;
    intent = new Intent().setClass(this, DashboardActivity.class);
    spec = tabHost.newTabSpec("home").setIndicator("Home", res.getDrawable (R.drawable.ic_tab_dashboard)).setContent(intent);
    tabHost.addTab(spec);
    intent = new Intent().setClass(this, CreditCardActivity.class);
    spec = tabHost.newTabSpec("sample1").setIndicator("Sample Tab",res.getDrawable (R.drawable.ic_tab_sample1)).setContent(intent);
    tabHost.addTab(spec);
    

    如果你想移动标签,使用选择器布局:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:drawable="@drawable/helpblue" android:state_selected="true">
      <item android:drawable="@drawable/helpgray"></item>
    </item></selector>
    

    这里是示例截图。

    alt text http://rayyildiz.com/wp-content/uploads/2010/06/android_sample_tab-201x300.pngalt text http://rayyildiz.com/wp-content/uploads/2010/06/android_sample_tab2-201x300.png

    【讨论】:

      【解决方案3】:

      创建一个扩展 TabActivity 的附加类,并将该类作为主要活动。

      要在您的 XML 清单中执行此操作,您需要包括:

      <activity android:name=".TabActivtyClass" android:label="@string/app_name"
          android:theme="@android:style/Theme.NoTitleBar">
          <intent-filter>
              <action android:name="android.intent.action.MAIN" />
              <category android:name="android.intent.category.LAUNCHER" />
          </intent-filter>
      </activity>
      

      在这门课中你会写这样的东西:

      public class TabActivtyClass extends TabActivity {
      
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
              TabHost tabHost = getTabHost(); // The associated TabHost
      
              // Create an Intent to launch given Activty for this tab
              Intent i = new Intent().setClass(this, FirstActivty.class);
              TabHost.TabSpec spec = tabHost.newTabSpec("tab_name").setIndicator("Tab Name").setContent(i); // <- references the intent we just created
              tabHost.addTab(spec);
      
              // And do the same for the other tabs ...
          }
      }
      


      这个 TabActivty 类可以是大或小,但通常它会是全屏,每个选项卡的 Activity 都被加载到屏幕的主要部分,如下所示:
      (来源:android.com)


      附:另请注意,Eclipse 布局编辑器不适用于选项卡。是a bug which has already been logged。

      【讨论】:

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