今天遇到了一个需求,在一个Activity里,要用与ActionBar一样颜色的一块布局,写在Activity的最上面,让它们看起来像是一片的样子。但是默认的情况下,在ActionBar下面是有一段阴影的,现在考虑如何将这个阴影去掉呢。
默认的样子中,使用的样式:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
MainActivity的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="120dip"
android:background="@color/colorPrimary"
android:gravity="center"
android:text="Hello World!"
android:textColor="@color/white" />
</RelativeLayout>
得到的图片的样子是:
可以清楚的看到,虽然TextView与ActionBar的颜色用的是同一个颜色,但是由于ActionBar下面有一条阴影,不是想要的效果,需要通过修改样式把这个阴影去掉!
去掉后的效果是:
在Android5.0之前,直接在主题样式中添加
<item name="android:windowContentOverlay">[@null](http://my.oschina.net/u/561366)</item>
即可,在Android5.0之后,在样式中添加一个ActionBar的样式
<style name="Theme.MyApp.ActionBar" parent="style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<!-- remove shadow below action bar -->
<!-- <item name="android:elevation">0dp</item> -->
<!-- Support library compatibility -->
<item name="elevation">0dp</item>
</style>
然后在theme中引入这个这个样式
<style name="Theme.MyApp" parent="Theme.AppCompat.Light">
<item name="actionBarStyle">@style/Theme.MyApp.ActionBar</item>
</style>
参考:http://stackoverflow.com/questions/12246388/remove-shadow-below-actionbar/12246593