隐藏和显示ActionBar调用hide()和show()这两个方法。这会导致Activity基于自己新的布局大小来重新计算和重绘布局.
当ActionBar隐藏或者是显示的时候,为了避免改变布局尺寸的大小,我们可以把ActionBar设定为overlay mode(覆盖模式)。当我们使用的是覆盖模式的时候,我们的Activity布局会使用所有的可用的空间,好像ActionBar不在那里一样,而且系统会在我们的布局(layout)前面绘制我们的ActionBar(就是说,用了覆盖模式之后,即使我们是隐藏后显示或者是显示之后隐藏,都不会感觉重新绘制,因为那个ActionBar它就是会覆盖在我们原来有的那个布局的最上面了)。这样可以解决以前自己在Activity的oncreate的时候用代码把ActionBar隐藏之后,但是一开始还是会先显示ActionBar然后才会不见的不友好的界面展示。这个覆盖了layout的头部,但是现在当ActionBar隐藏或者是显示的时候,系统就不必要重新测量layout的大小,并且是无缝连接的(看起来比较正常啦)
使用覆盖模式
要使用覆盖模式,需要创建一个继承于存在的ActionBar主题的主题,并且设置这个属性android:windowActionBarOverlay为true
3.0或者是3.0以上的:
<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>
兼容的:
<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>
注意到了哈:用支持库的,主题是有两个的第一个带着android前缀的用于支持新版本的,第二个是用于支持旧版本的。
使用覆盖模式最后一步:在layout.xml里面写paddingTop属性
3.0以及以上的:
<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>
使用支持库的:
<!-- Support library compatibility -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
...
</RelativeLayout>
这里没有android前缀的就和上面的不一样啦,它是兼容的,适用于2.1以及以上的