5

The problem I'm having is the Action Bar will not show on Android 2.3.7, but will work fine on 4.x+. The rest of my application works fine with the support v7 and v4 libraries, it's just this one area which is giving me trouble.

Here is what it should look like, as seen on 4.3:

Android 4.3 view

And here is what it looks like on 2.3.7:

Android 2.3.7 view

Inside my onCreate method (of the class which inherits from ActionBarActivity), I have this:

    // 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);

And here is my TabListener, an inner class:

/**
 * 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();
        }
    }

}

I have seen these two other questions and attempted to implement the answers, but am still having the issue.

edit: As requested, the theme which is being applied is simply the support library's AppCompat.Light.DarkActionBar theme with no overrides, seen below:

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

2 回答 2

6

如果删除 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();
于 2013-08-14T14:19:03.560 回答
0

你应该阅读官方文件

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

于 2014-03-01T03:21:26.787 回答