【问题标题】:ActionBar with support library and Fragments overlay content带有支持库和 Fragments 覆盖内容的 ActionBar
【发布时间】:2015-06-17 20:15:44
【问题描述】:

我将 android-support-library-v7-appcompat 添加到我的项目中,以支持上述 API 级别 7 的 ActionBar。

它在 android 4.0+ 和 android 2.3 上都像一个魅力,在 onCreate 中具有 setContentView 的普通 Activity,但是当 Activity 在 onCreate 中加载 Fragment 时,ActionBar 与我的布局内容重叠。在所有其他情况下,ActionBar 运行良好。

这里有一些代码:

class AssetsActivity extends ActionBarActivity{
    @Override
    protected void onCreate(Bundle arg0)
    {
        super.onCreate(arg0);

        OpenLocalFragment assets = OpenLocalFragment.newInstance(index);
            assets.setArguments(getIntent().getExtras());
        getSupportFragmentManager().beginTransaction()
                    .add(android.R.id.content, assets).commit();
    }
 }

该活动的主题在清单中设置为:

@style/Theme.AppCompat

这是在 android 2.3 上的结果(在 4.0+ 上,ActionBar 显示良好)

您可以看到第一个 lisview 项目与 ActionBar 重叠(白色圆形图标和标题“我的活动”)

可能是我在支持库中发现了一个错误,它现在只发布了 2 天? 谢谢大家。

【问题讨论】:

  • 问题解决了,请看下面我的回答。

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


【解决方案1】:

编辑: 此问题现已正式修复并在 Support Library v19 中发布。

正如 JJD 在下面评论的那样,您可以正常使用 android.R.id.content 和 appcompat-v7 r.19.0.0 或更高版本。主页按钮也可以。

换句话说:如果您使用 19.0.0 或更高版本,则不再需要以下解决方法。


我在code.google.com 得到了答案。我根据弗雷德里克的回答做了一个总结:

对于前 ICS 设备,您必须使用:

R.id.action_bar_activity_content

而不是

android.R.id.content

R.id.action_bar_activity_content是布局中用于显示应用内容的新id,当您使用support v7 appcompat ActionBarActivity时,它看起来会取代android.R.id.content。 p>

您可以使用此代码检索活动内容的正确 id:

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;
}

感谢弗雷德里克

【讨论】:

  • 注意:这现在正式成为 Android 中的一个错误。他们修复了它,它将在未来发布。 code.google.com/p/android/issues/detail?id=58108
  • 请注意,使用 appcompat-v7 r.19.0.0 会引入 new bug affecting devices running Android 2.3.x as discussed in issue #59077。
  • 除了 Primoz990 答案:如果您使用 appcompat-v7 r.19.0.0 或更新版本,您可以使用 android.R.id.content。 Chris Banes fixed issue 59077 所以主页按钮适用于 Android 2.3 及更高版本。您不再需要引用 R.id.action_bar_activity_content 的解决方法。
  • 我正在使用支持库版本 19.0.1,但该片段仍被覆盖。试图查看解决方法是否可行,但如果我在 Android 2.3 的 R.id.action_bar_activity_content 上使用 replace(),我得到一个“未找到 id 0x7f050015 (mypackage:id/action_bar_activity_content) 的视图”。我可以尝试什么?编辑:我刚刚使用了八十赫兹的解决方案,添加了一个空的 LinearLayout 并在那里添加了视图
  • 您可以尝试在片段交易中添加 .addToBackStack(null)。如果您将片段 A 替换为片段 B(addtobackstack),而不是将 B 替换为 C(无回栈),然后单击后退按钮,我会遇到一些问题。 C 没有暂停,A 显示为与 C 重叠。
【解决方案2】:

如果您不想修改 android-support-library-v7-appcompat 的源代码,另一种选择是在布局 xml 文件中添加一个空布局,例如:

<LinearLayout
    android:id="@+id/content_view"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

并进行片段调用以添加到该布局:

ft.add(R.id.content_view, mFragment, mTag);

【讨论】:

    【解决方案3】:

    似乎有点晚了,但我遇到了同样的问题,在这里没有看到答案....

    检查你的styles.xml文件,可能有一个xml属性用于覆盖actionbar设置为true ie

    是的

    整个条目看起来像这样

        <item name ="actionBarTabTextStyle" > @style/TabTextStyle</item>
        <item name = "windowActionBarOverlay">true</item>
    

    如果是这种情况,只需将“windowActionBarOverlay”的值更改为 false。

    【讨论】:

      【解决方案4】:

      添加一个带有操作栏大小的额外空列表标题。应该是一个很好的解决方法。

      【讨论】:

      • 在这种情况下,这是一个简单的解决方法,但我在许多具有不同布局的活动中使用此原则。我将使用我当前的解决方法发布一个编辑。无论如何,谢谢,可能是某人的解决方案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多