AppCompat操作栏库不显示添加片段 [英] AppCompat Action Bar library not displaying added fragments

查看:151
本文介绍了AppCompat操作栏库不显示添加片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

修改:如果我延长 FragmentActivity 而不是 ActionBarActivity 我的布局,显示再次(没有动作条当然)。

动作条按预期工作的 4.x版设备,但是在我的 2.3 设备我得到的是动作条和它下面一个黑屏。该片段似乎并没有被获取添加到活动

的themes.xml

 <样式名称=AppTheme父=AppBaseTheme>
    <项目名称=actionBarStyle> @风格/ Widget.ActionBar< /项目>
< /风格>
 

主题-v11.xml

 <样式名称=AppTheme父=AppBaseTheme>
    <项目名称=机器人:actionBarStyle> @风格/ Widget.ActionBar< /项目>
< /风格>
 

styles.xml

 <样式名称=Widget.ActionBar父=@风格/ Widget.AppCompat.ActionBar>
    <项目名称=机器人:背景> @色/ actionbar_background< /项目>
    <项目名称=背景> @色/ actionbar_background< /项目>
< /风格>
 

活动的onCreate()

  FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
myFragment =新WallFragment();
fragmentTransaction.add(android.R.id.content,
    myFragment,myFragment.FRAGMENT_TAG);
fragmentTransaction.commit();
 

我使用的摇篮,包括在我的应用程序的 AppCompat动作条库。

 编译com.android.support:appcompat-v7:18.0.+
 

解决方案

加载了在Android SDK的工具目录 hierarchyviewer ,似乎认为你把片段是 android.R.id.content 的4.x的设备,是 R.id.action_bar_activity_content 2.3上运行的设备在AppCompat动作条库。

不幸的是,看来你可能会添加片段时,需要根据分支关闭平台的版本。此建议在<一href="http://$c$c.google.com/p/android/issues/detail?id=58108">http://$c$c.google.com/p/android/issues/detail?id=58108 (不知道3.x设备还)。

使用这种方法来获取正确的视图中添加片段的。我的测试也表明,3.x设备的行为类似于2.3的设备使用AppCompat动作条库时。

 公共静态INT getContentViewCompat(){
    返回Build.VERSION.SDK_INT&GT; = Build.VERSION_ codeS.ICE_CREAM_SANDWICH?
               android.R.id.content:R.id.action_bar_activity_content;
}
 


hierarchyviewer截图

  • 2.3

  • 4.x版

EDIT: If I extend FragmentActivity instead of ActionBarActivity my layout shows up again (without an ActionBar of course).

The ActionBar works as intended on 4.x devices, but on my 2.3 device all I get is the ActionBar and a blank screen below it. The Fragment doesn't seem to be getting added to the Activity.

themes.xml

<style name="AppTheme" parent="AppBaseTheme">
    <item name="actionBarStyle">@style/Widget.ActionBar</item>
</style>

themes-v11.xml

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:actionBarStyle">@style/Widget.ActionBar</item>
</style>

styles.xml

<style name="Widget.ActionBar" parent="@style/Widget.AppCompat.ActionBar">
    <item name="android:background">@color/actionbar_background</item>
    <item name="background">@color/actionbar_background</item>
</style>

Activity onCreate()

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
myFragment = new WallFragment();
fragmentTransaction.add(android.R.id.content, 
    myFragment, myFragment.FRAGMENT_TAG);
fragmentTransaction.commit();

I am using Gradle to include the AppCompat ActionBar library in my app.

compile 'com.android.support:appcompat-v7:18.0.+'

解决方案

Loading up the hierarchyviewer in the Android SDK's tools directory, it appears that the view you place fragments in is android.R.id.content on 4.x devices and is R.id.action_bar_activity_content on 2.3 devices running the AppCompat ActionBar library.

Unfortunately it seems that you might need to branch based off platform versions when adding fragments. This is suggested in http://code.google.com/p/android/issues/detail?id=58108 (not sure about 3.x devices yet).

Use this method to get the proper view for adding Fragment's. My testing also shows that 3.x devices act similar to 2.3 devices when using the AppCompat ActionBar library.

public static int getContentViewCompat() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH ?
               android.R.id.content : R.id.action_bar_activity_content;
}


hierarchyviewer screenshots

  • 2.3

  • 4.x

这篇关于AppCompat操作栏库不显示添加片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆