android - 为什么我的 Action Bar 不叠加?

标签 android android-layout android-actionbar

我是 Android 开发新手。我已经在以下位置学习了 Android Overlay 培训:

http://developer.android.com/training/basics/actionbar/overlaying.html

并在过去的几天里努力让它发挥作用。我正在使用 AppCompat 库,我的最小 SDK 是 7,我的最大 SDK 是 18。

我的应用程序定义了一个自定义样式,如下所示:

<application

android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/CustomActionBarTheme" >

<activity
android:name="com.example.rtrt.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

我的布局是这样的:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/clickme"
android:onClick="sendMessage" />
</RelativeLayout>

我的 theme.xml 看起来像这样:

<style name="CustomActionBarTheme"  
parent="@style/Theme.AppCompat.Light">
<item name="android:actionBarStyle">@style/MyActionBar</item> 
</style>      <!-- ActionBar styles -->

<style name="MyActionBar"            
parent="@style/Widget.AppCompat.Light.ActionBar">

<item name="android:background">@android:color/transparent</item>

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

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

应用程序编译并运行...但是,Button 出现在ActionBar 下方(而不是下方)。当我隐藏 ActionBar 时,Button 移动到屏幕顶部。当 ActionBar 可见时,Button 将重新定位在屏幕下方。

为什么我的 ActionBar 没有 float (叠加)?

在此感谢任何帮助。

最佳答案

这是因为您的目标是低于 SDK 11。

将您的主题更改为 AppCompat 而不是 AppCompat.Light:

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

关于android - 为什么我的 Action Bar 不叠加?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20812933/

相关文章:

android - popBackStack 后 TextWatcher 过滤器无法正常工作

Android getActionBar 与 getSupportActionBar?

android - 如何避免剥离 Android 应用程序的 native 代码符号

android - C 和 Arduino 之间的通信

android - Android 中的自定义搜索?

java - 如何正确删除Android中按钮周围的填充(或边距?)?

android - android froyo(api 8)上的底部操作栏

java - 我的 viewpager 中每个片段的不同标题

android - 使用 Android Studio 初始化 MobileAds.initialize 时突然出错

android - 具有两列和自动调整大小的图像的 Gridview