【问题标题】:Overlaying the Action Bar not working覆盖操作栏不起作用
【发布时间】:2015-03-16 11:20:05
【问题描述】:

我正在关注 official developer's guide 以覆盖操作栏。

我的style.xml如下:

<!-- Base application theme. -->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
</style>

<style name="AppTheme" parent="AppBaseTheme">
    <item name="actionBarStyle">@style/CustomActionBarTheme</item>

</style>

<style name="CustomActionBarTheme"
    parent="@android:style/Theme.Holo">
    <item name="android:windowActionBarOverlay">true</item>
</style>

我的 midSdkVersion 是 14,预期输出类似于官方指南:。

相反,在我的情况下,输出是:

(我已经为活动设置了背景颜色...但它没有覆盖操作栏。)

如果我做错了什么,请帮助我。

编辑:

我想要在 airnb 和许多其他应用程序中使用类似的操作栏。谁能给我完整的答案?

【问题讨论】:

  • 请检查您的清单文件。您可能使用了错误的主题而不是 CustomActionBarTheme。
  • @iDroidExplorer 你能用代码写一个完整的答案吗?我会很感激的。谢谢。
  • 您将操作栏设置为覆盖,但您没有将操作栏的背景设置为透明。尝试添加&lt;item name="android:background"&gt;@drawable/actionbar_background&lt;/item&gt;@drawable/actionbar_background 是一个半透明的png 图像
  • @Krupal 你能用你的清单文件的代码更新你的问题吗,这样我就可以知道我要问的事情......

标签: android android-layout android-actionbar android-styles


【解决方案1】:

我在您的代码中发现了一些误解:

  1. windowActionBarOverlay 应该在您的主题中指定,而不是在您的 ActionBar 的样式中。
  2. 没有理由将Holo 与支持主题一起使用。这只会破坏您的支持能力。

试试这个:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <item name="android:windowActionBarOverlay">true</item>
    <!--For compatibility-->
    <item name="actionBarStyle">@style/MyActionBar</item>
    <item name="windowActionBarOverlay">true</item>
</style>

<color name="transparent_black">#80000000</color>
<style name="MyActionBar" parent="Widget.AppCompat.ActionBar.Solid">
    <item name="android:background">@color/transparent_black</item>
    <!--For compatibility-->
    <item name="background">@color/transparent_black</item>
</style>

【讨论】:

  • 我不同意你的第一点。因为它明确提到应该声明主题样式,您可以像下面这样使用它:developer.android.com/guide/topics/ui/themes.html#ApplyATheme
  • 我已经使用了您的解决方案,并且运行良好。但是ActionBar 的标题不见了。你能帮忙吗?
  • 这简直太完美了!
  • 为布局添加内边距 android:paddingTop="?android:attr/actionBarSize"
  • “为了兼容性”部分起到了作用,即使在具有 API 版本 23 的设备上运行时也是如此。android studio 生成的默认全屏活动没有添加!
【解决方案2】:

我想你错过了developer guide的这一点

启用叠加模式

仅适用于 Android 3.0 及更高版本

style.xml

<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme"
           parent="@android:style/Theme.Holo">
        <item name="android:windowActionBarOverlay">true</item>
    </style>
</resources>

适用于 Android 2.1 及更高版本

<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme"
           parent="@android:style/Theme.AppCompat">
        <item name="android:windowActionBarOverlay">true</item>

        <!-- Support library compatibility -->
        <item name="windowActionBarOverlay">true</item>
    </style>
</resources>

指定布局上边距

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?android:attr/actionBarSize">
    ...
</RelativeLayout>

我有两个示例链接,请查看它们以获得更多说明。

Android Tutorial: Overlay with User Instructions

Pushing the ActionBar to the Next Level

【讨论】:

  • 完美,如果有人在使用Sliding Tabs并想在滚动时显示和隐藏ActionBar,这里的关键是android:paddingTop="?android:attr/actionBarSize"
【解决方案3】:

试试这个:

<style name="AppBaseTheme" parent="android:style/Theme.Holo.Light.DarkActionBar">
</style>

<style name="OverlayActionBarTheme" parent="AppBaseTheme">
    <item name="android:windowActionBarOverlay">true</item>
    <item name="android:actionBarStyle">@style/Holo.ActionBar.Overlay</item>
</style>

<style
    name="Holo.ActionBar.Overlay"
    parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
    <item name="android:background">#80000000</item>
</style>

【讨论】:

    【解决方案4】:

    搜索了两个小时后,我觉得有必要在此处添加此答案。

    对我来说,诀窍是将以下所有 6 行添加到 styles.xml

        <item name="android:windowActionModeOverlay">true</item>
        <item name="android:windowActionBarOverlay">true</item>
        <item name="android:windowActionBar">true</item>
    
        <item name="windowActionModeOverlay">true</item>
        <item name="windowActionBarOverlay">true</item>
        <item name="windowActionBar">true</item>
    

    据我了解,所有这些行都做同样的事情,但不同的 API 级别只侦听某些行。我应该提到我使用最低 API 级别 19。

    【讨论】:

      【解决方案5】:

      您刚刚错过了您需要在应用程序主题中设置覆盖,如 documention 中所述

       <!-- the theme applied to the application or activity -->
      

      如果问题没有解决,请反馈;)

      【讨论】:

        【解决方案6】:

        我完全自定义视图,因此,活动布局将填满整个屏幕,并将自定义 LinearLayout 放在顶部,所有按钮和图标都膨胀

        【讨论】:

          猜你喜欢
          • 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
          相关资源
          最近更新 更多