【问题标题】:ActionBarActivity with TabHost带有 TabHost 的 ActionBarActivity
【发布时间】:2013-09-09 17:44:03
【问题描述】:

我正在开发一个应用程序,最低 sdk 级别为 7,这就是为什么我使用支持库和 ActionBarActivity 来拥有一个适当的 ActionBar,而且我的页面布局在活动底部包含四个选项卡,因为我已经扩展 ActionBarActivity,我无法扩展 TabActivity。 问题是我写的时候

public class HomePageActivity extends ActionBarActivity {
private TabHost mTabHost;

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

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

    TabHost.TabSpec tab1 = mTabHost.newTabSpec("TAB1");
    tab1.setIndicator("TAB 1");

    Intent popularContentIntent = new Intent().setClass(this, GuideActivity.class);
    tab1.setContent(popularContentIntent);
    mTabHost.addTab(tab1);

当我尝试将选项卡添加到选项卡主机时,我收到错误“您是否忘记调用 'public void setup(LocalActivityManager activityGroup)?'”。 如果我尝试在 tab.setContent(..) 中设置视图的 ID,如 tab.setContent(R.id.tab1) 并在 xml 中有一个 ID 为 tab1 的视图,我会收到该视图的错误找不到这样的号码。

我的布局:

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@android:id/tabs"
        android:layout_alignParentTop="true" />

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        </Button>
    </TabWidget>
</RelativeLayout>

是否有可能以某种方式同时使用选项卡和操作栏进行布局?我做错了什么?还有什么其他选择? 非常感谢!

【问题讨论】:

  • 在拨打addTab(...)之前,您必须先拨打mTabHost.setup()
  • @Squonk - 这就是我所做的: mTab​​Host = (TabHost) findViewById(android.R.id.tabhost); mTabHost.setup();
  • 抱歉,我略读了您的帖子,直接进入了所报告的错误。
  • 除了您在问题中提到的错误之外,您是否在 logcat 中看到任何错误?
  • @Squonk 不,不幸的是:(

标签: android android-actionbar android-tabhost android-tabs android-support-library


【解决方案1】:

TabActivity 在 API 级别 13 中已弃用。更新后的 documentation 建议改用 android.support.v4.app.FragmentTabHost

更好的方法是使用ViewPager 提供的here 示例。

【讨论】:

  • OP 没有使用TabActivity - 他们使用的是ActionBarActivity
  • 我需要在页面底部有四个固定标签,ViewPager 不允许我有这样的布局。
  • 就像我说的,使用android.support.v4.app.FragmentTabHostViewPager 只是另一种选择。
  • 我只是指出了TabActivity,因为文档建议了将其与 ActionBar 和片段一起使用的替代方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-18
  • 1970-01-01
  • 2012-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多