我遇到的问题是Action Bar不会在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>
答案 0 :(得分:6)
如果删除DetailFragment的背景,则ActionBar和Tabs实际显示在DetailFragment后面。而不是android.R.id.content,在主布局中创建自己的容器,并在FragmentTransaction中调用replace时使用R.id.yourcontent。通过进行此更改,它在2.3.3和4 +上为我工作。
似乎2.3.3将ActionBar添加到根视图元素,其中4+将其添加到根视图之外。
sft.replace(R.id.yourcontent, mFragment).commit();
答案 1 :(得分:0)
您应该阅读offcial document。
您不能在每个回调中为片段事务调用commit() - 系统会为您调用它,如果您自己调用它,它可能会抛出异常。您也无法将这些片段事务添加到后台堆栈。