今天遇到了一个需求,在一个Activity里,要用与ActionBar同样颜色的一块布局,写在Activity的最上面,让它们看起来像是一片的样子。可是默认的状况下,在ActionBar下面是有一段阴影的,如今考虑如何将这个阴影去掉呢。android
默认的样子中,使用的样式:布局
<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的布局:.net
<?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>
获得的图片的样子是:3d
能够清楚的看到,虽然TextView与ActionBar的颜色用的是同一个颜色,可是因为ActionBar下面有一条阴影,不是想要的效果,须要经过修改样式把这个阴影去掉!code
去掉后的效果是:xml
在Android5.0以前,直接在主题样式中添加blog
<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中引入这个这个样式ip
<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/12246593utf-8