【问题标题】:ActionBar Tabs with support library带有支持库的 ActionBar 选项卡
【发布时间】:2014-03-01 11:21:26
【问题描述】:

我遇到的问题是操作栏不会在 Android 2.3.7 上显示,但在 4.x+ 上可以正常工作。我的应用程序的其余部分可以与支持 v7 和 v4 库一起正常工作,只是这一方面给我带来了麻烦。

这是它的样子,如 4.3 所示:

这是 2.3.7 上的样子:

在我的 onCreate 方法(继承自 ActionBarActivity 的类)中,我有这个:

    // setup action bar for tabs
    ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setDisplayShowTitleEnabled(true);


    Tab tab = actionBar.newTab()
            .setText(R.string.details)
            .setTabListener(new TabListener<DetailsFragmentOne>(
                    this, "one", DetailsFragmentOne.class));
    actionBar.addTab(tab);

    tab = actionBar.newTab()
        .setText(R.string.grades)
        .setTabListener(new TabListener<DetailsFragmentTwo>(
                this, "one", DetailsFragmentTwo.class));
    actionBar.addTab(tab);

这是我的 TabListener,一个内部类:

/**
 * This is copied almost verbatim from <a href="http://developer.android.com/guide/topics/ui/actionbar.html#Tabs">the ActionBar Tabs API Guide</a>.
 * @param <T>
 */
public class TabListener<T extends Fragment> implements ActionBar.TabListener {
    private Fragment mFragment;
    private final Activity mActivity;
    private final String mTag;
    private final Class<T> mClass;

    /** Constructor used each time a new tab is created.
      * @param activity  The host Activity, used to instantiate the fragment
      * @param tag  The identifier tag for the fragment
      * @param clz  The fragment's Class, used to instantiate the fragment
      */
    public TabListener(Activity activity, String tag, Class<T> clz) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
    }

    /* The following are each of the ActionBar.TabListener callbacks */

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        FragmentTransaction sft = ((FragmentActivity) mActivity).getSupportFragmentManager().beginTransaction();
        mFragment = getSupportFragmentManager().findFragmentByTag(mTag);
        // Check if the fragment is already initialized
        if (mFragment == null) {
            // If not, instantiate and add it to the activity
            mFragment = Fragment.instantiate(mActivity, mClass.getName());
            // calling commit() here because we're not using the provided FragmentTransaction
            sft.replace(android.R.id.content, mFragment, mTag).commit();
        } else {
            // If it exists, simply attach it in order to show it
            // calling commit() here because we're not using the provided FragmentTransaction
            sft.replace(android.R.id.content, mFragment).commit();
        }
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        FragmentTransaction sft = ((FragmentActivity) mActivity).getSupportFragmentManager().beginTransaction();
        mFragment = getSupportFragmentManager().findFragmentByTag(mTag);
        if (mFragment != null) {
             // calling commit() here because we're not using the provided FragmentTransaction
            sft.replace(android.R.id.content, mFragment).commit();
        }
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {          
        FragmentTransaction sft = ((FragmentActivity) mActivity).getSupportFragmentManager().beginTransaction();
        mFragment = getSupportFragmentManager().findFragmentByTag(mTag);
        if (mFragment != null) {
             // calling commit() here because we're not using the provided FragmentTransaction
            sft.replace(android.R.id.content, mFragment).commit();
        }
    }

}

我已经看到这两个其他问题并尝试实施答案,但仍然遇到问题。

编辑: 根据要求,正在应用的主题只是支持库的 AppCompat.Light.DarkActionBar 主题,没有覆盖,如下所示:

<style name="Theme.MyTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
</style>

【问题讨论】:

  • 您的应用使用哪个主题?
  • @Ahmad,它使用的是 Action Bar 兼容主题。就像我提到的那样,我在应用程序中有其他使用 ActionBar 的视图并且工作正常,它只是一个没有显示选项卡的视图。
  • 您能粘贴 4.x+ 和 2.3.7 的样式文件吗?
  • @OnurA。我已经包含了主题。
  • 两者的风格是否相同?我的意思是 value-v11 和 values-v14 文件夹的样式文件是相同的?

标签: android android-fragments android-support-library


【解决方案1】:

如果删除 DetailFragment 的背景,ActionBar 和 Tabs 实际上会出现在 DetailFragment 后面。在主布局中创建自己的容器,而不是 android.R.id.content,并在 FragmentTransaction 中调用替换时使用 R.id.yourcontent。通过进行此更改,它在 2.3.3 和 4+ 上对我有用。

似乎 2.3.3 将 ActionBar 添加到根视图元素,而 4+ 将其添加到根视图之外。

sft.replace(R.id.yourcontent, mFragment).commit();

【讨论】:

  • 感谢您的回答。为我节省了很多工作。真的很棒。
【解决方案2】:

您应该阅读offcial document

您不能在每个回调中为片段事务调用 commit() - 系统会为您调用它,如果您自己调用它可能会抛出异常。您也不能将这些片段事务添加到后台堆栈。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-13
    • 1970-01-01
    • 2012-07-24
    • 1970-01-01
    • 1970-01-01
    • 2012-02-29
    相关资源
    最近更新 更多