去掉ActionBar下面的陰影

今天遇到了一個需求,在一個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

相關文章
相關標籤/搜索