为什么我的操作栏没有覆盖层?

4

我是Android开发的新手。我已经按照Android叠加训练课程进行了学习:

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 没有浮动(覆盖)?
感谢任何帮助。
1个回答

0

这是因为你的目标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>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接